mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 19:42: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
|
|
@ -6,21 +6,15 @@
|
|||
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||
import type { CSSWorker } from './cssWorker';
|
||||
import * as lsTypes from 'vscode-languageserver-types';
|
||||
import {
|
||||
languages,
|
||||
editor,
|
||||
IMarkdownString,
|
||||
Uri,
|
||||
Position,
|
||||
CancellationToken
|
||||
} from '../fillers/monaco-editor-core';
|
||||
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
|
||||
import {
|
||||
DiagnosticsAdapter,
|
||||
fromPosition,
|
||||
toRange,
|
||||
toTextEdit,
|
||||
fromRange,
|
||||
CompletionAdapter
|
||||
CompletionAdapter,
|
||||
HoverAdapter
|
||||
} from '../common/lspLanguageFeatures';
|
||||
|
||||
export interface WorkerAccessor {
|
||||
|
|
@ -39,71 +33,7 @@ export class CSSCompletionAdapter extends CompletionAdapter<CSSWorker> {
|
|||
}
|
||||
}
|
||||
|
||||
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 CSSHoverAdapter extends HoverAdapter<CSSWorker> {}
|
||||
|
||||
// --- document highlights ------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue