From 858b03fd8a29ba565a2327806a9c7e0dc9533481 Mon Sep 17 00:00:00 2001 From: tamayika Date: Thu, 6 Apr 2023 17:36:52 +0900 Subject: [PATCH] Change JSON symbol information to document symbol --- src/language/common/lspLanguageFeatures.ts | 45 ++++++++++++++++------ src/language/json/jsonWorker.ts | 4 +- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/language/common/lspLanguageFeatures.ts b/src/language/common/lspLanguageFeatures.ts index 48982daf..7197a2d2 100644 --- a/src/language/common/lspLanguageFeatures.ts +++ b/src/language/common/lspLanguageFeatures.ts @@ -651,7 +651,7 @@ function toWorkspaceEdit(edit: lsTypes.WorkspaceEdit | null): languages.Workspac //#region DocumentSymbolAdapter export interface ILanguageWorkerWithDocumentSymbols { - findDocumentSymbols(uri: string): Promise; + findDocumentSymbols(uri: string): Promise; } export class DocumentSymbolAdapter @@ -671,25 +671,48 @@ export class DocumentSymbolAdapter if (!items) { return; } - return items.map((item) => ({ - name: item.name, - detail: '', - containerName: item.containerName, - kind: toSymbolKind(item.kind), - range: toRange(item.location.range), - selectionRange: toRange(item.location.range), - tags: [] - })); + return items.map((item) => { + if (isDocumentSymbol(item)) { + return toDocumentSymbol(item); + } + return { + name: item.name, + detail: '', + containerName: item.containerName, + kind: toSymbolKind(item.kind), + range: toRange(item.location.range), + selectionRange: toRange(item.location.range), + tags: [] + }; + }); }); } } +function isDocumentSymbol( + symbol: lsTypes.SymbolInformation | lsTypes.DocumentSymbol +): symbol is lsTypes.DocumentSymbol { + return 'children' in symbol; +} + +function toDocumentSymbol(symbol: lsTypes.DocumentSymbol): languages.DocumentSymbol { + return { + name: symbol.name, + detail: symbol.detail ?? '', + kind: toSymbolKind(symbol.kind), + range: toRange(symbol.range), + selectionRange: toRange(symbol.selectionRange), + tags: symbol.tags ?? [], + children: (symbol.children ?? []).map((item) => toDocumentSymbol(item)) + }; +} + function toSymbolKind(kind: lsTypes.SymbolKind): languages.SymbolKind { let mKind = languages.SymbolKind; switch (kind) { case lsTypes.SymbolKind.File: - return mKind.Array; + return mKind.File; case lsTypes.SymbolKind.Module: return mKind.Module; case lsTypes.SymbolKind.Namespace: diff --git a/src/language/json/jsonWorker.ts b/src/language/json/jsonWorker.ts index d18a83de..9487161e 100644 --- a/src/language/json/jsonWorker.ts +++ b/src/language/json/jsonWorker.ts @@ -82,13 +82,13 @@ export class JSONWorker { async resetSchema(uri: string): Promise { return Promise.resolve(this._languageService.resetSchema(uri)); } - async findDocumentSymbols(uri: string): Promise { + async findDocumentSymbols(uri: string): Promise { let document = this._getTextDocument(uri); if (!document) { return []; } let jsonDocument = this._languageService.parseJSONDocument(document); - let symbols = this._languageService.findDocumentSymbols(document, jsonDocument); + let symbols = this._languageService.findDocumentSymbols2(document, jsonDocument); return Promise.resolve(symbols); } async findDocumentColors(uri: string): Promise {