Переглянути джерело

Extract a common `DocumentSymbolAdapter`

Alex Dima 3 роки тому
батько
коміт
81023950c6

+ 82 - 0
src/common/lspLanguageFeatures.ts

@@ -646,3 +646,85 @@ function toWorkspaceEdit(edit: lsTypes.WorkspaceEdit): languages.WorkspaceEdit {
 }
 
 //#endregion
+
+//#region DocumentSymbolAdapter
+
+export interface ILanguageWorkerWithDocumentSymbols {
+	findDocumentSymbols(uri: string): Promise<lsTypes.SymbolInformation[]>;
+}
+
+export class DocumentSymbolAdapter<T extends ILanguageWorkerWithDocumentSymbols>
+	implements languages.DocumentSymbolProvider
+{
+	constructor(private readonly _worker: WorkerAccessor<T>) {}
+
+	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: []
+				}));
+			});
+	}
+}
+
+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;
+}
+
+//#endregion

+ 1 - 1
src/css/cssMode.ts

@@ -66,7 +66,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
 			providers.push(
 				languages.registerDocumentSymbolProvider(
 					languageId,
-					new languageFeatures.DocumentSymbolAdapter(worker)
+					new languageFeatures.CSSDocumentSymbolAdapter(worker)
 				)
 			);
 		}

+ 3 - 74
src/css/languageFeatures.ts

@@ -18,7 +18,8 @@ import {
 	DocumentHighlightAdapter,
 	DefinitionAdapter,
 	ReferenceAdapter,
-	RenameAdapter
+	RenameAdapter,
+	DocumentSymbolAdapter
 } from '../common/lspLanguageFeatures';
 
 export interface WorkerAccessor {
@@ -47,79 +48,7 @@ export class CSSReferenceAdapter extends ReferenceAdapter<CSSWorker> {}
 
 export class CSSRenameAdapter extends RenameAdapter<CSSWorker> {}
 
-// --- 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 CSSDocumentSymbolAdapter extends DocumentSymbolAdapter<CSSWorker> {}
 
 export class DocumentColorAdapter implements languages.DocumentColorProvider {
 	constructor(private _worker: WorkerAccessor) {}

+ 2 - 2
src/html/htmlMode.ts

@@ -36,7 +36,7 @@ export function setupMode1(defaults: LanguageServiceDefaults): void {
 	);
 	languages.registerDocumentSymbolProvider(
 		languageId,
-		new languageFeatures.DocumentSymbolAdapter(worker)
+		new languageFeatures.HTMLDocumentSymbolAdapter(worker)
 	);
 	languages.registerSelectionRangeProvider(
 		languageId,
@@ -103,7 +103,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
 			providers.push(
 				languages.registerDocumentSymbolProvider(
 					languageId,
-					new languageFeatures.DocumentSymbolAdapter(worker)
+					new languageFeatures.HTMLDocumentSymbolAdapter(worker)
 				)
 			);
 		}

+ 3 - 74
src/html/languageFeatures.ts

@@ -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) {}

+ 1 - 1
src/json/jsonMode.ts

@@ -59,7 +59,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
 			providers.push(
 				languages.registerDocumentSymbolProvider(
 					languageId,
-					new languageFeatures.DocumentSymbolAdapter(worker)
+					new languageFeatures.JSONDocumentSymbolAdapter(worker)
 				)
 			);
 		}

+ 3 - 83
src/json/languageFeatures.ts

@@ -21,7 +21,8 @@ import {
 	toTextEdit,
 	fromRange,
 	CompletionAdapter,
-	HoverAdapter
+	HoverAdapter,
+	DocumentSymbolAdapter
 } from '../common/lspLanguageFeatures';
 
 export interface WorkerAccessor {
@@ -59,88 +60,7 @@ export class JSONCompletionAdapter extends CompletionAdapter<JSONWorker> {
 
 export class JSONHoverAdapter extends HoverAdapter<JSONWorker> {}
 
-// --- definition ------
-
-function toLocation(location: lsTypes.Location): languages.Location {
-	return {
-		uri: Uri.parse(location.uri),
-		range: toRange(location.range)
-	};
-}
-
-// --- 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 JSONDocumentSymbolAdapter extends DocumentSymbolAdapter<JSONWorker> {}
 
 function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.FormattingOptions {
 	return {