Skip to content

Commit

Permalink
Merge pull request #3218 from continuedev/pe/jb-handle-meta-backspace…
Browse files Browse the repository at this point in the history
…-non-mac

bugfix: delete single word on linux/windows
  • Loading branch information
Patrick-Erichsen authored Dec 6, 2024
2 parents ef7fa34 + 03d7a67 commit ee0e664
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions gui/src/components/mainInput/handleMetaKeyIssues.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Editor } from "@tiptap/react";
import { KeyboardEvent } from "react";
import { isWebEnvironment } from "../../util";
import { getPlatform, isWebEnvironment } from "../../util";

const isWebEnv = isWebEnvironment();
const platform = getPlatform();

/**
* This handles various keypress issues when OSR is enabled
Expand Down Expand Up @@ -56,15 +57,48 @@ export const handleVSCMetaKeyIssues = async (
}
};

const deleteSingleWord = (editor: Editor) => {
const textContent =
editor.state.doc.resolve(editor.state.selection.from).parent.textContent ??
"";

const cursorPosition = editor.state.selection.from;
const nodeStartPosition = editor.state.doc.resolve(cursorPosition).start();

const textBeforeCursor = textContent.slice(
0,
cursorPosition - nodeStartPosition,
);

// Match the last word including any trailing whitespace
const lastWordMatch = textBeforeCursor.match(/\S+\s*$/);

if (lastWordMatch) {
const lastWordWithSpace = lastWordMatch[0];
editor.commands.deleteRange({
from: editor.state.selection.from - lastWordWithSpace.length,
to: editor.state.selection.from,
});
}
};

export const handleJetBrainsMetaBackspace = (editor: Editor) => {
const { doc } = editor.state;

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

if (node.type.name !== "codeBlock") {
editor.commands.deleteNode(node.type.name);
if (node.type.name === "codeBlock") {
continue;
}

// For Linux/Windows, only delete the word to the left of the cursor
if (platform !== "mac") {
deleteSingleWord(editor);
break;
}

editor.commands.deleteNode(node.type.name);
}

// Add an empty string so the user can keep typing
Expand Down

0 comments on commit ee0e664

Please sign in to comment.