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
|
|
@ -390,3 +390,75 @@ function toCommand(c: lsTypes.Command | undefined): languages.Command | undefine
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
|
//#region HoverAdapter
|
||||||
|
|
||||||
|
export interface ILanguageWorkerWithHover {
|
||||||
|
doHover(uri: string, position: lsTypes.Position): Promise<lsTypes.Hover | null>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class HoverAdapter<T extends ILanguageWorkerWithHover> implements languages.HoverProvider {
|
||||||
|
constructor(private _worker: WorkerAccessor<T>) {}
|
||||||
|
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)];
|
||||||
|
}
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
}
|
}
|
||||||
if (modeConfiguration.hovers) {
|
if (modeConfiguration.hovers) {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker))
|
languages.registerHoverProvider(languageId, new languageFeatures.CSSHoverAdapter(worker))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (modeConfiguration.documentHighlights) {
|
if (modeConfiguration.documentHighlights) {
|
||||||
|
|
|
||||||
|
|
@ -6,21 +6,15 @@
|
||||||
import { LanguageServiceDefaults } from './monaco.contribution';
|
import { LanguageServiceDefaults } from './monaco.contribution';
|
||||||
import type { CSSWorker } from './cssWorker';
|
import type { CSSWorker } from './cssWorker';
|
||||||
import * as lsTypes from 'vscode-languageserver-types';
|
import * as lsTypes from 'vscode-languageserver-types';
|
||||||
import {
|
import { languages, editor, Uri, Position, CancellationToken } from '../fillers/monaco-editor-core';
|
||||||
languages,
|
|
||||||
editor,
|
|
||||||
IMarkdownString,
|
|
||||||
Uri,
|
|
||||||
Position,
|
|
||||||
CancellationToken
|
|
||||||
} from '../fillers/monaco-editor-core';
|
|
||||||
import {
|
import {
|
||||||
DiagnosticsAdapter,
|
DiagnosticsAdapter,
|
||||||
fromPosition,
|
fromPosition,
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
toTextEdit,
|
||||||
fromRange,
|
fromRange,
|
||||||
CompletionAdapter
|
CompletionAdapter,
|
||||||
|
HoverAdapter
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -39,71 +33,7 @@ export class CSSCompletionAdapter extends CompletionAdapter<CSSWorker> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMarkupContent(thing: any): thing is lsTypes.MarkupContent {
|
export class CSSHoverAdapter extends HoverAdapter<CSSWorker> {}
|
||||||
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)
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- document highlights ------
|
// --- document highlights ------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ export function setupMode1(defaults: LanguageServiceDefaults): void {
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.HTMLCompletionAdapter(worker)
|
new languageFeatures.HTMLCompletionAdapter(worker)
|
||||||
);
|
);
|
||||||
languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker));
|
languages.registerHoverProvider(languageId, new languageFeatures.HTMLHoverAdapter(worker));
|
||||||
|
|
||||||
languages.registerDocumentHighlightProvider(
|
languages.registerDocumentHighlightProvider(
|
||||||
languageId,
|
languageId,
|
||||||
|
|
@ -83,7 +83,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
}
|
}
|
||||||
if (modeConfiguration.hovers) {
|
if (modeConfiguration.hovers) {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker))
|
languages.registerHoverProvider(languageId, new languageFeatures.HTMLHoverAdapter(worker))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (modeConfiguration.documentHighlights) {
|
if (modeConfiguration.documentHighlights) {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import * as lsTypes from 'vscode-languageserver-types';
|
||||||
import {
|
import {
|
||||||
languages,
|
languages,
|
||||||
editor,
|
editor,
|
||||||
IMarkdownString,
|
|
||||||
Uri,
|
Uri,
|
||||||
Position,
|
Position,
|
||||||
Range,
|
Range,
|
||||||
|
|
@ -19,7 +18,8 @@ import {
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
toTextEdit,
|
||||||
fromRange,
|
fromRange,
|
||||||
CompletionAdapter
|
CompletionAdapter,
|
||||||
|
HoverAdapter
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -32,71 +32,7 @@ export class HTMLCompletionAdapter extends CompletionAdapter<HTMLWorker> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMarkupContent(thing: any): thing is lsTypes.MarkupContent {
|
export class HTMLHoverAdapter extends HoverAdapter<HTMLWorker> {}
|
||||||
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)
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- document highlights ------
|
// --- document highlights ------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
}
|
}
|
||||||
if (modeConfiguration.hovers) {
|
if (modeConfiguration.hovers) {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerHoverProvider(languageId, new languageFeatures.HoverAdapter(worker))
|
languages.registerHoverProvider(languageId, new languageFeatures.JSONHoverAdapter(worker))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (modeConfiguration.documentSymbols) {
|
if (modeConfiguration.documentSymbols) {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import * as lsTypes from 'vscode-languageserver-types';
|
||||||
import {
|
import {
|
||||||
languages,
|
languages,
|
||||||
editor,
|
editor,
|
||||||
IMarkdownString,
|
|
||||||
Uri,
|
Uri,
|
||||||
Position,
|
Position,
|
||||||
Range,
|
Range,
|
||||||
|
|
@ -21,7 +20,8 @@ import {
|
||||||
toRange,
|
toRange,
|
||||||
toTextEdit,
|
toTextEdit,
|
||||||
fromRange,
|
fromRange,
|
||||||
CompletionAdapter
|
CompletionAdapter,
|
||||||
|
HoverAdapter
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -57,71 +57,7 @@ export class JSONCompletionAdapter extends CompletionAdapter<JSONWorker> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMarkupContent(thing: any): thing is lsTypes.MarkupContent {
|
export class JSONHoverAdapter extends HoverAdapter<JSONWorker> {}
|
||||||
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)
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- definition ------
|
// --- definition ------
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue