Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 176c477

Browse files
committed
#898 #265 #630 #727 #746 and more
1 parent ad8345a commit 176c477

File tree

19 files changed

+699
-16
lines changed

19 files changed

+699
-16
lines changed

pythonFiles/completion.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def _get_call_signatures(self, script):
103103
call_signatures = script.call_signatures()
104104
except KeyError:
105105
call_signatures = []
106+
except :
107+
call_signatures = []
106108
for signature in call_signatures:
107109
for pos, param in enumerate(signature.params):
108110
if not param.name:
@@ -222,6 +224,8 @@ def _serialize_completions(self, script, identifier=None, prefix=''):
222224
completions = script.completions()
223225
except KeyError:
224226
completions = []
227+
except :
228+
completions = []
225229
for completion in completions:
226230
if self.show_doc_strings:
227231
try:
@@ -635,18 +639,40 @@ def _process_request(self, request):
635639
column=request['column'], path=request.get('path', ''))
636640

637641
if lookup == 'definitions':
638-
defs = self._get_definitionsx(script.goto_definitions(), request['id'])
639-
if len(defs) == 0:
640-
defs = self._get_definitionsx(script.goto_assignments(), request['id'])
642+
defs = []
643+
try:
644+
defs = self._get_definitionsx(script.goto_assignments(follow_imports=False), request['id'])
645+
except:
646+
pass
647+
try:
648+
if len(defs) == 0:
649+
defs = self._get_definitionsx(script.goto_definitions(), request['id'])
650+
except:
651+
pass
652+
try:
653+
if len(defs) == 0:
654+
defs = self._get_definitionsx(script.goto_assignments(), request['id'])
655+
except:
656+
pass
641657
return json.dumps({'id': request['id'], 'results': defs})
642658
if lookup == 'tooltip':
643659
if jediPreview:
644-
defs = self._get_definitionsx(script.goto_definitions(), request['id'], True)
645-
if len(defs) == 0:
646-
defs = self._get_definitionsx(script.goto_assignments(), request['id'], True)
660+
defs = []
661+
try:
662+
defs = self._get_definitionsx(script.goto_definitions(), request['id'], True)
663+
except:
664+
pass
665+
try:
666+
if len(defs) == 0:
667+
defs = self._get_definitionsx(script.goto_assignments(), request['id'], True)
668+
except:
669+
pass
647670
return json.dumps({'id': request['id'], 'results': defs})
648671
else:
649-
return self._serialize_tooltip(script.goto_definitions(), request['id'])
672+
try:
673+
return self._serialize_tooltip(script.goto_definitions(), request['id'])
674+
except:
675+
return json.dumps({'id': request['id'], 'results': []})
650676
elif lookup == 'arguments':
651677
return self._serialize_arguments(
652678
script, request['id'])

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ pydocstyle==1.0.0
88
jupyter
99
ipython
1010
nose
11-
pytest
11+
pytest
12+
fabric
13+
numba

src/test/autocomplete/base.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ let pythonSettings = settings.PythonSettings.getInstance();
1717
let autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'autocomp');
1818
const fileOne = path.join(autoCompPath, 'one.py');
1919
const fileImport = path.join(autoCompPath, 'imp.py');
20+
const fileDoc = path.join(autoCompPath, 'doc.py');
21+
const fileLambda = path.join(autoCompPath, 'lamb.py');
22+
const fileDecorator = path.join(autoCompPath, 'deco.py');
2023
const fileEncoding = path.join(autoCompPath, 'four.py');
2124
const fileEncodingUsed = path.join(autoCompPath, 'five.py');
2225

@@ -62,6 +65,60 @@ suite('Autocomplete', () => {
6265
assert.notEqual(list.items.filter(item => item.label === 'fstat').length, 0, 'fstat not found');
6366
});
6467

