blob: e4cf048205aa53c26a944ed9c5aa834973a40d18 [file] [log] [blame]
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Location, Range, SymbolKind } from 'vscode-languageserver-types';
export function findDocumentSymbols(document, htmlDocument) {
var symbols = [];
htmlDocument.roots.forEach(function (node) {
provideFileSymbolsInternal(document, node, '', symbols);
});
return symbols;
}
function provideFileSymbolsInternal(document, node, container, symbols) {
var name = nodeToName(node);
var location = Location.create(document.uri, Range.create(document.positionAt(node.start), document.positionAt(node.end)));
var symbol = {
name: name,
location: location,
containerName: container,
kind: SymbolKind.Field
};
symbols.push(symbol);
node.children.forEach(function (child) {
provideFileSymbolsInternal(document, child, name, symbols);
});
}
function nodeToName(node) {
var name = node.tag;
if (node.attributes) {
var id = node.attributes['id'];
var classes = node.attributes['class'];
if (id) {
name += "#" + id.replace(/[\"\']/g, '');
}
if (classes) {
name += classes.replace(/[\"\']/g, '').split(/\s+/).map(function (className) { return "." + className; }).join('');
}
}
return name || '?';
}