Extract a common DocumentColorAdapter

This commit is contained in:
Alex Dima 2021-11-17 14:53:34 +01:00
parent 3cb8acd2e9
commit 8d7bf2ad76
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
6 changed files with 79 additions and 132 deletions

View file

@ -833,3 +833,72 @@ function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.Fo
}
//#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