-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathprovideCompletionItems.ts
227 lines (197 loc) · 5.75 KB
/
provideCompletionItems.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import * as vscode from "vscode";
import {
AutocompleteResult,
MarkdownStringSpec,
ResultEntry,
} from "./binary/requests/requests";
import {
ATTRIBUTION_BRAND,
BRAND_NAME,
DEFAULT_DETAIL,
LIMITATION_SYMBOL,
} from "./globals/consts";
import tabnineExtensionProperties from "./globals/tabnineExtensionProperties";
import runCompletion from "./runCompletion";
import { COMPLETION_IMPORTS } from "./selectionHandler";
import { setCompletionStatus } from "./statusBar/statusBar";
import { escapeTabStopSign } from "./utils/utils";
import { Logger } from "./utils/logger";
const INCOMPLETE = true;
export default async function provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position
): Promise<vscode.CompletionList> {
return new vscode.CompletionList(
await completionsListFor(document, position),
INCOMPLETE
);
}
async function completionsListFor(
document: vscode.TextDocument,
position: vscode.Position
): Promise<vscode.CompletionItem[]> {
try {
if (!completionIsAllowed(document, position)) {
return [];
}
const response = await runCompletion({ document, position });
setCompletionStatus(response?.is_locked);
if (!response || response?.results.length === 0) {
return [];
}
const limit =
showFew(response, document, position) || response.is_locked
? 1
: response.results.length;
return response.results.slice(0, limit).map((entry, index) =>
makeCompletionItem({
document,
index,
position,
detailMessage: extractDetailMessage(response),
oldPrefix: response?.old_prefix,
entry,
results: response?.results,
limited: response?.is_locked,
})
);
} catch (e) {
Logger.error(`Error setting up request: ${e}`);
return [];
}
}
function extractDetailMessage(response: AutocompleteResult) {
return (response.user_message || []).join("\n") || DEFAULT_DETAIL;
}
function makeCompletionItem(args: {
document: vscode.TextDocument;
index: number;
position: vscode.Position;
detailMessage: string;
oldPrefix: string;
entry: ResultEntry;
results: ResultEntry[];
limited: boolean;
}): vscode.CompletionItem {
const item = new vscode.CompletionItem(
ATTRIBUTION_BRAND + args.entry.new_prefix
);
if (args.limited) {
item.detail = `${LIMITATION_SYMBOL} ${BRAND_NAME}`;
} else {
item.detail = BRAND_NAME;
}
item.sortText = String.fromCharCode(0) + String.fromCharCode(args.index);
item.insertText = new vscode.SnippetString(
escapeTabStopSign(args.entry.new_prefix)
);
item.filterText = args.entry.new_prefix;
item.preselect = args.index === 0;
item.kind = args.entry.completion_metadata?.kind;
item.range = new vscode.Range(
args.position.translate(0, -args.oldPrefix.length),
args.position.translate(0, args.entry.old_suffix.length)
);
if (tabnineExtensionProperties.isTabNineAutoImportEnabled) {
item.command = {
arguments: [
{
currentCompletion: args.entry.new_prefix,
completions: args.results,
position: args.position,
limited: args.limited,
oldPrefix: args.oldPrefix,
},
],
command: COMPLETION_IMPORTS,
title: "accept completion",
};
}
if (args.entry.new_suffix) {
item.insertText
.appendTabstop(0)
.appendText(escapeTabStopSign(args.entry.new_suffix));
}
if (args.entry.completion_metadata?.documentation) {
item.documentation = formatDocumentation(
args.entry.completion_metadata?.documentation
);
}
return item;
}
function formatDocumentation(
documentation: string | MarkdownStringSpec
): string | vscode.MarkdownString {
if (isMarkdownStringSpec(documentation)) {
if (documentation.kind === "markdown") {
return new vscode.MarkdownString(documentation.value);
}
return documentation.value;
}
return documentation;
}
function isMarkdownStringSpec(
x: string | MarkdownStringSpec
): x is MarkdownStringSpec {
return !(typeof x === "string");
}
export function completionIsAllowed(
document: vscode.TextDocument,
position: vscode.Position
): boolean {
const configuration = vscode.workspace.getConfiguration();
const disableLineRegex = getMisnamedConfigPropertyValue(
"tabnine.disableLineRegex",
"tabnine.disable_line_regex",
configuration
);
const line = document.getText(
new vscode.Range(
position.with({ character: 0 }),
position.with({ character: 500 })
)
);
if (disableLineRegex.some((r) => new RegExp(r).test(line))) {
return false;
}
const disableFileRegex = getMisnamedConfigPropertyValue(
"tabnine.disableFileRegex",
"tabnine.disable_file_regex",
configuration
);
return !disableFileRegex.some((r) => new RegExp(r).test(document.fileName));
}
function getMisnamedConfigPropertyValue(
properPropName: string,
propMisname: string,
configuration: vscode.WorkspaceConfiguration
): string[] {
let disableLineRegex = configuration.get<string[]>(properPropName);
if (!disableLineRegex || !disableLineRegex.length) {
disableLineRegex = configuration.get<string[]>(propMisname);
}
if (disableLineRegex === undefined) {
disableLineRegex = [];
}
return disableLineRegex;
}
function showFew(
response: AutocompleteResult,
document: vscode.TextDocument,
position: vscode.Position
): boolean {
if (
response.results.some(
(entry) =>
entry.completion_metadata?.kind ||
entry.completion_metadata?.documentation
)
) {
return false;
}
const leftPoint = position.translate(0, -response.old_prefix.length);
const tail = document.getText(
new vscode.Range(document.lineAt(leftPoint).range.start, leftPoint)
);
return tail.endsWith(".") || tail.endsWith("::");
}