forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletionProvider.ts
More file actions
71 lines (64 loc) · 3.08 KB
/
completionProvider.ts
File metadata and controls
71 lines (64 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
import * as vscode from 'vscode';
import * as proxy from './jediProxy';
import * as telemetryContracts from '../common/telemetryContracts';
import { extractSignatureAndDocumentation } from './jediHelpers';
import { EOL } from 'os';
import { PythonSettings } from '../common/configSettings';
const pythonSettings = PythonSettings.getInstance();
export class PythonCompletionItemProvider implements vscode.CompletionItemProvider {
private jediProxyHandler: proxy.JediProxyHandler<proxy.ICompletionResult>;
public constructor(context: vscode.ExtensionContext, jediProxy: proxy.JediProxy = null) {
this.jediProxyHandler = new proxy.JediProxyHandler(context, jediProxy);
}
private static parseData(data: proxy.ICompletionResult): vscode.CompletionItem[] {
if (data && data.items.length > 0) {
return data.items.map(item => {
const sigAndDocs = extractSignatureAndDocumentation(item);
let completionItem = new vscode.CompletionItem(item.text);
completionItem.kind = item.type;
completionItem.documentation = sigAndDocs[1].length === 0 ? item.description : sigAndDocs[1];
completionItem.detail = sigAndDocs[0].split(EOL).join('');
if (pythonSettings.autoComplete.addBrackets === true &&
(item.kind === vscode.SymbolKind.Function || item.kind === vscode.SymbolKind.Method)) {
completionItem.insertText = item.text + '({{}})';
}
// ensure the built in memebers are at the bottom
completionItem.sortText = (completionItem.label.startsWith('__') ? 'z' : (completionItem.label.startsWith('_') ? 'y' : '__')) + completionItem.label;
return completionItem;
});
}
return [];
}
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
if (position.character <= 0) {
return Promise.resolve([]);
}
const filename = document.fileName;
const lineText = document.lineAt(position.line).text;
if (lineText.match(/^\s*\/\//)) {
return Promise.resolve([]);
}
// If starts with a comment, then return
if (lineText.trim().startsWith('#')) {
return Promise.resolve([]);
}
// If starts with a """ (possible doc string), then return
if (lineText.trim().startsWith('"""')) {
return Promise.resolve([]);
}
const type = proxy.CommandType.Completions;
const columnIndex = position.character;
const source = document.getText();
const cmd: proxy.ICommand<proxy.ICommandResult> = {
command: type,
fileName: filename,
columnIndex: columnIndex,
lineIndex: position.line,
source: source
};
return this.jediProxyHandler.sendCommand(cmd, token).then(data => {
return PythonCompletionItemProvider.parseData(data);
});
}
}