Skip to content

Commit ee0e664

Browse files
Merge pull request #3218 from continuedev/pe/jb-handle-meta-backspace-non-mac
bugfix: delete single word on linux/windows
2 parents ef7fa34 + 03d7a67 commit ee0e664

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

gui/src/components/mainInput/handleMetaKeyIssues.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Editor } from "@tiptap/react";
22
import { KeyboardEvent } from "react";
3-
import { isWebEnvironment } from "../../util";
3+
import { getPlatform, isWebEnvironment } from "../../util";
44

55
const isWebEnv = isWebEnvironment();
6+
const platform = getPlatform();
67

78
/**
89
* This handles various keypress issues when OSR is enabled
@@ -56,15 +57,48 @@ export const handleVSCMetaKeyIssues = async (
5657
}
5758
};
5859

60+
const deleteSingleWord = (editor: Editor) => {
61+
const textContent =
62+
editor.state.doc.resolve(editor.state.selection.from).parent.textContent ??
63+
"";
64+
65+
const cursorPosition = editor.state.selection.from;
66+
const nodeStartPosition = editor.state.doc.resolve(cursorPosition).start();
67+
68+
const textBeforeCursor = textContent.slice(
69+
0,
70+
cursorPosition - nodeStartPosition,
71+
);
72+
73+
// Match the last word including any trailing whitespace
74+
const lastWordMatch = textBeforeCursor.match(/\S+\s*$/);
75+
76+
if (lastWordMatch) {
77+
const lastWordWithSpace = lastWordMatch[0];
78+
editor.commands.deleteRange({
79+
from: editor.state.selection.from - lastWordWithSpace.length,
80+
to: editor.state.selection.from,
81+
});
82+
}
83+
};
84+
5985
export const handleJetBrainsMetaBackspace = (editor: Editor) => {
6086
const { doc } = editor.state;
6187

6288
for (let i = doc.content.childCount - 1; i >= 0; i--) {
6389
const node = doc.content.child(i);
6490

65-
if (node.type.name !== "codeBlock") {
66-
editor.commands.deleteNode(node.type.name);
91+
if (node.type.name === "codeBlock") {
92+
continue;
6793
}
94+
95+
// For Linux/Windows, only delete the word to the left of the cursor
96+
if (platform !== "mac") {
97+
deleteSingleWord(editor);
98+
break;
99+
}
100+
101+
editor.commands.deleteNode(node.type.name);
68102
}
69103

70104
// Add an empty string so the user can keep typing

0 commit comments

Comments
 (0)