-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcodeview.ts
More file actions
302 lines (282 loc) · 9.19 KB
/
codeview.ts
File metadata and controls
302 lines (282 loc) · 9.19 KB
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
/*
* codeview.ts
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import {
CompletionItem as VCompletionItem,
CompletionItemKind as VCompletionItemKind,
MarkdownString,
SnippetString,
Range,
Position,
TextDocument,
commands
} from "vscode";
import {
CompletionItem,
CompletionItemKind,
CompletionItemLabelDetails,
CompletionList,
InsertTextFormat,
MarkupContent,
MarkupKind
} from "vscode-languageserver-types";
import { JsonRpcRequestTransport } from "core";
import {
CodeViewCellContext,
CodeViewCompletionContext,
CodeViewExecute,
CodeViewServer,
DiagramState,
kCodeViewGetCompletions,
kCodeViewGetDiagnostics,
LintItem
} from "editor-types";
import { hasHooks } from "../../host/hooks";
import { embeddedLanguage } from "../../vdoc/languages";
import { virtualDocForCode } from "../../vdoc/vdoc";
import { vdocCompletions } from "../../vdoc/vdoc-completion";
import { MarkdownEngine } from "../../markdown/engine";
export function vscodeCodeViewServer(_engine: MarkdownEngine, document: TextDocument, lspRequest: JsonRpcRequestTransport): CodeViewServer {
return {
async codeViewAssist(context: CodeViewCellContext) {
await commands.executeCommand("quarto.codeViewAssist", context, lspRequest);
},
async codeViewExecute(execute: CodeViewExecute) {
switch (execute) {
case "cell":
await commands.executeCommand("quarto.runCurrentCell");
break;
case "cell+advance":
await commands.executeCommand("quarto.runCurrentAdvance");
break;
case "above":
await commands.executeCommand("quarto.runCellsAbove");
break;
case "below":
await commands.executeCommand("quarto.runCellsBelow");
break;
}
},
async codeViewDiagnostics(context: CodeViewCellContext): Promise<LintItem[] | undefined> {
// if this is yaml then call the lsp directly
if (context.language === "yaml") {
return lspRequest(kCodeViewGetDiagnostics, [context]);
}
},
async codeViewCompletions(context: CodeViewCompletionContext): Promise<CompletionList> {
// if this is yaml then call the lsp directly
if (context.language === "yaml") {
return lspRequest(kCodeViewGetCompletions, [context]);
}
// see if we have an embedded langaage
const language = embeddedLanguage(context.language);
if (!language) {
return {
items: [],
isIncomplete: false
};
}
// if this is a yaml comment line then call the lsp
const line = context.code[context.selection.start.line];
if (language.comment && line.startsWith(`${language.comment}| `)) {
return lspCellYamlOptionsCompletions(context, lspRequest);
}
// if this is Positron, no visual editor completions
// TODO: fix LSP issues for visual editor in Positron:
// https://github.com/posit-dev/positron/issues/1805
// if (hasHooks()) {
// return {
// items: [],
// isIncomplete: false
// };
// }
// otherwise delegate to vscode completion system
const vdoc = virtualDocForCode(context.code, language);
const completions = await vdocCompletions(
vdoc,
new Position(
context.selection.start.line,
context.selection.start.character
),
undefined,
language,
document.uri
);
return {
items: completions.map(vsCompletionItemToLsCompletionItem),
isIncomplete: false
};
},
async codeViewPreviewDiagram(state: DiagramState, activate: boolean) {
commands.executeCommand("quarto.previewDiagram", { state, activate });
}
};
}
function lspCellYamlOptionsCompletions(context: CodeViewCompletionContext, lspRequest: JsonRpcRequestTransport) {
// strip out lines that aren't in the code block
const code = context.code.map((codeLine, index) => {
if (index < context.cellBegin || index > context.cellEnd) {
return "";
} else {
return codeLine;
}
});
// include language header (we offset cellEnd below accordingly)
code.splice(context.cellBegin, 0, `{${context.language}}`);
// make request
return lspRequest(kCodeViewGetCompletions, [{
...context,
code,
cellEnd: context.cellEnd + 1,
selection: {
start: {
...context.selection.start,
line: context.selection.start.line + 1
},
end: {
...context.selection.end,
line: context.selection.end.line + 1
}
}
}]);
}
export function vsCompletionItemToLsCompletionItem(item: VCompletionItem): CompletionItem {
const insertText = item.insertText instanceof SnippetString
? item.insertText.value
: item.insertText || "";
const completion: CompletionItem = {
...labelWithDetails(item),
kind: vsKindToLsKind(item.kind),
detail: item.detail,
documentation: item.documentation instanceof MarkdownString
? mdStringToMdContent(item.documentation)
: item.documentation,
sortText: item.sortText && /^\d/.test(item.sortText) ? item.sortText : undefined,
filterText: item.filterText,
insertText,
insertTextFormat: item.insertText instanceof SnippetString
? InsertTextFormat.Snippet
: InsertTextFormat.PlainText,
command: item.command
};
if (item.range) {
const isRange = (x?: unknown): x is Range => { return !!x && !!(x as Record<string, unknown>).start; };
if (isRange(item.range)) {
completion.textEdit = {
newText: insertText,
range: {
start: item.range.start,
end: item.range.end
}
};
} else {
completion.textEdit = {
newText: insertText,
insert: {
start: item.range.inserting.start,
end: item.range.replacing.end
},
replace: {
start: item.range.replacing.start,
end: item.range.replacing.end
}
};
}
}
return completion;
}
export function labelWithDetails(item: VCompletionItem): { label: string, labelWithDetails: CompletionItemLabelDetails; } {
if (typeof (item.label) === "string") {
return {
label: item.label,
labelWithDetails: {
detail: item.detail
}
};
} else {
return {
label: item.label.label,
labelWithDetails: {
detail: item.label.detail
}
};
}
}
export function vsKindToLsKind(kind?: VCompletionItemKind): CompletionItemKind | undefined {
if (kind === undefined) {
return undefined;
}
switch (kind) {
case VCompletionItemKind.Text:
return CompletionItemKind.Text;
case VCompletionItemKind.Method:
return CompletionItemKind.Method;
case VCompletionItemKind.Function:
return CompletionItemKind.Function;
case VCompletionItemKind.Constructor:
return CompletionItemKind.Constructor;
case VCompletionItemKind.Field:
return CompletionItemKind.Field;
case VCompletionItemKind.Variable:
return CompletionItemKind.Variable;
case VCompletionItemKind.Class:
return CompletionItemKind.Class;
case VCompletionItemKind.Interface:
return CompletionItemKind.Interface;
case VCompletionItemKind.Module:
return CompletionItemKind.Module;
case VCompletionItemKind.Property:
return CompletionItemKind.Property;
case VCompletionItemKind.Unit:
return CompletionItemKind.Unit;
case VCompletionItemKind.Value:
return CompletionItemKind.Value;
case VCompletionItemKind.Enum:
return CompletionItemKind.Enum;
case VCompletionItemKind.Keyword:
return CompletionItemKind.Keyword;
case VCompletionItemKind.Snippet:
return CompletionItemKind.Snippet;
case VCompletionItemKind.Color:
return CompletionItemKind.Color;
case VCompletionItemKind.Reference:
return CompletionItemKind.Reference;
case VCompletionItemKind.File:
return CompletionItemKind.File;
case VCompletionItemKind.Folder:
return CompletionItemKind.Folder;
case VCompletionItemKind.EnumMember:
return CompletionItemKind.EnumMember;
case VCompletionItemKind.Constant:
return CompletionItemKind.Constant;
case VCompletionItemKind.Struct:
return CompletionItemKind.Struct;
case VCompletionItemKind.Event:
return CompletionItemKind.Event;
case VCompletionItemKind.Operator:
return CompletionItemKind.Operator;
case VCompletionItemKind.TypeParameter:
return CompletionItemKind.TypeParameter;
case VCompletionItemKind.User:
case VCompletionItemKind.Issue:
default:
return CompletionItemKind.Text;
}
}
function mdStringToMdContent(mdString: MarkdownString): MarkupContent {
return {
kind: MarkupKind.Markdown,
value: mdString.value
};
}