Skip to content

Commit

Permalink
Backport PR #15898: Fix spurious dedent when opening inspector tooltip (
Browse files Browse the repository at this point in the history
#15917)

Co-authored-by: Michał Krassowski <5832902+krassowski@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and krassowski committed Mar 5, 2024
1 parent c348665 commit a3cdd38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions packages/codemirror/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import {
indentLess,
indentMore,
insertNewlineAndIndent,
insertTab
Expand All @@ -19,6 +20,12 @@ import {
*/
const CODE_RUNNER_SELECTOR = '[data-jp-code-runner]';

/**
* Selector for a widget that can open a tooltip.
*/
const TOOLTIP_OPENER_SELECTOR =
'.jp-CodeMirrorEditor:not(.jp-mod-has-primary-selection):not(.jp-mod-in-leading-whitespace):not(.jp-mod-completer-active)';

/**
* CodeMirror commands namespace
*/
Expand Down Expand Up @@ -76,4 +83,22 @@ export namespace StateCommands {
}
return false;
}

/**
* Prevent dedenting when launching inspection request (a.k.a tooltip).
*
* This function should be removed once a better way to prevent default
* CodeMirror commands is implemented, as tracked in
* https://github.com/jupyterlab/jupyterlab/issues/15897
*/
export function dedentIfNotLaunchingTooltip(target: {
dom: HTMLElement;
state: EditorState;
dispatch: (transaction: Transaction) => void;
}): boolean {
if (target.dom.closest(TOOLTIP_OPENER_SELECTOR)) {
return true;
}
return indentLess(target);
}
}
4 changes: 2 additions & 2 deletions packages/codemirror/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
import { defaultKeymap, indentLess } from '@codemirror/commands';
import { defaultKeymap } from '@codemirror/commands';
import {
bracketMatching,
foldGutter,
Expand Down Expand Up @@ -715,7 +715,7 @@ export namespace EditorExtensionRegistry {
{
key: 'Tab',
run: StateCommands.indentMoreOrInsertTab,
shift: indentLess
shift: StateCommands.dedentIfNotLaunchingTooltip
}
],
factory: () =>
Expand Down

0 comments on commit a3cdd38

Please sign in to comment.