Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change JSON symbol information to document symbol #3894

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/language/common/lspLanguageFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ function toWorkspaceEdit(edit: lsTypes.WorkspaceEdit | null): languages.Workspac
//#region DocumentSymbolAdapter

export interface ILanguageWorkerWithDocumentSymbols {
findDocumentSymbols(uri: string): Promise<lsTypes.SymbolInformation[]>;
findDocumentSymbols(uri: string): Promise<lsTypes.SymbolInformation[] | lsTypes.DocumentSymbol[]>;
}

export class DocumentSymbolAdapter<T extends ILanguageWorkerWithDocumentSymbols>
Expand All @@ -671,25 +671,48 @@ export class DocumentSymbolAdapter<T extends ILanguageWorkerWithDocumentSymbols>
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a small mistake in addition.

case lsTypes.SymbolKind.Module:
return mKind.Module;
case lsTypes.SymbolKind.Namespace:
Expand Down
4 changes: 2 additions & 2 deletions src/language/json/jsonWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ export class JSONWorker {
async resetSchema(uri: string): Promise<boolean> {
return Promise.resolve(this._languageService.resetSchema(uri));
}
async findDocumentSymbols(uri: string): Promise<jsonService.SymbolInformation[]> {
async findDocumentSymbols(uri: string): Promise<jsonService.DocumentSymbol[]> {
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<jsonService.ColorInformation[]> {
Expand Down
Loading