-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathextension.ts
More file actions
60 lines (53 loc) · 1.75 KB
/
extension.ts
File metadata and controls
60 lines (53 loc) · 1.75 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
import * as vscode from 'vscode';
import { Range } from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('inline-completions demo started');
vscode.commands.registerCommand('demo-ext.command1', async (...args) => {
vscode.window.showInformationMessage('command1: ' + JSON.stringify(args));
});
const provider: vscode.InlineCompletionItemProvider = {
async provideInlineCompletionItems(document, position, context, token) {
console.log('provideInlineCompletionItems triggered');
const regexp = /\/\/ \[(.+?),(.+?)\)(.*?):(.*)/;
if (position.line <= 0) {
return;
}
const result: vscode.InlineCompletionList = {
items: [],
};
let offset = 1;
while (offset > 0) {
if (position.line - offset < 0) {
break;
}
const lineBefore = document.lineAt(position.line - offset).text;
const matches = lineBefore.match(regexp);
if (!matches) {
break;
}
offset++;
const start = matches[1];
const startInt = parseInt(start, 10);
const end = matches[2];
const endInt =
end === '*'
? document.lineAt(position.line).text.length
: parseInt(end, 10);
const flags = matches[3];
const isSnippet = flags.includes('s');
const text = matches[4].replace(/\\n/g, '\n');
result.items.push({
insertText: isSnippet ? new vscode.SnippetString(text) : text,
range: new Range(position.line, startInt, position.line, endInt),
command:{
command: 'demo-ext.command1',
title: 'My Inline Completion Demo Command',
arguments: [1, 2],
}
});
}
return result;
},
};
vscode.languages.registerInlineCompletionItemProvider({ pattern: '**' }, provider);
}