Extract a common DocumentSymbolAdapter

This commit is contained in:
Alex Dima 2021-11-17 14:00:14 +01:00
parent 6e986e8f96
commit 81023950c6
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
7 changed files with 95 additions and 235 deletions

View file

@ -21,7 +21,8 @@ import {
CompletionAdapter,
HoverAdapter,
DocumentHighlightAdapter,
RenameAdapter
RenameAdapter,
DocumentSymbolAdapter
} from '../common/lspLanguageFeatures';
export interface WorkerAccessor {
@ -40,79 +41,7 @@ export class HTMLDocumentHighlightAdapter extends DocumentHighlightAdapter<HTMLW
export class HTMLRenameAdapter extends RenameAdapter<HTMLWorker> {}
// --- document symbols ------
function toSymbolKind(kind: lsTypes.SymbolKind): languages.SymbolKind {
let mKind = languages.SymbolKind;
switch (kind) {
case lsTypes.SymbolKind.File:
return mKind.Array;
case lsTypes.SymbolKind.Module:
return mKind.Module;
case lsTypes.SymbolKind.Namespace:
return mKind.Namespace;
case lsTypes.SymbolKind.Package:
return mKind.Package;
case lsTypes.SymbolKind.Class:
return mKind.Class;
case lsTypes.SymbolKind.Method:
return mKind.Method;
case lsTypes.SymbolKind.Property:
return mKind.Property;
case lsTypes.SymbolKind.Field:
return mKind.Field;
case lsTypes.SymbolKind.Constructor:
return mKind.Constructor;
case lsTypes.SymbolKind.Enum:
return mKind.Enum;
case lsTypes.SymbolKind.Interface:
return mKind.Interface;
case lsTypes.SymbolKind.Function:
return mKind.Function;
case lsTypes.SymbolKind.Variable:
return mKind.Variable;
case lsTypes.SymbolKind.Constant:
return mKind.Constant;
case lsTypes.SymbolKind.String:
return mKind.String;
case lsTypes.SymbolKind.Number:
return mKind.Number;
case lsTypes.SymbolKind.Boolean:
return mKind.Boolean;
case lsTypes.SymbolKind.Array:
return mKind.Array;
}
return mKind.Function;
}
export class DocumentSymbolAdapter implements languages.DocumentSymbolProvider {
constructor(private _worker: WorkerAccessor) {}
public provideDocumentSymbols(
model: editor.IReadOnlyModel,
token: CancellationToken
): Promise<languages.DocumentSymbol[] | undefined> {
const resource = model.uri;
return this._worker(resource)
.then((worker) => worker.findDocumentSymbols(resource.toString()))
.then((items) => {
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: []
}));
});
}
}
export class HTMLDocumentSymbolAdapter extends DocumentSymbolAdapter<HTMLWorker> {}
export class DocumentLinkAdapter implements languages.LinkProvider {
constructor(private _worker: WorkerAccessor) {}