-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
foldingRanges.ts
198 lines (187 loc) · 6.91 KB
/
foldingRanges.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
import { TextDocument } from 'vscode-languageserver-textdocument';
import type { SourceFile } from '../sourceFile';
import type { SourceMap, TsSourceMap } from '../utils/sourceMaps';
import * as sharedLs from '../utils/sharedLs';
import { FoldingRangeKind } from 'vscode-css-languageservice';
import { FoldingRange } from 'vscode-languageserver/node';
import { createSourceFile } from '../sourceFile';
import { getDummyTsLs } from '../utils/sharedLs';
import { notEmpty } from '@volar/shared';
import type { HtmlApiRegisterOptions } from '../types';
export function register({ ts }: HtmlApiRegisterOptions) {
return (document: TextDocument) => {
let uriTsDocumentMap = new Map();
const dummyTsLs = getDummyTsLs(ts, document);
const sourceFile = createSourceFile(document, dummyTsLs, ts, undefined, uriTsDocumentMap);
const vueResult = getVueResult(sourceFile); // include html folding ranges
const tsResult = getTsResult(sourceFile);
const cssResult = getCssResult(sourceFile);
const pugResult = getPugResult(sourceFile);
return [
...vueResult,
...tsResult,
...cssResult,
...pugResult,
];
function getVueResult(sourceFile: SourceFile) {
let docTextWithoutBlocks = document.getText();
const desc = sourceFile.getDescriptor();
const blocks = [desc.script, desc.scriptSetup, ...desc.styles, ...desc.customBlocks].filter(notEmpty);
if (desc.template && desc.template.lang !== 'html') {
blocks.push(desc.template);
}
for (const block of blocks) {
const content = docTextWithoutBlocks.substring(block.loc.start, block.loc.end);
docTextWithoutBlocks = docTextWithoutBlocks.substring(0, block.loc.start)
+ content.split('\n').map(line => ' '.repeat(line.length)).join('\n')
+ docTextWithoutBlocks.substring(block.loc.end);
}
return sharedLs.htmlLs.getFoldingRanges(TextDocument.create(document.uri, document.languageId, document.version, docTextWithoutBlocks));
}
function getTsResult(sourceFile: SourceFile) {
const tsSourceMaps = [
...sourceFile.getTsSourceMaps(),
sourceFile.getTemplateScriptFormat().sourceMap,
...sourceFile.getScriptsRaw().sourceMaps,
].filter(notEmpty);
let result: FoldingRange[] = [];
for (const sourceMap of tsSourceMaps) {
if (!sourceMap.capabilities.foldingRanges)
continue;
const dummyTsLs = getDummyTsLs(ts, sourceMap.mappedDocument);
const foldingRanges = dummyTsLs.getFoldingRanges(sourceMap.mappedDocument.uri);
result = result.concat(toVueFoldingRangesTs(foldingRanges, sourceMap));
}
return result;
}
function getCssResult(sourceFile: SourceFile) {
let result: FoldingRange[] = [];
for (const sourceMap of sourceFile.getCssSourceMaps()) {
if (!sourceMap.capabilities.foldingRanges) continue;
const cssLs = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLs) continue;
const foldingRanges = cssLs.getFoldingRanges(sourceMap.mappedDocument);
result = result.concat(toVueFoldingRanges(foldingRanges, sourceMap));
}
return result;
}
function getPugResult(sourceFile: SourceFile) {
let result: FoldingRange[] = [];
for (const sourceMap of sourceFile.getPugSourceMaps()) {
const text = sourceMap.mappedDocument.getText();
const lines = text.split('\n');
const lineOffsets = getLineOffsets(lines);
const lineIndents = getLineIndents(lines);
const foldingRanges: FoldingRange[] = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const offset = lineOffsets[i];
const indent = lineIndents[i];
if (indent === undefined) continue;
const startPos = sourceMap.mappedDocument.positionAt(offset);
const kind = getFoldingRangeKind(line);
let found = false;
for (let j = i + 1; j < lines.length; j++) {
const offset_2 = lineOffsets[j];
const indent_2 = lineIndents[j];
if (indent_2 === undefined) continue;
if (indent_2.length <= indent.length) {
const endPos = sourceMap.mappedDocument.positionAt(offset_2);
const foldingRange = FoldingRange.create(
startPos.line,
endPos.line - 1,
undefined,
undefined,
kind,
);
foldingRanges.push(foldingRange);
found = true;
break;
}
}
if (!found) {
const offset_2 = text.length;
const endPos = sourceMap.mappedDocument.positionAt(offset_2);
const foldingRange = FoldingRange.create(
startPos.line,
endPos.line - 1,
undefined,
undefined,
kind,
);
foldingRanges.push(foldingRange);
}
}
result = result.concat(toVueFoldingRanges(foldingRanges, sourceMap));
}
return result;
function getLineOffsets(lines: string[]) {
const offsets: number[] = [];
let currentOffset = 0;
for (const line of lines) {
offsets.push(currentOffset);
currentOffset += line.length + 1;
}
return offsets;
}
function getLineIndents(lines: string[]) {
const indents: (string | undefined)[] = [];
for (const line of lines) {
const line2 = line.trimStart();
if (line2 === '') {
indents.push(undefined);
}
else {
const offset = line.length - line2.length;
const indent = line.substr(0, offset);
indents.push(indent);
}
}
return indents;
}
function getFoldingRangeKind(line: string) {
if (line.trimStart().startsWith('//')) {
return FoldingRangeKind.Comment;
}
}
}
}
}
function toVueFoldingRanges(virtualFoldingRanges: FoldingRange[], sourceMap: SourceMap) {
const result: FoldingRange[] = [];
for (const foldingRange of virtualFoldingRanges) {
const vueRange = sourceMap.getSourceRange(
{ line: foldingRange.startLine, character: foldingRange.startCharacter ?? 0 },
{ line: foldingRange.endLine, character: foldingRange.endCharacter ?? 0 },
);
if (vueRange) {
foldingRange.startLine = vueRange.start.line;
foldingRange.endLine = vueRange.end.line;
if (foldingRange.startCharacter !== undefined)
foldingRange.startCharacter = vueRange.start.character;
if (foldingRange.endCharacter !== undefined)
foldingRange.endCharacter = vueRange.end.character;
result.push(foldingRange);
}
}
return result;
}
function toVueFoldingRangesTs(virtualFoldingRanges: FoldingRange[], sourceMap: TsSourceMap) {
const result: FoldingRange[] = [];
for (const foldingRange of virtualFoldingRanges) {
const vueLoc = sourceMap.getSourceRange(
{ line: foldingRange.startLine, character: foldingRange.startCharacter ?? 0 },
{ line: foldingRange.endLine, character: foldingRange.endCharacter ?? 0 },
);
if (vueLoc && vueLoc.data.capabilities.foldingRanges) {
foldingRange.startLine = vueLoc.start.line;
foldingRange.endLine = vueLoc.end.line;
if (foldingRange.startCharacter !== undefined)
foldingRange.startCharacter = vueLoc.start.character;
if (foldingRange.endCharacter !== undefined)
foldingRange.endCharacter = vueLoc.end.character;
result.push(foldingRange);
}
}
return result;
}