-
Notifications
You must be signed in to change notification settings - Fork 130
/
completions.ts
332 lines (284 loc) · 13 KB
/
completions.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import * as vscode from 'vscode';
import * as session from './session';
import { extendSelection } from './selection';
import { cleanLine } from './lineCache';
import { globalRHelp } from './extension';
import { config } from './util';
import { getChunks } from './rmarkdown';
import { CompletionItemKind } from 'vscode-languageclient';
// Get with names(roxygen2:::default_tags())
const roxygenTagCompletionItems = [
'export', 'exportClass', 'exportMethod', 'exportPattern', 'import', 'importClassesFrom',
'importFrom', 'importMethodsFrom', 'rawNamespace', 'S3method', 'useDynLib', 'aliases',
'author', 'backref', 'concept', 'describeIn', 'description', 'details',
'docType', 'encoding', 'evalRd', 'example', 'examples', 'family',
'field', 'format', 'inherit', 'inheritParams', 'inheritDotParams', 'inheritSection',
'keywords', 'method', 'name', 'md', 'noMd', 'noRd',
'note', 'param', 'rdname', 'rawRd', 'references', 'return',
'section', 'seealso', 'slot', 'source', 'template', 'templateVar',
'title', 'usage'
].map((x: string) => new vscode.CompletionItem(`${x} `));
export class HoverProvider implements vscode.HoverProvider {
async provideHover(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.Hover | null> {
if(!session.workspaceData?.globalenv){
return null;
}
if (document.languageId === 'rmd') {
const chunks = getChunks(document);
const chunk = chunks.find((chunk) => chunk.language === 'r' && chunk.startLine < position.line && chunk.endLine > position.line);
if (!chunk) {
return null;
}
}
let hoverRange = document.getWordRangeAtPosition(position);
let hoverText = null;
if (session.server) {
const exprRegex = /([a-zA-Z0-9._$@ ])+(?<![@$])/;
hoverRange = document.getWordRangeAtPosition(position, exprRegex)?.with({ end: hoverRange?.end });
const expr = document.getText(hoverRange);
const response = await session.sessionRequest(session.server, {
type: 'hover',
expr: expr
});
if (response) {
hoverText = response.str;
}
} else {
const symbol = document.getText(hoverRange);
const str = session.workspaceData.globalenv[symbol]?.str;
if (str) {
hoverText = str;
}
}
if (hoverText) {
return new vscode.Hover(`\`\`\`\n${hoverText}\n\`\`\``, hoverRange);
}
return null;
}
}
export class HelpLinkHoverProvider implements vscode.HoverProvider {
async provideHover(document: vscode.TextDocument, position: vscode.Position): Promise<vscode.Hover | null> {
if(!config().get<boolean>('helpPanel.enableHoverLinks')){
return null;
}
if (document.languageId === 'rmd') {
const chunks = getChunks(document);
const chunk = chunks.find((chunk) => chunk.language === 'r' && chunk.startLine < position.line && chunk.endLine > position.line);
if (!chunk) {
return null;
}
}
const re = /([a-zA-Z0-9._:])+/;
const wordRange = document.getWordRangeAtPosition(position, re);
const token = document.getText(wordRange);
const aliases = await globalRHelp?.getMatchingAliases(token) || [];
const mds = aliases.map(a => {
const cmdText = `${a.package}::${a.alias}`;
const args = [`/library/${a.package}/html/${a.name}.html`];
const encodedArgs = encodeURIComponent(JSON.stringify(args));
const cmd = 'command:r.helpPanel.openForPath';
const cmdUri = vscode.Uri.parse(`${cmd}?${encodedArgs}`);
return `[\`${cmdText}\`](${cmdUri})`;
});
const md = new vscode.MarkdownString(mds.join(' \n'));
md.isTrusted = true;
return new vscode.Hover(md, wordRange);
}
}
export class StaticCompletionItemProvider implements vscode.CompletionItemProvider {
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position): vscode.CompletionItem[] | undefined {
if (document.languageId === 'rmd') {
const chunks = getChunks(document);
const chunk = chunks.find((chunk) => chunk.language === 'r' && chunk.startLine < position.line && chunk.endLine > position.line);
if (!chunk) {
return undefined;
}
}
if (document.lineAt(position).text.startsWith('#\'')) {
return roxygenTagCompletionItems;
}
return undefined;
}
}
export class LiveCompletionItemProvider implements vscode.CompletionItemProvider {
async provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
completionContext: vscode.CompletionContext
): Promise<vscode.CompletionItem[]> {
const items: vscode.CompletionItem[] = [];
if (token.isCancellationRequested || !session.workspaceData?.globalenv) {
return items;
}
if (document.languageId === 'rmd') {
const chunks = getChunks(document);
const chunk = chunks.find((chunk) => chunk.language === 'r' && chunk.startLine < position.line && chunk.endLine > position.line);
if (!chunk) {
return items;
}
}
const trigger = completionContext.triggerCharacter;
if (trigger === undefined) {
Object.keys(session.workspaceData.globalenv).forEach((key) => {
const obj = session.workspaceData.globalenv[key];
const item = new vscode.CompletionItem(
key,
obj.type === 'closure' || obj.type === 'builtin'
? vscode.CompletionItemKind.Function
: vscode.CompletionItemKind.Field
);
item.detail = '[session]';
item.documentation = new vscode.MarkdownString(`\`\`\`r\n${obj.str}\n\`\`\``);
items.push(item);
});
} else if(trigger === '$' || trigger === '@') {
const symbolPosition = new vscode.Position(position.line, position.character - 1);
if (session.server) {
const re = /([a-zA-Z0-9._$@ ])+(?<![@$])/;
const exprRange = document.getWordRangeAtPosition(symbolPosition, re)?.with({ end: symbolPosition });
const expr = document.getText(exprRange);
const response: RObjectElement[] = await session.sessionRequest(session.server, {
type: 'complete',
expr: expr,
trigger: completionContext.triggerCharacter
});
if (response) {
items.push(...getCompletionItemsFromElements(response, '[session]'));
}
} else {
const symbolRange = document.getWordRangeAtPosition(symbolPosition);
const symbol = document.getText(symbolRange);
const doc = new vscode.MarkdownString('Element of `' + symbol + '`');
const obj = session.workspaceData.globalenv[symbol];
let names: string[] | undefined;
if (obj !== undefined) {
if (completionContext.triggerCharacter === '$') {
names = obj.names;
} else if (completionContext.triggerCharacter === '@') {
names = obj.slots;
}
}
if (names) {
items.push(...getCompletionItems(names, vscode.CompletionItemKind.Variable, '[session]', doc));
}
}
}
if (trigger === undefined || trigger === '[' || trigger === ',' || trigger === '"' || trigger === '\'') {
items.push(...getBracketCompletionItems(document, position, token));
}
if (trigger === undefined || trigger === '(' || trigger === ',') {
items.push(...getPipelineCompletionItems(document, position, token));
}
return items;
}
}
interface RObjectElement {
name: string;
type: string;
str: string;
}
function getCompletionItemsFromElements(elements: RObjectElement[], detail: string): vscode.CompletionItem[] {
const len = elements.length.toString().length;
let index = 0;
return elements.map((e) => {
const item = new vscode.CompletionItem(e.name, (e.type === 'closure' || e.type === 'builtin') ? CompletionItemKind.Function : vscode.CompletionItemKind.Variable);
item.detail = detail;
item.documentation = new vscode.MarkdownString(`\`\`\`r\n${e.str}\n\`\`\``);
item.sortText = `0-${index.toString().padStart(len, '0')}`;
index++;
return item;
});
}
function getCompletionItems(names: string[], kind: vscode.CompletionItemKind, detail: string, documentation: vscode.MarkdownString): vscode.CompletionItem[] {
const len = names.length.toString().length;
let index = 0;
return names.map((name) => {
const item = new vscode.CompletionItem(name, kind);
item.detail = detail;
item.documentation = documentation;
item.sortText = `0-${index.toString().padStart(len, '0')}`;
index++;
return item;
});
}
function getBracketCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.CompletionItem[] {
const items: vscode.CompletionItem[] = [];
let range: vscode.Range | undefined = new vscode.Range(new vscode.Position(position.line, 0), position);
let expectOpenBrackets = 0;
let symbol: string | undefined = undefined;
while (range) {
if (token.isCancellationRequested) { return []; }
const text = document.getText(range);
for (let i = text.length - 1; i >= 0; i -= 1) {
const chr = text.charAt(i);
if (chr === ']') {
expectOpenBrackets += 1;
} else if (chr === '[') {
if (expectOpenBrackets === 0) {
const symbolPosition = new vscode.Position(range.start.line, i - 1);
const symbolRange = document.getWordRangeAtPosition(symbolPosition);
symbol = document.getText(symbolRange);
range = undefined;
break;
} else {
expectOpenBrackets -= 1;
}
}
}
if (range?.start?.line !== undefined && range.start.line > 0) {
range = document.lineAt(range.start.line - 1).range; // check previous line
} else {
range = undefined;
}
}
if (!token.isCancellationRequested && symbol !== undefined) {
const obj = session.workspaceData.globalenv[symbol];
if (obj !== undefined && obj.names !== undefined) {
const doc = new vscode.MarkdownString('Element of `' + symbol + '`');
items.push(...getCompletionItems(obj.names, vscode.CompletionItemKind.Variable, '[session]', doc));
}
}
return items;
}
function getPipelineCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.CompletionItem[] {
const items: vscode.CompletionItem[] = [];
const range = extendSelection(position.line, (x) => document.lineAt(x).text, document.lineCount);
let symbol: string | undefined = undefined;
for (let i = range.startLine; i <= range.endLine; i++) {
if (token.isCancellationRequested) {
break;
}
const line = document.lineAt(i);
if (line.isEmptyOrWhitespace) {
continue;
}
const cleanedLine = cleanLine(line.text);
if (cleanedLine.length === 0) {
continue;
}
const pipeSymbolIndex = line.text.search(/([\w_.]+)\s*(%.+%|\|>)/);
if (pipeSymbolIndex < 0) {
break;
}
const symbolPosition = new vscode.Position(i, pipeSymbolIndex);
const symbolRange = document.getWordRangeAtPosition(symbolPosition);
if (symbolRange !== undefined) {
symbol = document.getText(symbolRange);
}
break;
}
if (!token.isCancellationRequested && symbol !== undefined) {
const obj = session.workspaceData.globalenv[symbol];
if (obj !== undefined && obj.names !== undefined) {
const doc = new vscode.MarkdownString('Element of `' + symbol + '`');
items.push(...getCompletionItems(obj.names, vscode.CompletionItemKind.Variable, '[session]', doc));
}
}
return items;
}