Skip to content

Commit 94fdadb

Browse files
committed
Support string in IHoverOptions.text
Fixes microsoft#105841
1 parent 42226a9 commit 94fdadb

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/vs/workbench/services/hover/browser/hover.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ export interface IHoverService {
3939

4040
export interface IHoverOptions {
4141
/**
42-
* The text to display in the primary section of the hover.
42+
* The text to display in the primary section of the hover. The type of text determines the
43+
* default `hideOnHover` behavior.
4344
*/
44-
text: IMarkdownString;
45+
text: IMarkdownString | string;
4546

4647
/**
4748
* The target for the hover. This determines the position of the hover and it will only be
@@ -69,8 +70,13 @@ export interface IHoverOptions {
6970

7071
/**
7172
* Whether to hide the hover when the mouse leaves the `target` and enters the actual hover.
72-
* This is false by default and note that it will be ignored if any `actions` are provided such
73-
* that they are accessible.
73+
* This is false by default when text is an `IMarkdownString` and true when `text` is a
74+
* `string`. Note that this will be ignored if any `actions` are provided as hovering is
75+
* required to make them accessible.
76+
*
77+
* In general hiding on hover is desired for:
78+
* - Regular text where selection is not important
79+
* - Markdown that contains no links where selection is not important
7480
*/
7581
hideOnHover?: boolean;
7682
}

src/vs/workbench/services/hover/browser/hoverWidget.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Widget } from 'vs/base/browser/ui/widget';
1717
import { AnchorPosition } from 'vs/base/browser/ui/contextview/contextview';
1818
import { IOpenerService } from 'vs/platform/opener/common/opener';
1919
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
20+
import { MarkdownString } from 'vs/base/common/htmlContent';
2021

2122
const $ = dom.$;
2223

@@ -78,7 +79,8 @@ export class HoverWidget extends Widget {
7879

7980
const rowElement = $('div.hover-row.markdown-hover');
8081
const contentsElement = $('div.hover-contents');
81-
const markdownElement = renderMarkdown(options.text, {
82+
const markdown = typeof options.text === 'string' ? new MarkdownString(options.text) : options.text;
83+
const markdownElement = renderMarkdown(markdown, {
8284
actionHandler: {
8385
callback: (content) => this._linkHandler(content),
8486
disposeables: this._messageListeners
@@ -118,7 +120,20 @@ export class HoverWidget extends Widget {
118120
}
119121

120122
const mouseTrackerTargets = [...this._target.targetElements];
121-
if (!options.hideOnHover || (options.actions && options.actions.length > 0)) {
123+
let hideOnHover: boolean;
124+
if (options.hideOnHover === undefined) {
125+
if (options.actions && options.actions.length > 0) {
126+
// If there are actions, require hover so they can be accessed
127+
hideOnHover = false;
128+
} else {
129+
// Defaults to true when string, false when markdown as it may contain links
130+
hideOnHover = typeof options.text === 'string';
131+
}
132+
} else {
133+
// It's set explicitly
134+
hideOnHover = options.hideOnHover;
135+
}
136+
if (!hideOnHover) {
122137
mouseTrackerTargets.push(this._hover.containerDomNode);
123138
}
124139
this._mouseTracker = new CompositeMouseTracker(mouseTrackerTargets);

0 commit comments

Comments
 (0)