Extract a common DocumentHighlightAdapter

This commit is contained in:
Alex Dima 2021-11-17 13:41:00 +01:00
parent ca17e09d53
commit 09c6ea48f0
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
5 changed files with 62 additions and 85 deletions

View file

@ -14,7 +14,8 @@ import {
toTextEdit,
fromRange,
CompletionAdapter,
HoverAdapter
HoverAdapter,
DocumentHighlightAdapter
} from '../common/lspLanguageFeatures';
export interface WorkerAccessor {
@ -35,47 +36,7 @@ export class CSSCompletionAdapter extends CompletionAdapter<CSSWorker> {
export class CSSHoverAdapter extends HoverAdapter<CSSWorker> {}
// --- document highlights ------
function toDocumentHighlightKind(
kind: lsTypes.DocumentHighlightKind
): languages.DocumentHighlightKind {
switch (kind) {
case lsTypes.DocumentHighlightKind.Read:
return languages.DocumentHighlightKind.Read;
case lsTypes.DocumentHighlightKind.Write:
return languages.DocumentHighlightKind.Write;
case lsTypes.DocumentHighlightKind.Text:
return languages.DocumentHighlightKind.Text;
}
return languages.DocumentHighlightKind.Text;
}
export class DocumentHighlightAdapter implements languages.DocumentHighlightProvider {
constructor(private _worker: WorkerAccessor) {}
public provideDocumentHighlights(
model: editor.IReadOnlyModel,
position: Position,
token: CancellationToken
): Promise<languages.DocumentHighlight[]> {
const resource = model.uri;
return this._worker(resource)
.then((worker) => worker.findDocumentHighlights(resource.toString(), fromPosition(position)))
.then((entries) => {
if (!entries) {
return;
}
return entries.map((entry) => {
return <languages.DocumentHighlight>{
range: toRange(entry.range),
kind: toDocumentHighlightKind(entry.kind)
};
});
});
}
}
export class CSSDocumentHighlightAdapter extends DocumentHighlightAdapter<CSSWorker> {}
// --- definition ------