-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
Copy pathhtmlSymbolsProvider.ts
64 lines (52 loc) · 1.46 KB
/
htmlSymbolsProvider.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
import { TextDocument, Location, Range, SymbolInformation, SymbolKind } from 'vscode-languageserver-types';
import { HTMLDocument, Node } from '../parser/htmlParser';
export function findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): SymbolInformation[] {
const symbols = <SymbolInformation[]>[];
htmlDocument.roots.forEach(node => {
provideFileSymbolsInternal(document, node, '', symbols);
});
return symbols;
}
function provideFileSymbolsInternal(
document: TextDocument,
node: Node,
container: string,
symbols: SymbolInformation[]
): void {
const name = nodeToName(node);
const location = Location.create(
document.uri,
Range.create(document.positionAt(node.start), document.positionAt(node.end))
);
const symbol: SymbolInformation = {
name,
location,
containerName: container,
kind: <SymbolKind>SymbolKind.Field
};
symbols.push(symbol);
node.children.forEach(child => {
provideFileSymbolsInternal(document, child, name, symbols);
});
}
function nodeToName(node: Node): string {
let name = node.tag;
if(!name) {
return '';
}
if (node.attributes) {
const id = node.attributes['id'];
const classes = node.attributes['class'];
if (id) {
name += `#${id.replace(/[\"\']/g, '')}`;
}
if (classes) {
name += classes
.replace(/[\"\']/g, '')
.split(/\s+/)
.map(className => `.${className}`)
.join('');
}
}
return name;
}