mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 23:13:02 +01:00
Extract a common DocumentColorAdapter
This commit is contained in:
parent
3cb8acd2e9
commit
8d7bf2ad76
6 changed files with 79 additions and 132 deletions
|
|
@ -833,3 +833,72 @@ function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.Fo
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
|
//#region DocumentColorAdapter
|
||||||
|
|
||||||
|
export interface ILanguageWorkerWithDocumentColors {
|
||||||
|
findDocumentColors(uri: string): Promise<lsTypes.ColorInformation[]>;
|
||||||
|
getColorPresentations(
|
||||||
|
uri: string,
|
||||||
|
color: lsTypes.Color,
|
||||||
|
range: lsTypes.Range
|
||||||
|
): Promise<lsTypes.ColorPresentation[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DocumentColorAdapter<T extends ILanguageWorkerWithDocumentColors>
|
||||||
|
implements languages.DocumentColorProvider
|
||||||
|
{
|
||||||
|
constructor(private readonly _worker: WorkerAccessor<T>) {}
|
||||||
|
|
||||||
|
public provideDocumentColors(
|
||||||
|
model: editor.IReadOnlyModel,
|
||||||
|
token: CancellationToken
|
||||||
|
): Promise<languages.IColorInformation[] | undefined> {
|
||||||
|
const resource = model.uri;
|
||||||
|
|
||||||
|
return this._worker(resource)
|
||||||
|
.then((worker) => worker.findDocumentColors(resource.toString()))
|
||||||
|
.then((infos) => {
|
||||||
|
if (!infos) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return infos.map((item) => ({
|
||||||
|
color: item.color,
|
||||||
|
range: toRange(item.range)
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public provideColorPresentations(
|
||||||
|
model: editor.IReadOnlyModel,
|
||||||
|
info: languages.IColorInformation,
|
||||||
|
token: CancellationToken
|
||||||
|
): Promise<languages.IColorPresentation[] | undefined> {
|
||||||
|
const resource = model.uri;
|
||||||
|
|
||||||
|
return this._worker(resource)
|
||||||
|
.then((worker) =>
|
||||||
|
worker.getColorPresentations(resource.toString(), info.color, fromRange(info.range))
|
||||||
|
)
|
||||||
|
.then((presentations) => {
|
||||||
|
if (!presentations) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return presentations.map((presentation) => {
|
||||||
|
let item: languages.IColorPresentation = {
|
||||||
|
label: presentation.label
|
||||||
|
};
|
||||||
|
if (presentation.textEdit) {
|
||||||
|
item.textEdit = toTextEdit(presentation.textEdit);
|
||||||
|
}
|
||||||
|
if (presentation.additionalTextEdits) {
|
||||||
|
item.additionalTextEdits =
|
||||||
|
presentation.additionalTextEdits.map<languages.TextEdit>(toTextEdit);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerColorProvider(
|
languages.registerColorProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentColorAdapter(worker)
|
new languageFeatures.CSSDocumentColorAdapter(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,14 @@ import {
|
||||||
DiagnosticsAdapter,
|
DiagnosticsAdapter,
|
||||||
fromPosition,
|
fromPosition,
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
|
||||||
fromRange,
|
|
||||||
CompletionAdapter,
|
CompletionAdapter,
|
||||||
HoverAdapter,
|
HoverAdapter,
|
||||||
DocumentHighlightAdapter,
|
DocumentHighlightAdapter,
|
||||||
DefinitionAdapter,
|
DefinitionAdapter,
|
||||||
ReferenceAdapter,
|
ReferenceAdapter,
|
||||||
RenameAdapter,
|
RenameAdapter,
|
||||||
DocumentSymbolAdapter
|
DocumentSymbolAdapter,
|
||||||
|
DocumentColorAdapter
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -50,59 +49,7 @@ export class CSSRenameAdapter extends RenameAdapter<CSSWorker> {}
|
||||||
|
|
||||||
export class CSSDocumentSymbolAdapter extends DocumentSymbolAdapter<CSSWorker> {}
|
export class CSSDocumentSymbolAdapter extends DocumentSymbolAdapter<CSSWorker> {}
|
||||||
|
|
||||||
export class DocumentColorAdapter implements languages.DocumentColorProvider {
|
export class CSSDocumentColorAdapter extends DocumentColorAdapter<CSSWorker> {}
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
public provideDocumentColors(
|
|
||||||
model: editor.IReadOnlyModel,
|
|
||||||
token: CancellationToken
|
|
||||||
): Promise<languages.IColorInformation[] | undefined> {
|
|
||||||
const resource = model.uri;
|
|
||||||
|
|
||||||
return this._worker(resource)
|
|
||||||
.then((worker) => worker.findDocumentColors(resource.toString()))
|
|
||||||
.then((infos) => {
|
|
||||||
if (!infos) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return infos.map((item) => ({
|
|
||||||
color: item.color,
|
|
||||||
range: toRange(item.range)
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public provideColorPresentations(
|
|
||||||
model: editor.IReadOnlyModel,
|
|
||||||
info: languages.IColorInformation,
|
|
||||||
token: CancellationToken
|
|
||||||
): Promise<languages.IColorPresentation[] | undefined> {
|
|
||||||
const resource = model.uri;
|
|
||||||
|
|
||||||
return this._worker(resource)
|
|
||||||
.then((worker) =>
|
|
||||||
worker.getColorPresentations(resource.toString(), info.color, fromRange(info.range))
|
|
||||||
)
|
|
||||||
.then((presentations) => {
|
|
||||||
if (!presentations) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return presentations.map((presentation) => {
|
|
||||||
let item: languages.IColorPresentation = {
|
|
||||||
label: presentation.label
|
|
||||||
};
|
|
||||||
if (presentation.textEdit) {
|
|
||||||
item.textEdit = toTextEdit(presentation.textEdit);
|
|
||||||
}
|
|
||||||
if (presentation.additionalTextEdits) {
|
|
||||||
item.additionalTextEdits =
|
|
||||||
presentation.additionalTextEdits.map<languages.TextEdit>(toTextEdit);
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
constructor(private _worker: WorkerAccessor) {}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,10 @@
|
||||||
|
|
||||||
import type { HTMLWorker } from './htmlWorker';
|
import type { HTMLWorker } from './htmlWorker';
|
||||||
import * as lsTypes from 'vscode-languageserver-types';
|
import * as lsTypes from 'vscode-languageserver-types';
|
||||||
import {
|
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
|
||||||
languages,
|
|
||||||
editor,
|
|
||||||
Uri,
|
|
||||||
Position,
|
|
||||||
Range,
|
|
||||||
CancellationToken
|
|
||||||
} from '../fillers/monaco-editor-core';
|
|
||||||
import {
|
import {
|
||||||
fromPosition,
|
fromPosition,
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
|
||||||
fromRange,
|
|
||||||
CompletionAdapter,
|
CompletionAdapter,
|
||||||
HoverAdapter,
|
HoverAdapter,
|
||||||
DocumentHighlightAdapter,
|
DocumentHighlightAdapter,
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerColorProvider(
|
languages.registerColorProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentColorAdapter(worker)
|
new languageFeatures.JSONDocumentColorAdapter(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,25 +6,17 @@
|
||||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||||
import type { JSONWorker } from './jsonWorker';
|
import type { JSONWorker } from './jsonWorker';
|
||||||
import * as lsTypes from 'vscode-languageserver-types';
|
import * as lsTypes from 'vscode-languageserver-types';
|
||||||
import {
|
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
|
||||||
languages,
|
|
||||||
editor,
|
|
||||||
Uri,
|
|
||||||
Position,
|
|
||||||
Range,
|
|
||||||
CancellationToken
|
|
||||||
} from '../fillers/monaco-editor-core';
|
|
||||||
import {
|
import {
|
||||||
DiagnosticsAdapter,
|
DiagnosticsAdapter,
|
||||||
fromPosition,
|
fromPosition,
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
|
||||||
fromRange,
|
|
||||||
CompletionAdapter,
|
CompletionAdapter,
|
||||||
HoverAdapter,
|
HoverAdapter,
|
||||||
DocumentSymbolAdapter,
|
DocumentSymbolAdapter,
|
||||||
DocumentFormattingEditProvider,
|
DocumentFormattingEditProvider,
|
||||||
DocumentRangeFormattingEditProvider
|
DocumentRangeFormattingEditProvider,
|
||||||
|
DocumentColorAdapter
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -68,59 +60,7 @@ export class JSONDocumentFormattingEditProvider extends DocumentFormattingEditPr
|
||||||
|
|
||||||
export class JSONDocumentRangeFormattingEditProvider extends DocumentRangeFormattingEditProvider<JSONWorker> {}
|
export class JSONDocumentRangeFormattingEditProvider extends DocumentRangeFormattingEditProvider<JSONWorker> {}
|
||||||
|
|
||||||
export class DocumentColorAdapter implements languages.DocumentColorProvider {
|
export class JSONDocumentColorAdapter extends DocumentColorAdapter<JSONWorker> {}
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
public provideDocumentColors(
|
|
||||||
model: editor.IReadOnlyModel,
|
|
||||||
token: CancellationToken
|
|
||||||
): Promise<languages.IColorInformation[] | undefined> {
|
|
||||||
const resource = model.uri;
|
|
||||||
|
|
||||||
return this._worker(resource)
|
|
||||||
.then((worker) => worker.findDocumentColors(resource.toString()))
|
|
||||||
.then((infos) => {
|
|
||||||
if (!infos) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return infos.map((item) => ({
|
|
||||||
color: item.color,
|
|
||||||
range: toRange(item.range)
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public provideColorPresentations(
|
|
||||||
model: editor.IReadOnlyModel,
|
|
||||||
info: languages.IColorInformation,
|
|
||||||
token: CancellationToken
|
|
||||||
): Promise<languages.IColorPresentation[] | undefined> {
|
|
||||||
const resource = model.uri;
|
|
||||||
|
|
||||||
return this._worker(resource)
|
|
||||||
.then((worker) =>
|
|
||||||
worker.getColorPresentations(resource.toString(), info.color, fromRange(info.range))
|
|
||||||
)
|
|
||||||
.then((presentations) => {
|
|
||||||
if (!presentations) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return presentations.map((presentation) => {
|
|
||||||
let item: languages.IColorPresentation = {
|
|
||||||
label: presentation.label
|
|
||||||
};
|
|
||||||
if (presentation.textEdit) {
|
|
||||||
item.textEdit = toTextEdit(presentation.textEdit);
|
|
||||||
}
|
|
||||||
if (presentation.additionalTextEdits) {
|
|
||||||
item.additionalTextEdits =
|
|
||||||
presentation.additionalTextEdits.map<languages.TextEdit>(toTextEdit);
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
constructor(private _worker: WorkerAccessor) {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue