-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
documentSymbol.ts
152 lines (142 loc) · 4.51 KB
/
documentSymbol.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
import { notEmpty } from '@volar/shared';
import {
Location,
Range,
SymbolInformation,
SymbolKind
} from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as sharedLs from '../utils/sharedLs';
export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOptions) {
return (uri: string) => {
const sourceFile = sourceFiles.get(uri);
if (!sourceFile) return;
const document = sourceFile.getTextDocument();
const vueResult = getVueResult(sourceFile);
const tsResult = getTsResult(sourceFile);
const htmlResult = getHtmlResult(sourceFile);
const cssResult = getCssResult(sourceFile);
// TODO: pug
return [
...vueResult,
...tsResult,
...htmlResult,
...cssResult,
];
function getVueResult(sourceFile: SourceFile) {
const result: SymbolInformation[] = [];
const desc = sourceFile.getDescriptor();
if (desc.template) {
result.push({
name: '<template>',
kind: SymbolKind.Module,
location: Location.create(uri, Range.create(
document.positionAt(desc.template.loc.start),
document.positionAt(desc.template.loc.end),
)),
});
}
if (desc.script) {
result.push({
name: '<script>',
kind: SymbolKind.Module,
location: Location.create(uri, Range.create(
document.positionAt(desc.script.loc.start),
document.positionAt(desc.script.loc.end),
)),
});
}
if (desc.scriptSetup) {
result.push({
name: '<script setup>',
kind: SymbolKind.Module,
location: Location.create(uri, Range.create(
document.positionAt(desc.scriptSetup.loc.start),
document.positionAt(desc.scriptSetup.loc.end),
)),
});
}
for (const style of desc.styles) {
result.push({
name: `<${['style', style.scoped ? 'scoped' : undefined, style.module ? 'module' : undefined].filter(notEmpty).join(' ')}>`,
kind: SymbolKind.Module,
location: Location.create(uri, Range.create(
document.positionAt(style.loc.start),
document.positionAt(style.loc.end),
)),
});
}
for (const customBlock of desc.customBlocks) {
result.push({
name: `<${customBlock.type}>`,
kind: SymbolKind.Module,
location: Location.create(uri, Range.create(
document.positionAt(customBlock.loc.start),
document.positionAt(customBlock.loc.end),
)),
});
}
return result;
}
function getTsResult(sourceFile: SourceFile) {
const result: SymbolInformation[] = [];
const map = new Map<string, SymbolInformation>();
for (const sourceMap of sourceFile.getTsSourceMaps()) {
if (!sourceMap.capabilities.documentSymbol) continue;
let symbols = tsLanguageService.findWorkspaceSymbols(sourceMap.mappedDocument.uri);
for (const s of symbols) {
const vueRange = sourceMap.getSourceRange(s.location.range.start, s.location.range.end);
if (vueRange) {
map.set(`${sourceMap.mappedDocument.offsetAt(s.location.range.start)}:${sourceMap.mappedDocument.offsetAt(s.location.range.end)}:${s.kind}:${s.name}`, {
...s,
location: Location.create(uri, vueRange),
});
}
}
}
for (const info of map.values()) {
result.push(info);
}
return result;
}
function getHtmlResult(sourceFile: SourceFile) {
const result: SymbolInformation[] = [];
const sourceMaps = sourceFile.getHtmlSourceMaps();
for (const sourceMap of sourceMaps) {
let symbols = sharedLs.htmlLs.findDocumentSymbols(sourceMap.mappedDocument, sourceMap.htmlDocument);
if (!symbols) continue;
for (const s of symbols) {
const vueRange = sourceMap.getSourceRange(s.location.range.start, s.location.range.end);
if (vueRange) {
result.push({
...s,
location: Location.create(uri, vueRange),
});
}
}
}
return result;
}
function getCssResult(sourceFile: SourceFile) {
const result: SymbolInformation[] = [];
const sourceMaps = sourceFile.getCssSourceMaps();
for (const sourceMap of sourceMaps) {
const cssLs = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLs || !sourceMap.stylesheet) continue;
let symbols = cssLs.findDocumentSymbols(sourceMap.mappedDocument, sourceMap.stylesheet);
if (!symbols) continue;
for (const s of symbols) {
const vueRange = sourceMap.getSourceRange(s.location.range.start, s.location.range.end);
if (vueRange) {
result.push({
...s,
location: Location.create(uri, vueRange),
});
}
}
}
return result;
}
}
}