mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 12:45:39 +01:00
Extract a common DocumentHighlightAdapter
This commit is contained in:
parent
ca17e09d53
commit
09c6ea48f0
5 changed files with 62 additions and 85 deletions
|
|
@ -462,3 +462,56 @@ function toMarkedStringArray(
|
|||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region
|
||||
|
||||
export interface ILanguageWorkerWithDocumentHighlights {
|
||||
findDocumentHighlights(
|
||||
uri: string,
|
||||
position: lsTypes.Position
|
||||
): Promise<lsTypes.DocumentHighlight[]>;
|
||||
}
|
||||
|
||||
export class DocumentHighlightAdapter<T extends ILanguageWorkerWithDocumentHighlights>
|
||||
implements languages.DocumentHighlightProvider
|
||||
{
|
||||
constructor(private _worker: WorkerAccessor<T>) {}
|
||||
|
||||
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)
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
|||
providers.push(
|
||||
languages.registerDocumentHighlightProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentHighlightAdapter(worker)
|
||||
new languageFeatures.CSSDocumentHighlightAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ------
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ export function setupMode1(defaults: LanguageServiceDefaults): void {
|
|||
|
||||
languages.registerDocumentHighlightProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentHighlightAdapter(worker)
|
||||
new languageFeatures.HTMLDocumentHighlightAdapter(worker)
|
||||
);
|
||||
languages.registerLinkProvider(languageId, new languageFeatures.DocumentLinkAdapter(worker));
|
||||
languages.registerFoldingRangeProvider(
|
||||
|
|
@ -90,7 +90,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
|||
providers.push(
|
||||
languages.registerDocumentHighlightProvider(
|
||||
languageId,
|
||||
new languageFeatures.DocumentHighlightAdapter(worker)
|
||||
new languageFeatures.HTMLDocumentHighlightAdapter(worker)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ import {
|
|||
toTextEdit,
|
||||
fromRange,
|
||||
CompletionAdapter,
|
||||
HoverAdapter
|
||||
HoverAdapter,
|
||||
DocumentHighlightAdapter
|
||||
} from '../common/lspLanguageFeatures';
|
||||
|
||||
export interface WorkerAccessor {
|
||||
|
|
@ -36,45 +37,7 @@ export class HTMLHoverAdapter extends HoverAdapter<HTMLWorker> {}
|
|||
|
||||
// --- 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 HTMLDocumentHighlightAdapter extends DocumentHighlightAdapter<HTMLWorker> {}
|
||||
|
||||
function toWorkspaceEdit(edit: lsTypes.WorkspaceEdit): languages.WorkspaceEdit {
|
||||
if (!edit || !edit.changes) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue