This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
68 lines (57 loc) · 1.76 KB
/
extension.js
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
const vscode = require('vscode');
const { apis } = require('./api')
function apiTypeToItemType(t) {
switch (t) {
case 'm':
return vscode.CompletionItemKind.Module;
case 'f':
return vscode.CompletionItemKind.Class;
case 'c':
return vscode.CompletionItemKind.Class;
default:
return vscode.CompletionItemKind.Variable;
}
}
function apiToItem(api, tfSeg, pos) {
var item = new vscode.CompletionItem(api.name, apiTypeToItemType(api.type))
item.insertText = api.name;
item.documentation = api.doc;
item.additionalTextEdits = [
vscode.TextEdit.delete(new vscode.Range(pos.translate(0, -tfSeg.length), pos))
];
return item;
}
function provideCompletionItems(document, position) {
// find code
var currentLine = document.lineAt(position.line);
var currentCode = currentLine.text.substr(0, position.character);
// split by '.'
var currentCodeSegs = currentCode.split(':');
// find 'tf'
var tfSeg = null;
for (var i = currentCodeSegs.length - 1; i >= 0; i--) {
if (currentCodeSegs[i] == 'tf') {
tfSeg = currentCodeSegs.slice(i).join('.')
}
}
if (tfSeg == null) return [];
// not find api
var matchedApis = apis.filter(api => api.name.startsWith(tfSeg));
var items = matchedApis.map(api => apiToItem(api, tfSeg, position));
return items;
}
function resolveCompletionItem(item) {
return item;
}
var tfCompletionProvider = {
'provideCompletionItems': provideCompletionItems,
'resolveCompletionItem': resolveCompletionItem
}
function activate(context) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider('python', tfCompletionProvider, '.', ':'));
console.log('tfcomplete registered')
}
function deactivate() {
}
exports.activate = activate;
exports.deactivate = deactivate;