-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathdebounceCompletions.ts
70 lines (58 loc) · 2.24 KB
/
debounceCompletions.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
import * as vscode from "vscode";
import { Capability, isCapabilityEnabled } from "./capabilities/capabilities";
import getInlineCompletionItems from "./getInlineCompletionItems";
import TabnineInlineCompletionItem from "./inlineSuggestions/tabnineInlineCompletionItem";
import { sleep, timed } from "./utils/utils";
const ALPHA_ONE_SECOND_DEBOUNCE = 1000;
export default async function debounceCompletions(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): Promise<
vscode.InlineCompletionList<TabnineInlineCompletionItem> | undefined
> {
const { time, value: current } = await timed(() =>
getInlineCompletionItems(document, position, token)
);
const debounceTime = calculateDebounceMs(time);
if (debounceTime === 0) {
return current;
}
await debounceOrCancelOnRequest(token, debounceTime);
if (token.isCancellationRequested) {
return undefined;
}
// re fetch the most updated suggestions
return getInlineCompletionItems(document, position, token);
}
async function debounceOrCancelOnRequest(
token: vscode.CancellationToken,
debounceTime: number
) {
const canceledPromise = new Promise<void>((resolve) =>
token.onCancellationRequested(resolve)
);
await Promise.race([canceledPromise, sleep(debounceTime)]);
}
function calculateDebounceMs(time: number): number {
const debounceMilliseconds = getDebounceMs();
const debounceTime = Math.max(debounceMilliseconds - time, 0);
return debounceTime;
}
function getDebounceMs(): number {
const debounceMilliseconds = vscode.workspace
.getConfiguration()
.get<number>("tabnine.debounceMilliseconds");
const experimentDebounceMs = getDebounceMsByExperiment();
return debounceMilliseconds || experimentDebounceMs;
}
function getDebounceMsByExperiment(): number {
if (isCapabilityEnabled(Capability.ALPHA_CAPABILITY))
return ALPHA_ONE_SECOND_DEBOUNCE;
if (isCapabilityEnabled(Capability.DEBOUNCE_VALUE_300)) return 300;
if (isCapabilityEnabled(Capability.DEBOUNCE_VALUE_600)) return 600;
if (isCapabilityEnabled(Capability.DEBOUNCE_VALUE_900)) return 900;
if (isCapabilityEnabled(Capability.DEBOUNCE_VALUE_1200)) return 1200;
if (isCapabilityEnabled(Capability.DEBOUNCE_VALUE_1500)) return 1500;
return 0;
}