-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathrunCompletion.ts
147 lines (133 loc) · 4.01 KB
/
runCompletion.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { CancellationToken, Position, Range, TextDocument } from "vscode";
import {
autocomplete,
AutocompleteParams,
AutocompleteResult,
} from "./binary/requests/requests";
import getTabSize from "./binary/requests/tabSize";
import { Capability, isCapabilityEnabled } from "./capabilities/capabilities";
import {
CHAR_LIMIT,
INLINE_REQUEST_TIMEOUT,
MAX_NUM_RESULTS,
} from "./globals/consts";
import languages from "./globals/languages";
import { getSDKPath } from "./languages";
export default async function runCompletion({
document,
position,
timeout = undefined,
currentSuggestionText = "",
retry,
}: {
document: TextDocument;
position: Position;
timeout?: number | undefined;
currentSuggestionText?: string;
retry?: {
cancellationToken?: CancellationToken;
interval?: number;
timeout?: number;
};
}): Promise<AutocompleteResult | null | undefined> {
const offset = document.offsetAt(position);
const beforeStartOffset = Math.max(0, offset - CHAR_LIMIT);
const afterEndOffset = offset + CHAR_LIMIT;
const beforeStart = document.positionAt(beforeStartOffset);
const afterEnd = document.positionAt(afterEndOffset);
const requestData = {
filename: getFileNameWithExtension(document),
before:
document.getText(new Range(beforeStart, position)) +
currentSuggestionText,
after: document.getText(new Range(position, afterEnd)),
region_includes_beginning: beforeStartOffset === 0,
region_includes_end: document.offsetAt(afterEnd) !== afterEndOffset,
max_num_results: getMaxResults(),
offset,
line: position.line,
character: position.character,
indentation_size: getTabSize(),
sdk_path: getSDKPath(document.languageId),
};
const isEmptyLine = document.lineAt(position.line).text.trim().length === 0;
const result = await autocomplete(
requestData,
isEmptyLine ? INLINE_REQUEST_TIMEOUT : timeout
);
if (result?.results.length || !retry?.cancellationToken) {
return result;
}
return handleRetries(requestData, retry);
}
function handleRetries(
requestData: AutocompleteParams,
{
cancellationToken,
interval = 200,
timeout = 1000,
}: {
cancellationToken?: CancellationToken;
interval?: number;
timeout?: number;
}
): Promise<AutocompleteResult | null | undefined> | null | undefined {
if (cancellationToken?.isCancellationRequested) {
return null;
}
return new Promise((resolve, reject) => {
let timeoutId: NodeJS.Timeout | undefined;
let lastResult: AutocompleteResult | undefined;
const intervalId = setInterval(() => {
void autocomplete({ ...requestData, cached_only: true })
.then((result) => {
if (result?.results.length) {
clearInterval(intervalId);
clearTimeout(timeoutId as NodeJS.Timeout);
lastResult = result;
resolve(result);
}
})
.catch((error) => {
clearInterval(intervalId);
clearTimeout(timeoutId as NodeJS.Timeout);
reject(error);
});
}, interval);
timeoutId = setTimeout(() => {
clearInterval(intervalId);
resolve(lastResult);
}, timeout);
cancellationToken?.onCancellationRequested(() => {
clearInterval(intervalId);
clearTimeout(timeoutId as NodeJS.Timeout);
resolve(null);
});
});
}
function getMaxResults(): number {
if (isCapabilityEnabled(Capability.SUGGESTIONS_SINGLE)) {
return 1;
}
if (isCapabilityEnabled(Capability.SUGGESTIONS_TWO)) {
return 2;
}
return MAX_NUM_RESULTS;
}
type KnownLanguageType = keyof typeof languages;
export function getLanguageFileExtension(
languageId: string
): string | undefined {
return languages[languageId as KnownLanguageType];
}
function getFileNameWithExtension(document: TextDocument): string {
const { languageId, fileName } = document;
if (!document.isUntitled) {
return fileName;
}
const extension = getLanguageFileExtension(languageId);
if (extension) {
return fileName.concat(extension);
}
return fileName;
}