Extract a common `SelectionRangeAdapter`

pull/2779/head
Alex Dima 3 years ago
parent b49f8ffd5f
commit 8f57069f86
No known key found for this signature in database
GPG Key ID: 39563C1504FDD0C9

@ -956,3 +956,46 @@ function toFoldingRangeKind(
}
//#endregion
//#region SelectionRangeAdapter
export interface ILanguageWorkerWithSelectionRanges {
getSelectionRanges(uri: string, positions: lsTypes.Position[]): Promise<lsTypes.SelectionRange[]>;
}
export class SelectionRangeAdapter<T extends ILanguageWorkerWithSelectionRanges>
implements languages.SelectionRangeProvider
{
constructor(private _worker: WorkerAccessor<T>) {}
public provideSelectionRanges(
model: editor.IReadOnlyModel,
positions: Position[],
token: CancellationToken
): Promise<languages.SelectionRange[][] | undefined> {
const resource = model.uri;
return this._worker(resource)
.then((worker) =>
worker.getSelectionRanges(
resource.toString(),
positions.map<lsTypes.Position>(fromPosition)
)
)
.then((selectionRanges) => {
if (!selectionRanges) {
return;
}
return selectionRanges.map((selectionRange: lsTypes.SelectionRange | undefined) => {
const result: languages.SelectionRange[] = [];
while (selectionRange) {
result.push({ range: toRange(selectionRange.range) });
selectionRange = selectionRange.parent;
}
return result;
});
});
}
}
//#endregion

@ -98,7 +98,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
providers.push(
languages.registerSelectionRangeProvider(
languageId,
new languageFeatures.SelectionRangeAdapter(worker)
new languageFeatures.CSSSelectionRangeAdapter(worker)
)
);
}

