-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathgetInlineCompletionItems.ts
54 lines (50 loc) · 1.62 KB
/
getInlineCompletionItems.ts
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
import * as vscode from "vscode";
import TabnineInlineCompletionItem from "./inlineSuggestions/tabnineInlineCompletionItem";
import runCompletion from "./runCompletion";
import getAutoImportCommand from "./getAutoImportCommand";
import { SuggestionTrigger } from "./globals/consts";
import { AutocompleteResult, ResultEntry } from "./binary/requests/requests";
import { isMultiline } from "./utils/utils";
export default async function getInlineCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
cancellationToken: vscode.CancellationToken
): Promise<vscode.InlineCompletionList<TabnineInlineCompletionItem>> {
const response = await runCompletion({
document,
position,
retry: {
cancellationToken,
},
});
const completions = response?.results.map(
(result) =>
new TabnineInlineCompletionItem(
result.new_prefix,
result,
calculateRange(position, response, result),
getAutoImportCommand(
result,
response,
position,
SuggestionTrigger.DocumentChanged
),
result.completion_metadata?.completion_kind,
result.completion_metadata?.is_cached,
result.completion_metadata?.snippet_context
)
);
return new vscode.InlineCompletionList(completions || []);
}
function calculateRange(
position: vscode.Position,
response: AutocompleteResult,
result: ResultEntry
): vscode.Range {
return new vscode.Range(
position.translate(0, -response.old_prefix.length),
isMultiline(result.old_suffix)
? position
: position.translate(0, result.old_suffix.length)
);
}