Fix terminal Cmd+Backspace on macOS (#2027)
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
44afe784a3
commit
7968f278ee
3 files changed
+65
-1
No files matched your search
@@ -28,7 +28,11 @@ import {
|
||||
resolveWrappedTerminalLinkRange,
|
||||
wrappedTerminalLinkRangeIntersectsBufferLine,
|
||||
} from "../terminal-links";
|
||||
import { isTerminalClearShortcut, terminalNavigationShortcutData } from "../keybindings";
|
||||
import {
|
||||
isTerminalClearShortcut,
|
||||
terminalDeleteShortcutData,
|
||||
terminalNavigationShortcutData,
|
||||
} from "../keybindings";
|
||||
import {
|
||||
DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
DEFAULT_THREAD_TERMINAL_ID,
|
||||
@@ -407,6 +411,14 @@ export function TerminalViewport({
|
||||
return false;
|
||||
}
|
||||
|
||||
const deleteData = terminalDeleteShortcutData(event);
|
||||
if (deleteData !== null) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
void sendTerminalInput(deleteData, "Failed to delete terminal input");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isTerminalClearShortcut(event)) return true;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
resolveShortcutCommand,
|
||||
shouldShowThreadJumpHints,
|
||||
shortcutLabelForCommand,
|
||||
terminalDeleteShortcutData,
|
||||
terminalNavigationShortcutData,
|
||||
threadJumpCommandForIndex,
|
||||
threadJumpIndexFromCommand,
|
||||
@@ -560,6 +561,34 @@ describe("isTerminalClearShortcut", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("terminalDeleteShortcutData", () => {
|
||||
it("maps Cmd+Backspace on macOS to delete-to-line-start", () => {
|
||||
assert.strictEqual(
|
||||
terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "MacIntel"),
|
||||
"\u0015",
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores non-macOS platforms and modified variants", () => {
|
||||
assert.isNull(terminalDeleteShortcutData(event({ key: "Backspace", metaKey: true }), "Linux"));
|
||||
assert.isNull(
|
||||
terminalDeleteShortcutData(
|
||||
event({ key: "Backspace", metaKey: true, altKey: true }),
|
||||
"MacIntel",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores non-keydown events", () => {
|
||||
assert.isNull(
|
||||
terminalDeleteShortcutData(
|
||||
event({ type: "keyup", key: "Backspace", metaKey: true }),
|
||||
"MacIntel",
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("terminalNavigationShortcutData", () => {
|
||||
it("maps Option+Arrow on macOS to word movement", () => {
|
||||
assert.strictEqual(
|
||||
|
||||
@@ -37,6 +37,7 @@ const TERMINAL_WORD_BACKWARD = "\u001bb";
|
||||
const TERMINAL_WORD_FORWARD = "\u001bf";
|
||||
const TERMINAL_LINE_START = "\u0001";
|
||||
const TERMINAL_LINE_END = "\u0005";
|
||||
const TERMINAL_DELETE_TO_LINE_START = "\u0015";
|
||||
const EVENT_CODE_KEY_ALIASES: Readonly<Record<string, readonly string[]>> = {
|
||||
BracketLeft: ["["],
|
||||
BracketRight: ["]"],
|
||||
@@ -370,6 +371,28 @@ export function isTerminalClearShortcut(
|
||||
);
|
||||
}
|
||||
|
||||
export function terminalDeleteShortcutData(
|
||||
event: ShortcutEventLike,
|
||||
platform = navigator.platform,
|
||||
): string | null {
|
||||
if (event.type !== undefined && event.type !== "keydown") {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isMacPlatform(platform)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const key = normalizeEventKey(event.key);
|
||||
if (key !== "backspace") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey
|
||||
? TERMINAL_DELETE_TO_LINE_START
|
||||
: null;
|
||||
}
|
||||
|
||||
export function terminalNavigationShortcutData(
|
||||
event: ShortcutEventLike,
|
||||
platform = navigator.platform,
|
||||
|
||||
Reference in new issue
Block a user