@ -5,12 +5,9 @@
import { LanguageServiceDefaults } from './monaco.contribution';
import type { CSSWorker } from './cssWorker';
import * as lsTypes from 'vscode-languageserver-types';
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
import { Uri } from '../fillers/monaco-editor-core';
import {
DiagnosticsAdapter,
fromPosition,
toRange,
CompletionAdapter,
HoverAdapter,
DocumentHighlightAdapter,
@ -19,7 +16,8 @@ import {
RenameAdapter,
DocumentSymbolAdapter,
DocumentColorAdapter,
FoldingRangeAdapter
FoldingRangeAdapter,
SelectionRangeAdapter
} from '../common/lspLanguageFeatures';
export interface WorkerAccessor {
@ -54,35 +52,4 @@ export class CSSDocumentColorAdapter extends DocumentColorAdapter<CSSWorker> {}
export class CSSFoldingRangeAdapter extends FoldingRangeAdapter<CSSWorker> {}
export class SelectionRangeAdapter implements languages.SelectionRangeProvider {
constructor(private _worker: WorkerAccessor) {}
public provideSelectionRanges(
model: editor.IReadOnlyModel,
positions: Position[],
token: CancellationToken
): Promise<languages.SelectionRange[][] | undefined> {
const resource = model.uri;
return this._worker(resource)
.then((worker) =>
worker.getSelectionRanges(
resource.toString(),
positions.map<lsTypes.Position>(fromPosition)
)
)
.then((selectionRanges) => {
if (!selectionRanges) {
return;
}
return selectionRanges.map((selectionRange: lsTypes.SelectionRange | undefined) => {
const result: languages.SelectionRange[] = [];
while (selectionRange) {
result.push({ range: toRange(selectionRange.range) });
selectionRange = selectionRange.parent;
}
return result;
});
});
}
}
export class CSSSelectionRangeAdapter extends SelectionRangeAdapter<CSSWorker> {}

@ -40,7 +40,7 @@ export function setupMode1(defaults: LanguageServiceDefaults): void {
);
languages.registerSelectionRangeProvider(
languageId,
new languageFeatures.SelectionRangeAdapter(worker)
new languageFeatures.HTMLSelectionRangeAdapter(worker)
);
languages.registerRenameProvider(languageId, new languageFeatures.HTMLRenameAdapter(worker));
@ -127,7 +127,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
providers.push(
languages.registerSelectionRangeProvider(
languageId,
new languageFeatures.SelectionRangeAdapter(worker)
new languageFeatures.HTMLSelectionRangeAdapter(worker)
)
);
}

@ -4,11 +4,8 @@
*--------------------------------------------------------------------------------------------*/
import type { HTMLWorker } from './htmlWorker';
import * as lsTypes from 'vscode-languageserver-types';
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
import { Uri } from '../fillers/monaco-editor-core';
import {
fromPosition,
toRange,
CompletionAdapter,
HoverAdapter,
DocumentHighlightAdapter,
@ -17,7 +14,8 @@ import {
DocumentLinkAdapter,
DocumentFormattingEditProvider,
DocumentRangeFormattingEditProvider,
FoldingRangeAdapter
FoldingRangeAdapter,
SelectionRangeAdapter
} from '../common/lspLanguageFeatures';
export interface WorkerAccessor {
@ -46,35 +44,4 @@ export class HTMLDocumentRangeFormattingEditProvider extends DocumentRangeFormat
export class HTMLFoldingRangeAdapter extends FoldingRangeAdapter<HTMLWorker> {}
export class SelectionRangeAdapter implements languages.SelectionRangeProvider {
constructor(private _worker: WorkerAccessor) {}
public provideSelectionRanges(
model: editor.IReadOnlyModel,
positions: Position[],
token: CancellationToken
): Promise<languages.SelectionRange[][] | undefined> {
const resource = model.uri;
return this._worker(resource)
.then((worker) =>
worker.getSelectionRanges(
resource.toString(),
positions.map<lsTypes.Position>(fromPosition)
)
)
.then((selectionRanges) => {
if (!selectionRanges) {
return;
}
return selectionRanges.map((selectionRange: lsTypes.SelectionRange | undefined) => {
const result: languages.SelectionRange[] = [];
while (selectionRange) {
result.push({ range: toRange(selectionRange.range) });
selectionRange = selectionRange.parent;
}
return result;
});
});
}
}
export class HTMLSelectionRangeAdapter extends SelectionRangeAdapter<HTMLWorker> {}

@ -89,7 +89,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
providers.push(
languages.registerSelectionRangeProvider(
languageId,
new languageFeatures.SelectionRangeAdapter(worker)
new languageFeatures.JSONSelectionRangeAdapter(worker)
)
);
}

@ -5,19 +5,17 @@
import { LanguageServiceDefaults } from './monaco.contribution';
import type { JSONWorker } from './jsonWorker';
import * as lsTypes from 'vscode-languageserver-types';
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
import { editor, Uri } from '../fillers/monaco-editor-core';
import {
DiagnosticsAdapter,
fromPosition,
toRange,
CompletionAdapter,
HoverAdapter,
DocumentSymbolAdapter,
DocumentFormattingEditProvider,
DocumentRangeFormattingEditProvider,
DocumentColorAdapter,
FoldingRangeAdapter
FoldingRangeAdapter,
SelectionRangeAdapter
} from '../common/lspLanguageFeatures';
export interface WorkerAccessor {
@ -65,35 +63,4 @@ export class JSONDocumentColorAdapter extends DocumentColorAdapter<JSONWorker> {
export class JSONFoldingRangeAdapter extends FoldingRangeAdapter<JSONWorker> {}
export class SelectionRangeAdapter implements languages.SelectionRangeProvider {
constructor(private _worker: WorkerAccessor) {}
public provideSelectionRanges(
model: editor.IReadOnlyModel,
positions: Position[],
token: CancellationToken
): Promise<languages.SelectionRange[][] | undefined> {
const resource = model.uri;
return this._worker(resource)
.then((worker) =>
worker.getSelectionRanges(
resource.toString(),
positions.map<lsTypes.Position>(fromPosition)
)
)
.then((selectionRanges) => {
if (!selectionRanges) {
return;
}
return selectionRanges.map((selectionRange: lsTypes.SelectionRange | undefined) => {
const result: languages.SelectionRange[] = [];
while (selectionRange) {
result.push({ range: toRange(selectionRange.range) });
selectionRange = selectionRange.parent;
}
return result;
});
});
}
}
export class JSONSelectionRangeAdapter extends SelectionRangeAdapter<JSONWorker> {}

Loading…
Cancel
Save