Skip to content

Commit

Permalink
refactor: SymbolInformation -> DocumentSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 22, 2023
1 parent 9adbf2a commit 3733e09
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/htmlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { findLinkedEditingRanges } from './services/htmlLinkedEditing';
import {
Scanner, HTMLDocument, CompletionConfiguration, ICompletionParticipant, HTMLFormatConfiguration, DocumentContext,
IHTMLDataProvider, HTMLDataV1, LanguageServiceOptions, TextDocument, SelectionRange, WorkspaceEdit,
Position, CompletionList, Hover, Range, SymbolInformation, TextEdit, DocumentHighlight, DocumentLink, FoldingRange, HoverSettings
Position, CompletionList, Hover, Range, DocumentSymbol, TextEdit, DocumentHighlight, DocumentLink, FoldingRange, HoverSettings
} from './htmlLanguageTypes';
import { HTMLFolding } from './services/htmlFolding';
import { HTMLSelectionRange } from './services/htmlSelectionRange';
Expand All @@ -38,7 +38,7 @@ export interface LanguageService {
doHover(document: TextDocument, position: Position, htmlDocument: HTMLDocument, options?: HoverSettings): Hover | null;
format(document: TextDocument, range: Range | undefined, options: HTMLFormatConfiguration): TextEdit[];
findDocumentLinks(document: TextDocument, documentContext: DocumentContext): DocumentLink[];
findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): SymbolInformation[];
findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): DocumentSymbol[];
doQuoteComplete(document: TextDocument, position: Position, htmlDocument: HTMLDocument, options?: CompletionConfiguration): string | null;
doTagComplete(document: TextDocument, position: Position, htmlDocument: HTMLDocument): string | null;
getFoldingRanges(document: TextDocument, context?: { rangeLimit?: number }): FoldingRange[];
Expand Down
4 changes: 2 additions & 2 deletions src/htmlLanguageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MarkupContent, MarkupKind, MarkedString, DocumentUri,
SelectionRange, WorkspaceEdit,
CompletionList, CompletionItemKind, CompletionItem, CompletionItemTag, InsertTextMode, Command,
SymbolInformation, SymbolKind,
DocumentSymbol, SymbolKind,
Hover, TextEdit, InsertReplaceEdit, InsertTextFormat, DocumentHighlight, DocumentHighlightKind,
DocumentLink, FoldingRange, FoldingRangeKind,
SignatureHelp, Definition, Diagnostic, FormattingOptions, Color, ColorInformation, ColorPresentation
Expand All @@ -22,7 +22,7 @@ export {
MarkupContent, MarkupKind, MarkedString, DocumentUri,
SelectionRange, WorkspaceEdit,
CompletionList, CompletionItemKind, CompletionItem, CompletionItemTag, InsertTextMode, Command,
SymbolInformation, SymbolKind,
DocumentSymbol, SymbolKind,
Hover, TextEdit, InsertReplaceEdit, InsertTextFormat, DocumentHighlight, DocumentHighlightKind,
DocumentLink, FoldingRange, FoldingRangeKind,
SignatureHelp, Definition, Diagnostic, FormattingOptions, Color, ColorInformation, ColorPresentation
Expand Down
28 changes: 15 additions & 13 deletions src/services/htmlSymbolsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Location, Range, SymbolInformation, SymbolKind, TextDocument} from '../htmlLanguageTypes';
import { Range, DocumentSymbol, SymbolKind, TextDocument} from '../htmlLanguageTypes';
import { HTMLDocument, Node } from '../parser/htmlParser';

export function findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): SymbolInformation[] {
const symbols = <SymbolInformation[]>[];
export function findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): DocumentSymbol[] {
const symbols: DocumentSymbol[] = [];

htmlDocument.roots.forEach(node => {
provideFileSymbolsInternal(document, node, '', symbols);
provideFileSymbolsInternal(document, node, symbols);
});

return symbols;
}

function provideFileSymbolsInternal(document: TextDocument, node: Node, container: string, symbols: SymbolInformation[]): void {
function provideFileSymbolsInternal(document: TextDocument, node: Node, symbols: DocumentSymbol[]): 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: name,
location: location,
containerName: container,
kind: <SymbolKind>SymbolKind.Field
};
const range = Range.create(document.positionAt(node.start), document.positionAt(node.end));
const symbol = DocumentSymbol.create(
name,
undefined,
SymbolKind.Field,
range,
range,
);

symbols.push(symbol);

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

Expand Down
50 changes: 28 additions & 22 deletions src/test/symbols.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
import * as assert from 'assert';
import * as htmlLanguageService from '../htmlLanguageService';

import { SymbolInformation, SymbolKind, Location, Range, TextDocument } from '../htmlLanguageService';
import { DocumentSymbol, SymbolKind, Range, TextDocument } from '../htmlLanguageService';

