From 3733e0926176060ad458e8514b1250a9fa4a81ec Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Wed, 22 Feb 2023 18:45:32 +0800 Subject: [PATCH] refactor: SymbolInformation -> DocumentSymbol --- src/htmlLanguageService.ts | 4 +-- src/htmlLanguageTypes.ts | 4 +-- src/services/htmlSymbolsProvider.ts | 28 ++++++++-------- src/test/symbols.test.ts | 50 ++++++++++++++++------------- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/htmlLanguageService.ts b/src/htmlLanguageService.ts index 4c79fef..dbdd4da 100644 --- a/src/htmlLanguageService.ts +++ b/src/htmlLanguageService.ts @@ -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'; @@ -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[]; diff --git a/src/htmlLanguageTypes.ts b/src/htmlLanguageTypes.ts index cbb29a0..49b52bf 100644 --- a/src/htmlLanguageTypes.ts +++ b/src/htmlLanguageTypes.ts @@ -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 @@ -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 diff --git a/src/services/htmlSymbolsProvider.ts b/src/services/htmlSymbolsProvider.ts index f162862..2ecf057 100644 --- a/src/services/htmlSymbolsProvider.ts +++ b/src/services/htmlSymbolsProvider.ts @@ -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 = []; +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 = { - name: name, - location: location, - containerName: container, - kind: 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); }); } diff --git a/src/test/symbols.test.ts b/src/test/symbols.test.ts index 3def279..bd4c6d6 100644 --- a/src/test/symbols.test.ts +++ b/src/test/symbols.test.ts @@ -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(result: T): Promise { - 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); @@ -29,18 +25,25 @@ suite('HTML Symbols', () => { }; test('Simple', () => { - testSymbolsFor('
', [{ containerName: '', name: 'div', kind: SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 0, 0, 11)) }]); - testSymbolsFor('
', [{ containerName: '', name: 'div', kind: SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 0, 0, 53)) }, - { containerName: 'div', name: 'input#test.checkbox', kind: SymbolKind.Field, location: Location.create(TEST_URI, Range.create(0, 5, 0, 47)) }]); + testSymbolsFor('
', [ + DocumentSymbol.create('div', undefined, SymbolKind.Field, Range.create(0, 0, 0, 11), Range.create(0, 0, 0, 11))] + ); + testSymbolsFor('
', [ + 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 = '
'; - 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); @@ -49,10 +52,11 @@ suite('HTML Symbols', () => { test('Self closing', function () { const content = '

'; - 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); @@ -61,10 +65,12 @@ suite('HTML Symbols', () => { test('No attrib', function () { const content = '
'; - 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);