From 5edb6102629c0b4256b417b25fc2e54f475f2276 Mon Sep 17 00:00:00 2001 From: Alexandru Dima Date: Thu, 31 Dec 2020 15:32:39 +0100 Subject: [PATCH] Extract all MarkdownHover computation to MarkdownHoverParticipant --- .../contrib/hover/markdownHoverParticipant.ts | 37 +++++++++++++++++-- .../contrib/hover/markerHoverParticipant.ts | 2 +- .../editor/contrib/hover/modesContentHover.ts | 32 ++++------------ 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/vs/editor/contrib/hover/markdownHoverParticipant.ts b/src/vs/editor/contrib/hover/markdownHoverParticipant.ts index c1a413edf37..250aab419a5 100644 --- a/src/vs/editor/contrib/hover/markdownHoverParticipant.ts +++ b/src/vs/editor/contrib/hover/markdownHoverParticipant.ts @@ -15,6 +15,10 @@ import { IOpenerService } from 'vs/platform/opener/common/opener'; import { IModeService } from 'vs/editor/common/services/modeService'; import { IModelDecoration, ITextModel } from 'vs/editor/common/model'; import { IEditorHover, IEditorHoverParticipant, IHoverPart } from 'vs/editor/contrib/hover/modesContentHover'; +import { HoverProviderRegistry } from 'vs/editor/common/modes'; +import { getHover } from 'vs/editor/contrib/hover/getHover'; +import { Position } from 'vs/editor/common/core/position'; +import { CancellationToken } from 'vs/base/common/cancellation'; const $ = dom.$; @@ -42,11 +46,11 @@ export class MarkdownHoverParticipant implements IEditorHoverParticipant { + if (!this._editor.hasModel() || !range) { + return Promise.resolve([]); + } + + const model = this._editor.getModel(); + + if (!HoverProviderRegistry.has(model)) { + return Promise.resolve([]); + } + + const hovers = await getHover(model, new Position( + range.startLineNumber, + range.startColumn + ), token); + + const result: MarkdownHover[] = []; + for (const hover of hovers) { + if (isEmptyMarkdownString(hover.contents)) { + continue; + } + const rng = hover.range ? Range.lift(hover.range) : range; + result.push(new MarkdownHover(rng, hover.contents)); + } + return result; + } + + public renderHoverParts(hoverParts: MarkdownHover[], fragment: DocumentFragment): IDisposable { const disposables = new DisposableStore(); for (const hoverPart of hoverParts) { for (const contents of hoverPart.contents) { diff --git a/src/vs/editor/contrib/hover/markerHoverParticipant.ts b/src/vs/editor/contrib/hover/markerHoverParticipant.ts index 28021a2b206..eaced514126 100644 --- a/src/vs/editor/contrib/hover/markerHoverParticipant.ts +++ b/src/vs/editor/contrib/hover/markerHoverParticipant.ts @@ -62,7 +62,7 @@ export class MarkerHoverParticipant implements IEditorHoverParticipant { - computeHoverPart(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): T | null; + computeSync(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): T | null; + computeAsync?(range: Range, token: CancellationToken): Promise; renderHoverParts(hoverParts: T[], fragment: DocumentFragment): IDisposable; } @@ -99,26 +99,8 @@ class ModesContentComputer implements IHoverComputer { return Promise.resolve([]); } - const model = this._editor.getModel(); - - if (!HoverProviderRegistry.has(model)) { - return Promise.resolve([]); - } - - const hovers = await getHover(model, new Position( - this._range.startLineNumber, - this._range.startColumn - ), token); - - const result: HoverPartInfo[] = []; - for (const hover of hovers) { - const range = hover.range ? Range.lift(hover.range) : this._range; - if (!range) { - continue; - } - result.push(new HoverPartInfo(this._markdownHoverParticipant, false, new MarkdownHover(range, hover.contents))); - } - return result; + const markdownHovers = await this._markdownHoverParticipant.computeAsync(this._range, token); + return markdownHovers.map(h => new HoverPartInfo(this._markdownHoverParticipant, false, h)); } public computeSync(): HoverPartInfo[] { @@ -148,7 +130,7 @@ class ModesContentComputer implements IHoverComputer { return null; } - const markerHover = this._markerHoverParticipant.computeHoverPart(hoverRange, model, d); + const markerHover = this._markerHoverParticipant.computeSync(hoverRange, model, d); if (markerHover) { return new HoverPartInfo(this._markerHoverParticipant, true, markerHover); } @@ -162,7 +144,7 @@ class ModesContentComputer implements IHoverComputer { return new HoverPartInfo(null, true, new ColorHover(Range.lift(range), color, colorData.provider)); } - const markdownHover = this._markdownHoverParticipant.computeHoverPart(hoverRange, model, d); + const markdownHover = this._markdownHoverParticipant.computeSync(hoverRange, model, d); if (markdownHover) { return new HoverPartInfo(this._markdownHoverParticipant, true, markdownHover); } -- GitLab