Extract all MarkdownHover computation to MarkdownHoverParticipant

上级 408539d8
......@@ -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<Markdow
@IOpenerService private readonly _openerService: IOpenerService,
) { }
createLoadingMessage(range: Range): MarkdownHover {
public createLoadingMessage(range: Range): MarkdownHover {
return new MarkdownHover(range, [new MarkdownString().appendText(nls.localize('modesContentHover.loading', "Loading..."))]);
}
computeHoverPart(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): MarkdownHover | null {
public computeSync(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): MarkdownHover | null {
const hoverMessage = decoration.options.hoverMessage;
if (!hoverMessage || isEmptyMarkdownString(hoverMessage)) {
return null;
......@@ -59,7 +63,34 @@ export class MarkdownHoverParticipant implements IEditorHoverParticipant<Markdow
return new MarkdownHover(range, asArray(hoverMessage));
}
renderHoverParts(hoverParts: MarkdownHover[], fragment: DocumentFragment): IDisposable {
public async computeAsync(range: Range, token: CancellationToken): Promise<MarkdownHover[]> {
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) {
......
......@@ -62,7 +62,7 @@ export class MarkerHoverParticipant implements IEditorHoverParticipant<MarkerHov
@IOpenerService private readonly _openerService: IOpenerService,
) { }
public computeHoverPart(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): MarkerHover | null {
public computeSync(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): MarkerHover | null {
const marker = this._markerDecorationsService.getMarker(model, decoration);
if (marker) {
const lineNumber = hoverRange.startLineNumber;
......
......@@ -11,12 +11,11 @@ import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentW
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { DocumentColorProvider, HoverProviderRegistry, IColor, TokenizationRegistry } from 'vs/editor/common/modes';
import { DocumentColorProvider, IColor, TokenizationRegistry } from 'vs/editor/common/modes';
import { getColorPresentations } from 'vs/editor/contrib/colorPicker/color';
import { ColorDetector } from 'vs/editor/contrib/colorPicker/colorDetector';
import { ColorPickerModel } from 'vs/editor/contrib/colorPicker/colorPickerModel';
import { ColorPickerWidget } from 'vs/editor/contrib/colorPicker/colorPickerWidget';
import { getHover } from 'vs/editor/contrib/hover/getHover';
import { HoverOperation, HoverStartMode, IHoverComputer } from 'vs/editor/contrib/hover/hoverOperation';
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
import { coalesce } from 'vs/base/common/arrays';
......@@ -44,7 +43,8 @@ export interface IEditorHover {
}
export interface IEditorHoverParticipant<T extends IHoverPart = IHoverPart> {
computeHoverPart(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): T | null;
computeSync(hoverRange: Range, model: ITextModel, decoration: IModelDecoration): T | null;
computeAsync?(range: Range, token: CancellationToken): Promise<T[]>;
renderHoverParts(hoverParts: T[], fragment: DocumentFragment): IDisposable;
}
......@@ -99,26 +99,8 @@ class ModesContentComputer implements IHoverComputer<HoverPartInfo[]> {
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<HoverPartInfo[]> {
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<HoverPartInfo[]> {
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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册