Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/vs/editor/contrib/hover/browser/contentHoverController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IKeybindingService } from '../../../../platform/keybinding/common/keybi
import { ResultKind } from '../../../../platform/keybinding/common/keybindingResolver.js';
import { HoverVerbosityAction } from '../../../common/languages.js';
import { RunOnceScheduler } from '../../../../base/common/async.js';
import { isMousePositionWithinElement, shouldShowHover } from './hoverUtils.js';
import { isMousePositionWithinElement, shouldShowHover, isTriggerModifierPressed } from './hoverUtils.js';
import { ContentHoverWidgetWrapper } from './contentHoverWidgetWrapper.js';
import './hover.css';
import { Emitter } from '../../../../base/common/event.js';
Expand Down Expand Up @@ -269,6 +269,29 @@ export class ContentHoverController extends Disposable implements IEditorContrib
if (this._ignoreMouseEvents) {
return;
}
// New behavior: if hover is configured for keyboard modifier and the user presses the triggering modifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this comment. The code explains itself here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

// we should show the hover immediately for the current (last known) mouse location even without moving the mouse again.
if (this._hoverSettings.enabled === 'onKeyboardModifier' && this._mouseMoveEvent) {
const multiCursorModifier = this._editor.getOption(EditorOption.multiCursorModifier); // 'altKey' | 'ctrlKey' | 'metaKey'
const triggerPressed = isTriggerModifierPressed(multiCursorModifier, e);
if (triggerPressed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, this is simpler:

Suggested change
const multiCursorModifier = this._editor.getOption(EditorOption.multiCursorModifier); // 'altKey' | 'ctrlKey' | 'metaKey'
const triggerPressed = isTriggerModifierPressed(multiCursorModifier, e);
if (triggerPressed) {
const multiCursorModifier = this._editor.getOption(EditorOption.multiCursorModifier);
if (isTriggerModifierPressed(multiCursorModifier, e)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// Avoid re-trigger if already visible
if (!this._contentWidget?.isVisible) {
// Get the position/range from the last mouse event and show hover immediately
const mouseTarget = this._mouseMoveEvent.target;
if (mouseTarget.position) {
this.showContentHover(
Range.fromPositions(mouseTarget.position),
HoverStartMode.Immediate,
HoverStartSource.Keyboard,
false
);
}
}
// Do not hide hover for this key press
return;
}
}
if (!this._contentWidget) {
return;
}
Expand Down
21 changes: 16 additions & 5 deletions src/vs/editor/contrib/hover/browser/hoverUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ export function isMousePositionWithinElement(element: HTMLElement, posx: number,
* @param mouseEvent - The current mouse event containing modifier key states
* @returns true if hover should be shown, false otherwise
*/
/**
* Returns true if the trigger modifier (inverse of multi-cursor modifier) is pressed.
* This works with both mouse and keyboard events by relying only on the modifier flags.
*/
export function isTriggerModifierPressed(
multiCursorModifier: 'altKey' | 'ctrlKey' | 'metaKey',
event: { ctrlKey: boolean; metaKey: boolean; altKey: boolean }
): boolean {
if (multiCursorModifier === 'altKey') {
return event.ctrlKey || event.metaKey;
}
return event.altKey; // multiCursorModifier is ctrlKey or metaKey
}

export function shouldShowHover(
hoverEnabled: 'on' | 'off' | 'onKeyboardModifier',
multiCursorModifier: 'altKey' | 'ctrlKey' | 'metaKey',
Expand All @@ -37,9 +51,6 @@ export function shouldShowHover(
if (hoverEnabled === 'off') {
return false;
}
if (multiCursorModifier === 'altKey') {
return mouseEvent.event.ctrlKey || mouseEvent.event.metaKey;
} else {
return mouseEvent.event.altKey;
}
// onKeyboardModifier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This comment is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

return isTriggerModifierPressed(multiCursorModifier, mouseEvent.event);
}