Skip to content

Commit

Permalink
refactor(html): use the new findDocumentSymbols2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 6, 2023
1 parent e3b0bff commit e99c91b
Showing 1 changed file with 2 additions and 48 deletions.
50 changes: 2 additions & 48 deletions packages/html/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Service, DocumentSymbol, SymbolKind } from '@volar/language-service';
import type { Service } from '@volar/language-service';
import * as html from 'vscode-html-languageservice';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as path from 'path';
Expand Down Expand Up @@ -147,12 +147,7 @@ export function create({

provideDocumentSymbols(document) {
return worker(document, (htmlDocument) => {
// TODO: wait for https://github.com/microsoft/vscode-html-languageservice/pull/152
const symbols: DocumentSymbol[] = [];
htmlDocument.roots.forEach(node => {
provideFileSymbolsInternal(document, node, symbols);
});
return symbols;
return htmlLs.findDocumentSymbols2(document, htmlDocument);
});
},

Expand Down Expand Up @@ -358,44 +353,3 @@ const NL = '\n'.charCodeAt(0);
function isNewlineCharacter(charCode: number) {
return charCode === CR || charCode === NL;
}

function provideFileSymbolsInternal(document: TextDocument, node: html.Node, symbols: DocumentSymbol[]): void {

const name = nodeToName(node);
const range = {
start: document.positionAt(node.start),
end: document.positionAt(node.end),
};
const symbol: DocumentSymbol = {
name,
kind: 8 satisfies typeof SymbolKind.Field,
range,
selectionRange: range,
};

symbols.push(symbol);

node.children.forEach(child => {
symbol.children ??= [];
provideFileSymbolsInternal(document, child, symbol.children);
});
}

function nodeToName(node: html.Node): string {
let name = node.tag;

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 || '?';
}

0 comments on commit e99c91b

Please sign in to comment.