Skip to content

Commit f2eff1c

Browse files
authored
[Refactoring] Adding IHoverWidget interface and using switch statement in the _tryShowHoverWidget code (microsoft#210192)
adding interface and using switch statement
1 parent 50c781b commit f2eff1c

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/vs/editor/contrib/hover/browser/contentHover.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { IModelDecoration, PositionAffinity } from 'vs/editor/common/model';
1717
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
1818
import { TokenizationRegistry } from 'vs/editor/common/languages';
1919
import { HoverOperation, HoverStartMode, HoverStartSource, IHoverComputer } from 'vs/editor/contrib/hover/browser/hoverOperation';
20-
import { HoverAnchor, HoverAnchorType, HoverParticipantRegistry, HoverRangeAnchor, IEditorHoverColorPickerWidget, IEditorHoverAction, IEditorHoverParticipant, IEditorHoverRenderContext, IEditorHoverStatusBar, IHoverPart } from 'vs/editor/contrib/hover/browser/hoverTypes';
20+
import { HoverAnchor, HoverAnchorType, HoverParticipantRegistry, HoverRangeAnchor, IEditorHoverColorPickerWidget, IEditorHoverAction, IEditorHoverParticipant, IEditorHoverRenderContext, IEditorHoverStatusBar, IHoverPart, IHoverWidget } from 'vs/editor/contrib/hover/browser/hoverTypes';
2121
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2222
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
2323
import { AsyncIterableObject } from 'vs/base/common/async';
@@ -29,7 +29,7 @@ import { IAccessibilityService } from 'vs/platform/accessibility/common/accessib
2929

3030
const $ = dom.$;
3131

32-
export class ContentHoverController extends Disposable {
32+
export class ContentHoverController extends Disposable implements IHoverWidget {
3333

3434
private _currentResult: HoverResult | null = null;
3535

@@ -304,9 +304,6 @@ export class ContentHoverController extends Disposable {
304304
};
305305
}
306306

307-
/**
308-
* Returns true if the hover shows now or will show.
309-
*/
310307
public showsOrWillShow(mouseEvent: IEditorMouseEvent): boolean {
311308

312309
if (this._widget.isResizing) {

src/vs/editor/contrib/hover/browser/hover.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
2121
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
2222
import { editorHoverBorder } from 'vs/platform/theme/common/colorRegistry';
2323
import { registerThemingParticipant } from 'vs/platform/theme/common/themeService';
24-
import { HoverParticipantRegistry } from 'vs/editor/contrib/hover/browser/hoverTypes';
24+
import { HoverParticipantRegistry, IHoverWidget } from 'vs/editor/contrib/hover/browser/hoverTypes';
2525
import { MarkdownHoverParticipant } from 'vs/editor/contrib/hover/browser/markdownHoverParticipant';
2626
import { MarkerHoverParticipant } from 'vs/editor/contrib/hover/browser/markerHoverParticipant';
2727
import { InlineSuggestionHintsContentWidget } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionsHintsWidget';
@@ -286,9 +286,23 @@ export class HoverController extends Disposable implements IEditorContribution {
286286
}
287287

288288
private _tryShowHoverWidget(mouseEvent: IEditorMouseEvent, hoverWidgetType: HoverWidgetType): boolean {
289-
const isContentWidget = hoverWidgetType === HoverWidgetType.Content;
290-
const currentWidget = isContentWidget ? this._getOrCreateContentWidget() : this._getOrCreateGlyphWidget();
291-
const otherWidget = isContentWidget ? this._getOrCreateGlyphWidget() : this._getOrCreateContentWidget();
289+
const contentWidget: IHoverWidget = this._getOrCreateContentWidget();
290+
const glyphWidget: IHoverWidget = this._getOrCreateGlyphWidget();
291+
let currentWidget: IHoverWidget;
292+
let otherWidget: IHoverWidget;
293+
switch (hoverWidgetType) {
294+
case HoverWidgetType.Content:
295+
currentWidget = contentWidget;
296+
otherWidget = glyphWidget;
297+
break;
298+
case HoverWidgetType.Glyph:
299+
currentWidget = glyphWidget;
300+
otherWidget = contentWidget;
301+
break;
302+
default:
303+
throw new Error(`HoverWidgetType ${hoverWidgetType} is unrecognized`)
304+
}
305+
292306
const showsOrWillShow = currentWidget.showsOrWillShow(mouseEvent);
293307
if (showsOrWillShow) {
294308
otherWidget.hide();

src/vs/editor/contrib/hover/browser/hoverTypes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,17 @@ export const HoverParticipantRegistry = (new class HoverParticipantRegistry {
145145
}
146146

147147
}());
148+
149+
export interface IHoverWidget {
150+
/**
151+
* Returns whether the hover widget is shown or should show in the future.
152+
* If the widget should show, this triggers the display.
153+
* @param mouseEvent editor mouse event
154+
*/
155+
showsOrWillShow(mouseEvent: IEditorMouseEvent): boolean;
156+
157+
/**
158+
* Hides the hover.
159+
*/
160+
hide(): void;
161+
}

src/vs/editor/contrib/hover/browser/marginHover.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { HoverOperation, HoverStartMode, IHoverComputer } from 'vs/editor/contri
1515
import { IOpenerService } from 'vs/platform/opener/common/opener';
1616
import { HoverWidget } from 'vs/base/browser/ui/hover/hoverWidget';
1717
import { GlyphMarginLane } from 'vs/editor/common/model';
18+
import { IHoverWidget } from 'vs/editor/contrib/hover/browser/hoverTypes';
1819

1920
const $ = dom.$;
2021

@@ -24,7 +25,7 @@ export interface IHoverMessage {
2425

2526
type LaneOrLineNumber = GlyphMarginLane | 'lineNo';
2627

27-
export class MarginHoverWidget extends Disposable implements IOverlayWidget {
28+
export class MarginHoverWidget extends Disposable implements IOverlayWidget, IHoverWidget {
2829

2930
public static readonly ID = 'editor.contrib.modesGlyphHoverWidget';
3031

0 commit comments

Comments
 (0)