mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 20:52:56 +01:00
Extract a common HoverAdapter
This commit is contained in:
parent
1c2358bdf9
commit
ca17e09d53
7 changed files with 86 additions and 212 deletions
|
|
@ -8,7 +8,6 @@ import * as lsTypes from 'vscode-languageserver-types';
|
|||
import {
|
||||
languages,
|
||||
editor,
|
||||
IMarkdownString,
|
||||
Uri,
|
||||
Position,
|
||||
Range,
|
||||
|
|
@ -19,7 +18,8 @@ import {
|
|||
toRange,
|
||||
toTextEdit,
|
||||
fromRange,
|
||||
CompletionAdapter
|
||||
CompletionAdapter,
|
||||
HoverAdapter
|
||||
} from '../common/lspLanguageFeatures';
|
||||
|
||||
export interface WorkerAccessor {
|
||||
|
|
@ -32,71 +32,7 @@ export class HTMLCompletionAdapter extends CompletionAdapter<HTMLWorker> {
|
|||
}
|
||||
}
|
||||
|
||||
function isMarkupContent(thing: any): thing is lsTypes.MarkupContent {
|
||||
return (
|
||||
thing && typeof thing === 'object' && typeof (<lsTypes.MarkupContent>thing).kind === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
function toMarkdownString(entry: lsTypes.MarkupContent | lsTypes.MarkedString): IMarkdownString {
|
||||
if (typeof entry === 'string') {
|
||||
return {
|
||||
value: entry
|
||||
};
|
||||
}
|
||||
if (isMarkupContent(entry)) {
|
||||
if (entry.kind === 'plaintext') {
|
||||
return {
|
||||
value: entry.value.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&')
|
||||
};
|
||||
}
|
||||
return {
|
||||
value: entry.value
|
||||
};
|
||||
}
|
||||
|
||||
return { value: '```' + entry.language + '\n' + entry.value + '\n```\n' };
|
||||
}
|
||||
|
||||
function toMarkedStringArray(
|
||||
contents: lsTypes.MarkupContent | lsTypes.MarkedString | lsTypes.MarkedString[]
|
||||
): IMarkdownString[] | undefined {
|
||||
if (!contents) {
|
||||
return void 0;
|
||||
}
|
||||
if (Array.isArray(contents)) {
|
||||
return contents.map(toMarkdownString);
|
||||
}
|
||||
return [toMarkdownString(contents)];
|
||||
}
|
||||
|
||||
// --- hover ------
|
||||
|
||||
export class HoverAdapter implements languages.HoverProvider {
|
||||
constructor(private _worker: WorkerAccessor) {}
|
||||
|
||||
provideHover(
|
||||
model: editor.IReadOnlyModel,
|
||||
position: Position,
|
||||
token: CancellationToken
|
||||
): Promise<languages.Hover | undefined> {
|
||||
let resource = model.uri;
|
||||
|
||||
return this._worker(resource)
|
||||
.then((worker) => {
|
||||
return worker.doHover(resource.toString(), fromPosition(position));
|
||||
})
|
||||
.then((info) => {
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
return <languages.Hover>{
|
||||
range: toRange(info.range),
|
||||
contents: toMarkedStringArray(info.contents)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
export class HTMLHoverAdapter extends HoverAdapter<HTMLWorker> {}
|
||||
|
||||
// --- document highlights ------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue