forked from quarto-dev/quarto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
301 lines (265 loc) · 8.81 KB
/
format.ts
File metadata and controls
301 lines (265 loc) · 8.81 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
/*
* format.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 {
commands,
FormattingOptions,
Position,
Range,
TextDocument,
TextEdit,
window,
workspace,
CancellationToken,
Uri,
} from "vscode";
import {
ProvideDocumentFormattingEditsSignature,
ProvideDocumentRangeFormattingEditsSignature,
} from "vscode-languageclient/node";
import { lines } from "core";
import { TokenCodeBlock, TokenMath, codeForExecutableLanguageBlock, languageBlockAtLine } from "quarto-core";
import { Command } from "../core/command";
import { isQuartoDoc } from "../core/doc";
import { MarkdownEngine } from "../markdown/engine";
import { EmbeddedLanguage, languageCanFormatDocument } from "../vdoc/languages";
import {
languageFromBlock,
mainLanguage,
unadjustedRange,
VirtualDoc,
virtualDocForCode,
virtualDocForLanguage,
withVirtualDocUri,
} from "../vdoc/vdoc";
export function activateCodeFormatting(engine: MarkdownEngine) {
return [new FormatCellCommand(engine)];
}
export function embeddedDocumentFormattingProvider(engine: MarkdownEngine) {
return async (
document: TextDocument,
options: FormattingOptions,
token: CancellationToken,
next: ProvideDocumentFormattingEditsSignature
): Promise<TextEdit[] | null | undefined> => {
if (!isQuartoDoc(document, true)) {
// Delegate if we don't handle it
return next(document, options, token);
}
// Ensure we are dealing w/ the active document
const activeEditor = window.activeTextEditor;
if (!activeEditor) {
// Ensure that other formatters don't ever run over qmd files
return [];
}
if (activeEditor.document.uri.toString() !== document.uri.toString()) {
return [];
}
const tokens = engine.parse(document);
// Figure out language to use. Try selection's block, then fall back to main doc language.
const includeFence = false;
const line = activeEditor.selection.active.line;
const block = languageBlockAtLine(tokens, line, includeFence);
let language = block ? languageFromBlock(block) : undefined;
if (!language || !language.canFormat) {
language = mainLanguage(tokens, (lang) => !!lang.canFormat);
}
if (!language) {
// No language that can format in any way
return [];
}
if (languageCanFormatDocument(language)) {
// Full document formatting support
const vdoc = virtualDocForLanguage(document, tokens, language);
return executeFormatDocumentProvider(
vdoc,
document,
formattingOptions(document.uri, vdoc.language, options)
);
} else if (block) {
// Just format the selected block if there is one
const edits = await formatBlock(document, block);
return edits ? edits : [];
} else {
// Nothing we can format
return [];
}
};
}
export function embeddedDocumentRangeFormattingProvider(
engine: MarkdownEngine
) {
return async (
document: TextDocument,
range: Range,
options: FormattingOptions,
token: CancellationToken,
next: ProvideDocumentRangeFormattingEditsSignature
): Promise<TextEdit[] | null | undefined> => {
if (!isQuartoDoc(document, true)) {
// If we don't perform any formatting, then call the next handler
return next(document, range, options, token);
}
const includeFence = false;
const tokens = engine.parse(document);
const block = languageBlockAtLine(tokens, range.start.line, includeFence);
if (!block) {
// Don't let anyone else format qmd files
return [];
}
const endBlock = languageBlockAtLine(tokens, range.end.line, includeFence);
if (!endBlock) {
// Selection extends outside of a single block and into ambiguous non-block editor space
// (possibly spanning multiple blocks in the process)
return [];
}
if (block.range.start.line !== endBlock.range.start.line) {
// Selection spans multiple blocks
return [];
}
const edits = await formatBlock(document, block);
if (!edits) {
return [];
}
return edits;
};
}
class FormatCellCommand implements Command {
public readonly id = "quarto.formatCell";
constructor(private readonly engine_: MarkdownEngine) { }
public async execute(): Promise<void> {
const editor = window.activeTextEditor;
if (!editor) {
// No active text editor
return;
}
const document = editor.document;
if (!isQuartoDoc(document)) {
window.showInformationMessage("Active editor is not a Quarto document");
return;
}
const includeFence = false;
const tokens = this.engine_.parse(document);
const block = languageBlockAtLine(tokens, editor.selection.start.line, includeFence);
if (!block) {
window.showInformationMessage("Editor selection is not within a code cell.");
return;
}
const edits = await formatBlock(document, block);
if (!edits) {
// Nothing to do! Already formatted, or no formatter picked us up, or this language doesn't support formatting.
return;
}
editor.edit((editBuilder) => {
// Sort edits by descending start position to avoid range shifting issues
edits
.slice()
.sort((a, b) => b.range.start.compareTo(a.range.start))
.forEach((edit) => {
editBuilder.replace(edit.range, edit.newText);
});
});
}
}
function formattingOptions(
uri: Uri,
language: EmbeddedLanguage,
defaultOptions?: FormattingOptions
): FormattingOptions {
const config = workspace.getConfiguration(undefined, {
uri: uri,
languageId: language.ids[0],
});
return {
tabSize: config.get<number>("editor.tabSize", defaultOptions?.tabSize ?? 4),
insertSpaces: config.get<boolean>(
"editor.insertSpaces",
defaultOptions?.insertSpaces ?? true
),
};
}
async function executeFormatDocumentProvider(
vdoc: VirtualDoc,
document: TextDocument,
options: FormattingOptions
): Promise<TextEdit[] | undefined> {
const edits = await withVirtualDocUri(vdoc, document.uri, "format", async (uri: Uri) => {
return await commands.executeCommand<TextEdit[] | undefined>(
"vscode.executeFormatDocumentProvider",
uri,
options
);
});
if (edits) {
return unadjustedEdits(edits, vdoc.language);
} else {
return undefined;
}
}
async function formatBlock(doc: TextDocument, block: TokenMath | TokenCodeBlock): Promise<TextEdit[] | undefined> {
// Extract language
const language = languageFromBlock(block);
if (!language) {
return undefined;
}
// Refuse to format if not supported by this language
if (!language.canFormat) {
return undefined;
}
// Create virtual document containing the block
const blockLines = lines(codeForExecutableLanguageBlock(block, false));
const vdoc = virtualDocForCode(blockLines, language);
const edits = await executeFormatDocumentProvider(
vdoc,
doc,
formattingOptions(doc.uri, vdoc.language)
);
if (!edits) {
// Either no formatter picked us up, or there were no edits required.
// We can't determine the difference though!
return undefined;
}
// Because we format with the block code copied in an empty virtual
// document, we need to adjust the ranges to match the edits to the block
// cell in the original file.
const blockRange = new Range(
new Position(block.range.start.line, block.range.start.character),
new Position(block.range.end.line, block.range.end.character)
);
const adjustedEdits = edits
.map(edit => {
const range = new Range(
new Position(edit.range.start.line + block.range.start.line + 1, edit.range.start.character),
new Position(edit.range.end.line + block.range.start.line + 1, edit.range.end.character)
);
return new TextEdit(range, edit.newText);
});
// Bail if any edit is out of range. We used to filter these edits out but
// this could bork the cell. Return `[]` to indicate that we tried.
if (adjustedEdits.some(edit => !blockRange.contains(edit.range))) {
window.showInformationMessage(
"Formatting edits were out of range and could not be applied to the code cell."
);
return [];
}
return adjustedEdits;
}
function unadjustedEdits(
edits: TextEdit[],
language: EmbeddedLanguage
): TextEdit[] {
return edits.map((edit) => {
return new TextEdit(unadjustedRange(language, edit.range), edit.newText);
});
}