suite('HTML Symbols', () => {

const TEST_URI = "test://test/test.html";

function asPromise<T>(result: T): Promise<T> {
return Promise.resolve(result);
}

const assertSymbols = function (symbols: SymbolInformation[], expected: SymbolInformation[]) {
const assertSymbols = function (symbols: DocumentSymbol[], expected: DocumentSymbol[]) {
assert.deepEqual(symbols, expected);
};

const testSymbolsFor = function (value: string, expected: SymbolInformation[]) {
const testSymbolsFor = function (value: string, expected: DocumentSymbol[]) {
const ls = htmlLanguageService.getLanguageService();
const document = TextDocument.create(TEST_URI, 'html', 0, value);
const htmlDoc = ls.parseHTMLDocument(document);
Expand All @@ -29,18 +25,25 @@ suite('HTML Symbols', () => {
};

test('Simple', () => {
testSymbolsFor('<div></div>', [<SymbolInformation>{ containerName: '', name: 'div', kind: <SymbolKind>SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 0, 0, 11)) }]);
testSymbolsFor('<div><input checked id="test" class="checkbox"></div>', [{ containerName: '', name: 'div', kind: <SymbolKind>SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 0, 0, 53)) },
{ containerName: 'div', name: 'input#test.checkbox', kind: <SymbolKind>SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 5, 0, 47)) }]);
testSymbolsFor('<div></div>', [
DocumentSymbol.create('div', undefined, SymbolKind.Field, Range.create(0, 0, 0, 11), Range.create(0, 0, 0, 11))]
);
testSymbolsFor('<div><input checked id="test" class="checkbox"></div>', [
DocumentSymbol.create('div', undefined, SymbolKind.Field, Range.create(0, 0, 0, 53), Range.create(0, 0, 0, 53), [
DocumentSymbol.create('input#test.checkbox', undefined, SymbolKind.Field, Range.create(0, 5, 0, 47), Range.create(0, 5, 0, 47))
])
]);
});

test('Id and classes', function () {
const content = '<html id=\'root\'><body id="Foo" class="bar"><div class="a b"></div></body></html>';

const expected = [
{ name: 'html#root', kind: SymbolKind.Field, containerName: '', location: Location.create(TEST_URI, Range.create(0, 0, 0, 80)) },
{ name: 'body#Foo.bar', kind: SymbolKind.Field, containerName: 'html#root', location: Location.create(TEST_URI, Range.create(0, 16, 0, 73)) },
{ name: 'div.a.b', kind: SymbolKind.Field, containerName: 'body#Foo.bar', location: Location.create(TEST_URI, Range.create(0, 43, 0, 66)) },
const expected: DocumentSymbol[] = [
DocumentSymbol.create('html#root', undefined, SymbolKind.Field, Range.create(0, 0, 0, 80), Range.create(0, 0, 0, 80), [
DocumentSymbol.create('body#Foo.bar', undefined, SymbolKind.Field, Range.create(0, 16, 0, 73), Range.create(0, 16, 0, 73), [
DocumentSymbol.create('div.a.b', undefined, SymbolKind.Field, Range.create(0, 43, 0, 66), Range.create(0, 43, 0, 66))
])
])
];

testSymbolsFor(content, expected);
Expand All @@ -49,10 +52,11 @@ suite('HTML Symbols', () => {
test('Self closing', function () {
const content = '<html><br id="Foo"><br id=Bar></html>';

const expected = [
{ name: 'html', kind: SymbolKind.Field, containerName: '', location: Location.create(TEST_URI, Range.create(0, 0, 0, 37)) },
{ name: 'br#Foo', kind: SymbolKind.Field, containerName: 'html', location: Location.create(TEST_URI, Range.create(0, 6, 0, 19)) },
{ name: 'br#Bar', kind: SymbolKind.Field, containerName: 'html', location: Location.create(TEST_URI, Range.create(0, 19, 0, 30)) },
const expected: DocumentSymbol[] = [
DocumentSymbol.create('html', undefined, SymbolKind.Field, Range.create(0, 0, 0, 37), Range.create(0, 0, 0, 37), [
DocumentSymbol.create('br#Foo', undefined, SymbolKind.Field, Range.create(0, 6, 0, 19), Range.create(0, 6, 0, 19)),
DocumentSymbol.create('br#Bar', undefined, SymbolKind.Field, Range.create(0, 19, 0, 30), Range.create(0, 19, 0, 30))
])
];

testSymbolsFor(content, expected);
Expand All @@ -61,10 +65,12 @@ suite('HTML Symbols', () => {
test('No attrib', function () {
const content = '<html><body><div></div></body></html>';

const expected = [
{ name: 'html', kind: SymbolKind.Field, containerName: '', location: Location.create(TEST_URI, Range.create(0, 0, 0, 37)) },
{ name: 'body', kind: SymbolKind.Field, containerName: 'html', location: Location.create(TEST_URI, Range.create(0, 6, 0, 30)) },
{ name: 'div', kind: SymbolKind.Field, containerName: 'body', location: Location.create(TEST_URI, Range.create(0, 12, 0, 23)) }
const expected: DocumentSymbol[] = [
DocumentSymbol.create('html', undefined, SymbolKind.Field, Range.create(0, 0, 0, 37), Range.create(0, 0, 0, 37), [
DocumentSymbol.create('body', undefined, SymbolKind.Field, Range.create(0, 6, 0, 30), Range.create(0, 6, 0, 30), [
DocumentSymbol.create('div', undefined, SymbolKind.Field, Range.create(0, 12, 0, 23), Range.create(0, 12, 0, 23))
])
])
];

testSymbolsFor(content, expected);
Expand Down

0 comments on commit 3733e09

Please sign in to comment.