-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
format.ts
215 lines (155 loc) · 5.33 KB
/
format.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
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as vscode from 'vscode-languageserver-protocol';
import type { EmbeddedStructure } from '@volar/vue-typescript';
import type { DocumentServiceRuntimeContext } from '../types';
import { EmbeddedDocumentSourceMap, VueDocument } from '../vueDocuments';
export function register(context: DocumentServiceRuntimeContext) {
return async (document: TextDocument, options: vscode.FormattingOptions) => {
const originalDocument = document;
const rootEdits = await tryFormat(document);
const vueDocument = context.getVueDocument(document);
if (!vueDocument)
return rootEdits;
if (rootEdits?.length) {
applyEdits(rootEdits);
}
let level = 0;
while (true) {
tryUpdateVueDocument();
const embeddeds = getEmbeddedsByLevel(vueDocument, level++);
if (embeddeds.length === 0)
break;
let edits: vscode.TextEdit[] = [];
let toPatchIndent: {
lsType: string,
sourceMapEmbeddedDocumentUri: string,
} | undefined;
for (const embedded of embeddeds) {
if (!embedded.self?.file.capabilities.formatting)
continue;
const sourceMap = vueDocument.sourceMapsMap.get(embedded.self);
if (embedded.inheritParentIndent)
toPatchIndent = {
lsType: sourceMap.embeddedFile.lsType,
sourceMapEmbeddedDocumentUri: sourceMap.mappedDocument.uri,
};
const _edits = await tryFormat(sourceMap.mappedDocument);
if (!_edits)
continue;
for (const textEdit of _edits) {
for (const [range] of sourceMap.getSourceRanges(
textEdit.range.start,
textEdit.range.end,
)) {
edits.push({
newText: textEdit.newText,
range,
});
}
}
}
if (edits.length > 0) {
applyEdits(edits);
}
if (toPatchIndent) {
tryUpdateVueDocument();
const sourceMap = vueDocument.getSourceMaps().find(sourceMap => sourceMap.embeddedFile.lsType === toPatchIndent?.lsType && sourceMap.mappedDocument.uri === toPatchIndent.sourceMapEmbeddedDocumentUri);
if (sourceMap) {
const indentEdits = patchInterpolationIndent(vueDocument, sourceMap);
if (indentEdits.length > 0) {
applyEdits(indentEdits);
}
}
}
}
if (document.getText() === originalDocument.getText())
return;
const editRange = vscode.Range.create(
originalDocument.positionAt(0),
originalDocument.positionAt(originalDocument.getText().length),
);
const textEdit = vscode.TextEdit.replace(editRange, document.getText());
return [textEdit];
function tryUpdateVueDocument() {
if (vueDocument?.getDocument().getText() !== document.getText()) {
vueDocument?.file.update(document.getText(), document.version.toString());
}
}
function getEmbeddedsByLevel(vueDocument: VueDocument, level: number) {
const embeddeds = vueDocument.file.getEmbeddeds();
const embeddedsLevels: EmbeddedStructure[][] = [embeddeds];
while (true) {
if (embeddedsLevels.length > level)
return embeddedsLevels[level];
let nextEmbeddeds: EmbeddedStructure[] = [];
for (const embeddeds of embeddedsLevels[embeddedsLevels.length - 1]) {
nextEmbeddeds = nextEmbeddeds.concat(embeddeds.embeddeds);
}
embeddedsLevels.push(nextEmbeddeds);
}
}
async function tryFormat(document: TextDocument) {
const plugins = context.getFormatPlugins();
context.updateTsLs(document);
for (const plugin of plugins) {
if (!plugin.format)
continue;
let edits: vscode.TextEdit[] | null | undefined;
try {
edits = await plugin.format(document, undefined, options);
}
catch (err) {
console.error(err);
}
if (!edits)
continue;
return edits;
}
}
function applyEdits(textEdits: vscode.TextEdit[]) {
const newText = TextDocument.applyEdits(document, textEdits);
if (newText !== document.getText()) {
document = TextDocument.create(document.uri, document.languageId, document.version + 1, newText);
}
}
};
}
function patchInterpolationIndent(vueDocument: VueDocument, sourceMap: EmbeddedDocumentSourceMap) {
const indentTextEdits: vscode.TextEdit[] = [];
const document = vueDocument.getDocument();
for (const mapped of sourceMap.mappings) {
const textRange = {
start: document.positionAt(mapped.sourceRange.start),
end: document.positionAt(mapped.sourceRange.end),
};
const text = document.getText(textRange);
if (text.indexOf('\n') === -1)
continue;
const lines = text.split('\n');
const removeIndent = getRemoveIndent(lines);
const baseIndent = getBaseIndent(mapped.sourceRange.start);
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith(removeIndent)) {
lines[i] = line.replace(removeIndent, baseIndent);
}
else {
lines[i] = baseIndent.replace(removeIndent, '') + line;
}
}
indentTextEdits.push({
newText: lines.join('\n'),
range: textRange,
});
}
return indentTextEdits;
function getRemoveIndent(lines: string[]) {
const lastLine = lines[lines.length - 1];
return lastLine.substring(0, lastLine.length - lastLine.trimStart().length);
}
function getBaseIndent(pos: number) {
const startPos = document.positionAt(pos);
const startLineText = document.getText({ start: startPos, end: { line: startPos.line, character: 0 } });
return startLineText.substring(0, startLineText.length - startLineText.trimStart().length);
}
}