68+
// https://github.com/DonJayamanne/pythonVSCode/issues/898
69+
test('For "f.readlines()"', async () => {
70+
const textDocument = await vscode.workspace.openTextDocument(fileDoc);
71+
await vscode.window.showTextDocument(textDocument);
72+
const position = new vscode.Position(5, 27);
73+
const list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
74+
assert.notEqual(list.items.filter(item => item.label === 'capitalize').length, 0, 'capitalize not found (known not to work, Jedi issue)');
75+
assert.notEqual(list.items.filter(item => item.label === 'upper').length, 0, 'upper not found');
76+
assert.notEqual(list.items.filter(item => item.label === 'lower').length, 0, 'lower not found');
77+
});
78+
79+
// https://github.com/DonJayamanne/pythonVSCode/issues/265
80+
test('For "lambda"', async () => {
81+
const textDocument = await vscode.workspace.openTextDocument(fileLambda);
82+
await vscode.window.showTextDocument(textDocument);
83+
const position = new vscode.Position(1, 19);
84+
const list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
85+
assert.notEqual(list.items.filter(item => item.label === 'append').length, 0, 'append not found');
86+
assert.notEqual(list.items.filter(item => item.label === 'clear').length, 0, 'clear not found');
87+
assert.notEqual(list.items.filter(item => item.label === 'count').length, 0, 'cound not found');
88+
});
89+
90+
// https://github.com/DonJayamanne/pythonVSCode/issues/630
91+
test('For "abc.decorators"', async () => {
92+
const textDocument = await vscode.workspace.openTextDocument(fileDecorator);
93+
await vscode.window.showTextDocument(textDocument);
94+
let position = new vscode.Position(3, 9);
95+
let list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
96+
assert.notEqual(list.items.filter(item => item.label === 'ABCMeta').length, 0, 'ABCMeta not found');
97+
assert.notEqual(list.items.filter(item => item.label === 'abstractmethod').length, 0, 'abstractmethod not found');
98+
99+
position = new vscode.Position(4, 9);
100+
list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
101+
assert.notEqual(list.items.filter(item => item.label === 'ABCMeta').length, 0, 'ABCMeta not found');
102+
assert.notEqual(list.items.filter(item => item.label === 'abstractmethod').length, 0, 'abstractmethod not found');
103+
104+
position = new vscode.Position(2, 30);
105+
list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
106+
assert.notEqual(list.items.filter(item => item.label === 'ABCMeta').length, 0, 'ABCMeta not found');
107+
assert.notEqual(list.items.filter(item => item.label === 'abstractmethod').length, 0, 'abstractmethod not found');
108+
});
109+
110+
// https://github.com/DonJayamanne/pythonVSCode/issues/727
111+
// https://github.com/DonJayamanne/pythonVSCode/issues/746
112+
// https://github.com/davidhalter/jedi/issues/859
113+
test('For "time.slee"', async () => {
114+
const textDocument = await vscode.workspace.openTextDocument(fileDoc);
115+
await vscode.window.showTextDocument(textDocument);
116+
const position = new vscode.Position(10, 9);
117+
const list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
118+
assert.notEqual(list.items.filter(item => item.label === 'sleep').length, 0, 'sleep not found');
119+
assert.notEqual(list.items.filter(item => item.documentation.startsWith("Delay execution for a given number of seconds. The argument may be")).length, 0, 'Documentation incorrect');
120+
});
121+
65122
test('For custom class', done => {
66123
let textEditor: vscode.TextEditor;
67124
let textDocument: vscode.TextDocument;

src/test/autocomplete/pep526.test.ts

+58-2
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,73 @@ suite('Autocomplete PEP 526', () => {
3838
closeActiveWindows().then(() => done(), () => done());
3939
});
4040

41-
test('variable', async () => {
41+
test('variable (abc:str)', async () => {
4242
if (!await isPython3) {
4343
return;
4444
}
4545
let textDocument = await vscode.workspace.openTextDocument(filePep526);
4646
await vscode.window.showTextDocument(textDocument);
4747
assert(vscode.window.activeTextEditor, 'No active editor');
48-
const position = new vscode.Position(3, 8);
48+
const position = new vscode.Position(9, 8);
4949
let list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
5050
assert.notEqual(list.items.filter(item => item.label === 'capitalize').length, 0, 'capitalize not found');
5151
assert.notEqual(list.items.filter(item => item.label === 'upper').length, 0, 'upper not found');
5252
assert.notEqual(list.items.filter(item => item.label === 'lower').length, 0, 'lower not found');
5353
});
54+
55+
test('variable (abc: str = "")', async () => {
56+
if (!await isPython3) {
57+
return;
58+
}
59+
let textDocument = await vscode.workspace.openTextDocument(filePep526);
60+
await vscode.window.showTextDocument(textDocument);
61+
assert(vscode.window.activeTextEditor, 'No active editor');
62+
const position = new vscode.Position(8, 14);
63+
let list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
64+
assert.notEqual(list.items.filter(item => item.label === 'capitalize').length, 0, 'capitalize not found');
65+
assert.notEqual(list.items.filter(item => item.label === 'upper').length, 0, 'upper not found');
66+
assert.notEqual(list.items.filter(item => item.label === 'lower').length, 0, 'lower not found');
67+
});
68+
69+
test('variable (abc = UNKNOWN # type: str)', async () => {
70+
if (!await isPython3) {
71+
return;
72+
}
73+
let textDocument = await vscode.workspace.openTextDocument(filePep526);
74+
await vscode.window.showTextDocument(textDocument);
75+
assert(vscode.window.activeTextEditor, 'No active editor');
76+
const position = new vscode.Position(7, 14);
77+
let list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
78+
assert.notEqual(list.items.filter(item => item.label === 'capitalize').length, 0, 'capitalize not found');
79+
assert.notEqual(list.items.filter(item => item.label === 'upper').length, 0, 'upper not found');
80+
assert.notEqual(list.items.filter(item => item.label === 'lower').length, 0, 'lower not found');
81+
});
82+
83+
test('class methods', async () => {
84+
if (!await isPython3) {
85+
return;
86+
}
87+
let textDocument = await vscode.workspace.openTextDocument(filePep526);
88+
await vscode.window.showTextDocument(textDocument);
89+
assert(vscode.window.activeTextEditor, 'No active editor');
90+
let position = new vscode.Position(20, 4);
91+
let list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
92+
assert.notEqual(list.items.filter(item => item.label === 'a').length, 0, 'method a not found');
93+
94+
position = new vscode.Position(21, 4);
95+
list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
96+
assert.notEqual(list.items.filter(item => item.label === 'b').length, 0, 'method b not found');
97+
});
98+
99+
test('class method types', async () => {
100+
if (!await isPython3) {
101+
return;
102+
}
103+
let textDocument = await vscode.workspace.openTextDocument(filePep526);
104+
await vscode.window.showTextDocument(textDocument);
105+
assert(vscode.window.activeTextEditor, 'No active editor');
106+
const position = new vscode.Position(21, 6);
107+
const list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
108+
assert.notEqual(list.items.filter(item => item.label === 'bit_length').length, 0, 'bit_length not found');
109+
});
54110
});

src/test/definitions/code.test.ts

+65-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
// Place this right on top
6-
import { initialize, PYTHON_PATH, closeActiveWindows } from '../initialize';
6+
import { initialize, PYTHON_PATH, closeActiveWindows, setPythonExecutable } from '../initialize';
77
// The module 'assert' provides assertion methods from node
88
import * as assert from 'assert';
99
import { EOL } from 'os';
@@ -12,22 +12,29 @@ import { EOL } from 'os';
1212
import * as vscode from 'vscode';
1313
import * as path from 'path';
1414
import * as settings from '../../client/common/configSettings';
15+
import { execPythonFile } from '../../client/common/utils';
16+
import { createDeferred } from '../../client/common/helpers';
1517

1618
let pythonSettings = settings.PythonSettings.getInstance();
19+
let disposable: vscode.Disposable;
1720
let autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'definition');
1821
const fileOne = path.join(autoCompPath, 'one.py');
1922
const fileTwo = path.join(autoCompPath, 'two.py');
2023
const fileThree = path.join(autoCompPath, 'three.py');
2124
const fileDecorator = path.join(autoCompPath, 'decorators.py');
25+
const fileAwait = path.join(autoCompPath, 'await.test.py');
2226
const fileEncoding = path.join(autoCompPath, 'four.py');
2327
const fileEncodingUsed = path.join(autoCompPath, 'five.py');
2428

29+
2530
suite('Code Definition', () => {
26-
suiteSetup(done => {
27-
initialize().then(() => {
28-
pythonSettings.pythonPath = PYTHON_PATH;
29-
done();
30-
}, done);
31+
const isPython3Deferred = createDeferred<boolean>();
32+
const isPython3 = isPython3Deferred.promise;
33+
suiteSetup(async () => {
34+
disposable = setPythonExecutable(pythonSettings);
35+
await initialize();
36+
let version = await execPythonFile(pythonSettings.pythonPath, ['--version'], __dirname, true);
37+
isPython3Deferred.resolve(version.indexOf('3.') >= 0);
3138
});
3239

3340
suiteTeardown(done => {
@@ -50,6 +57,7 @@ suite('Code Definition', () => {
5057
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
5158
}).then(def => {
5259
assert.equal(def.length, 1, 'Definition length is incorrect');
60+
assert.equal(def[0].uri.fsPath, fileOne, 'Incorrect file');
5361
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '17,4', 'Start position is incorrect');
5462
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '21,11', 'End position is incorrect');
5563
}).then(done, done);
@@ -68,6 +76,7 @@ suite('Code Definition', () => {
6876
return vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
6977
}).then(def => {
7078
assert.equal(def.length, 1, 'Definition length is incorrect');
79+
assert.equal(def[0].uri.fsPath, fileOne, 'Incorrect file');
7180
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '32,0', 'Start position is incorrect');
7281
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '33,21', 'End position is incorrect');
7382
}).then(done, done);
@@ -79,10 +88,32 @@ suite('Code Definition', () => {
7988
const position = new vscode.Position(7, 2);
8089
const def = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
8190
assert.equal(def.length, 1, 'Definition length is incorrect');
91+
assert.equal(def[0].uri.fsPath, fileDecorator, 'Incorrect file');
8292
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '4,0', 'Start position is incorrect');
8393
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '5,22', 'End position is incorrect');
8494
});
8595

96+
test('Go to function with decorator (jit)', async () => {
97+
const textDocument = await vscode.workspace.openTextDocument(fileDecorator);
98+
await vscode.window.showTextDocument(textDocument);
99+
const position = new vscode.Position(27, 2);
100+
const def = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
101+
assert.equal(def.length, 1, 'Definition length is incorrect');
102+
assert.equal(def[0].uri.fsPath, fileDecorator, 'Incorrect file');
103+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '19,0', 'Start position is incorrect');
104+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '26,42', 'End position is incorrect');
105+
});
106+
107+
test('Go to function with decorator (fabric)', async () => {
108+
const textDocument = await vscode.workspace.openTextDocument(fileDecorator);
109+
await vscode.window.showTextDocument(textDocument);
110+
const position = new vscode.Position(13, 2);
111+
const def = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
112+
assert.equal(def.length, 1, 'Definition length is incorrect');
113+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '19,0', 'Start position is incorrect');
114+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '26,42', 'End position is incorrect');
115+
});
116+
86117
test('Go to function decorator', async () => {
87118
const textDocument = await vscode.workspace.openTextDocument(fileDecorator);
88119
await vscode.window.showTextDocument(textDocument);
@@ -93,6 +124,34 @@ suite('Code Definition', () => {
93124
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '1,12', 'End position is incorrect');
94125
});
95126

127+
test('Go to async method', async () => {
128+
if (!await isPython3) {
129+
return;
130+
}
131+
const textDocument = await vscode.workspace.openTextDocument(fileAwait);
132+
await vscode.window.showTextDocument(textDocument);
133+
const position = new vscode.Position(10, 22);
134+
const def = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
135+
assert.equal(def.length, 1, 'Definition length is incorrect (currently not working)');
136+
assert.equal(def[0].uri.fsPath, fileAwait, 'Wrong file (currently not working)');
137+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '6,10', 'Start position is incorrect (currently not working)');
138+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '1,12', 'End position is incorrect (currently not working)');
139+
});
140+
141+
test('Go to async function', async () => {
142+
if (!await isPython3) {
143+
return;
144+
}
145+
const textDocument = await vscode.workspace.openTextDocument(fileAwait);
146+
await vscode.window.showTextDocument(textDocument);
147+
const position = new vscode.Position(18, 12);
148+
const def = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, position);
149+
assert.equal(def.length, 1, 'Definition length is incorrect (currently not working)');
150+
assert.equal(def[0].uri.fsPath, fileAwait, 'Wrong file (currently not working)');
151+
assert.equal(`${def[0].range.start.line},${def[0].range.start.character}`, '6,10', 'Start position is incorrect (currently not working)');
152+
assert.equal(`${def[0].range.end.line},${def[0].range.end.character}`, '1,12', 'End position is incorrect (currently not working)');
153+
});
154+
96155
test('Across files', done => {
97156
let textEditor: vscode.TextEditor;
98157
let textDocument: vscode.TextDocument;

0 commit comments

Comments
 (0)