Skip to content

Commit

Permalink
Merge from 3.x: PR #3757
Browse files Browse the repository at this point in the history
Fixes #3704
  • Loading branch information
ccordoba12 committed Dec 6, 2016
2 parents 0b7dba2 + a7d8e81 commit 9fcd78f
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 3 deletions.
32 changes: 29 additions & 3 deletions spyder/utils/introspection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _get_info(self):
self.position = self.position - len(self.line) + self.column

def _get_docstring(self):
"""Find the docstring we are currently in"""
"""Find the docstring we are currently in."""
left = self.position
while left:
if self.source_code[left: left + 3] in ['"""', "'''"]:
Expand All @@ -150,7 +150,7 @@ def __eq__(self, other):
return False

def __getitem__(self, item):
"""Allow dictionary-like access"""
"""Allow dictionary-like access."""
return getattr(self, item)

def serialize(self):
Expand Down Expand Up @@ -210,11 +210,37 @@ def get_keywords(lexer):
continue
return keywords

def get_words(file_path=None, content=None, extension=None):
"""
Extract all words from a source code file to be used in code completion.
Extract the list of words that contains the file in the editor,
to carry out the inline completion similar to VSCode.
"""
if (file_path is None and (content is None or extension is None) or
file_path and content and extension):
error_msg = ('Must provide `file_path` or `content` and `extension`')
raise Exception(error_msg)

if file_path and content is None and extension is None:
extension = os.path.splitext(file_path)[1]
with open(file_path) as infile:
content = infile.read()

if extension in ['.css']:
regex = re.compile(r'([^a-zA-Z-])')
elif extension in ['.R', '.c', 'md', '.cpp, java', '.py']:
regex = re.compile(r'([^a-zA-Z_])')
else:
regex = re.compile(r'([^a-zA-Z])')

words = sorted(set(regex.sub(r' ', content).split()))
return words

@memoize
def get_parent_until(path):
"""
Given a file path, determine the full module path
Given a file path, determine the full module path.
e.g. '/usr/lib/python2.7/dist-packages/numpy/core/__init__.pyc' yields
'numpy.core'
Expand Down
3 changes: 3 additions & 0 deletions spyder/utils/tests/data/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello <- function( name ) {
sprintf( "Hello, %s", name );
}
22 changes: 22 additions & 0 deletions spyder/utils/tests/data/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>

int main()
{
struct foo
{
int x;
float y;
};

struct foo var;
struct foo* pvar;
pvar = &var;

var.x = 5;
(&var)->y = 14.3;
printf("%i - %.02f\n", var.x, (&var)->y);
pvar->x = 6;
pvar->y = 22.4;
printf("%i - %.02f\n", pvar->x, pvar->y);
return 0;
}
74 changes: 74 additions & 0 deletions spyder/utils/tests/data/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
using namespace std;

// Consider an actual class.
class Obj {
static int i, j;

public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
};

// Static member definitions:
int Obj::i = 10;
int Obj::j = 12;

// Implement a container for the above class
class ObjContainer {
vector<Obj*> a;
public:
void add(Obj* obj) {
a.push_back(obj); // call vector's standard method.
}
friend class SmartPointer;
};

// implement smart pointer to access member of Obj class.
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc) {
oc = objc;
index = 0;
}

// Return value indicates end of list:
bool operator++() // Prefix version {
if(index >= oc.a.size()) return false;
if(oc.a[++index] == 0) return false;
return true;
}

bool operator++(int) // Postfix version {
return operator++();
}

// overload operator->
Obj* operator->() const {
if(!oc.a[index]) {
cout << "Zero value";
return (Obj*)0;
}
return oc.a[index];
}
};

int main() {
const int sz = 10;
Obj o[sz];
ObjContainer oc;

for(int i = 0; i < sz; i++) {
oc.add(&o[i]);
}

SmartPointer sp(oc); // Create an iterator
do {
sp->f(); // smart pointer call
sp->g();
} while(sp++);
return 0;
}
7 changes: 7 additions & 0 deletions spyder/utils/tests/data/example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<style type="text/css">
h1 {
color: DeepSkyBlue;
}
</style>

<h1>Hello, world! nombre-valido </h1>
13 changes: 13 additions & 0 deletions spyder/utils/tests/data/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<p>
Jamie was here.
</p>
</body>
</html>
13 changes: 13 additions & 0 deletions spyder/utils/tests/data/example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/******************************************************************************
* Compilation: javac HelloWorld.java
* Execution: java HelloWorld
******************************************************************************/

public class HelloWorld {

public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}

}
41 changes: 41 additions & 0 deletions spyder/utils/tests/data/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
*Italic*
**Bold**
# Heading 1
=========
## Heading 2
---------
[Link](http://a.com)
[Link][1]
[1]: http://b.org
![Image](http://url/a.png)
![Image][1]
[1]: http://url/b.jpg
> Blockquote
A paragraph.
A paragraph after 1 blank line.

* List
* List
* List

- List
- List
- List

1. One
2. Two
3. Three

1) One
2) Two
3) Three

--- Horizontal Rule

*** Horizontal Rule
`Inline code` with backticks
```
# code block
print '3 backticks or'
print 'indent 4 spaces'
```
63 changes: 63 additions & 0 deletions spyder/utils/tests/data/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
********************* VerySimpleWebBrowser ************************
This is a Very Simple Web Browser implemented over Qt and QtWebKit.
author: Juan Manuel Garcia <[email protected]> ~/home
*******************************************************************
"""

import numpy as np

print (__file__)

def iterate_1(Z):
# Count neighbours
N = np.zeros(Z.shape, int)
N[1:-1,1:-1] += (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
Z[1:-1,0:-2] + Z[1:-1,2:] +
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])
N_ = N.ravel()
Z_ = Z.ravel()

# Apply rules
R1 = np.argwhere( (Z_==1) & (N_ < 2) )
R2 = np.argwhere( (Z_==1) & (N_ > 3) )
R3 = np.argwhere( (Z_==1) & ((N_==2) | (N_==3)) )
R4 = np.argwhere( (Z_==0) & (N_==3) )

# Set new values
Z_[R1] = 0
Z_[R2] = 0
Z_[R3] = Z_[R3]
Z_[R4] = 1

# Make sure borders stay null
Z[0,:] = Z[-1,:] = Z[:,0] = Z[:,-1] = 0


def iterate_2(Z):
# Count neighbours
N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
Z[1:-1,0:-2] + Z[1:-1,2:] +
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])

# Apply rules
birth = (N==3) & (Z[1:-1,1:-1]==0)
survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)
Z[...] = 0
Z[1:-1,1:-1][birth | survive] = 1
return Z

Z = np.array([[0,0,0,0,0,0],
[0,0,0,1,0,0],
[0,1,0,1,0,0],
[0,0,1,1,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]])

#print(Z)
for i in range(4):
iterate_2(Z)
#print (Z)
Loading

0 comments on commit 9fcd78f

Please sign in to comment.