fix(jb): meta keybinding issues

This commit is contained in:
Test 2024-11-05 16:54:25 -08:00
parent f7116eaa3f
commit b4dad78a72
5 changed files with 141 additions and 155 deletions

View file

@ -22,7 +22,7 @@ class ConfigJsonSchemaProviderFactory : JsonSchemaProviderFactory {
class ConfigJsonSchemaFileProvider : JsonSchemaFileProvider {
override fun isAvailable(file: VirtualFile): Boolean {
return path.endsWith("/.continue/config.json")
return file.path.endsWith("/.continue/config.json")
}
override fun getName(): String {

View file

@ -280,7 +280,7 @@ class ContinueBrowser(val project: Project, url: String) {
*
* Setting `setOffScreenRendering` to `false` causes a number of issues such as a white screen flash when loading
* the GUI and the inability to set `cursor: pointer`. However, setting `setOffScreenRendering` to `true` on
* platform versions prior to 2023.3.4 causes larger issues such as an inability to type input for certain langauges,
* platform versions prior to 2023.3.4 causes larger issues such as an inability to type input for certain languages,
* e.g. Korean.
*
* References:
@ -293,12 +293,6 @@ class ContinueBrowser(val project: Project, url: String) {
* it's a simple integer without dot notation, making it easier to compare.
*/
private fun shouldRenderOffScreen(): Boolean {
// With the 0.0.77 release, non-Mac users have been reporting issues with paste functionality
// in the browser. Disabling OSR for all non-Mac users for now.
if (!SystemInfo.isMac) {
return false
}
val minBuildNumber = 233
val applicationInfo = ApplicationInfo.getInstance()
val currentBuildNumber = applicationInfo.build.baselineVersion

View file

@ -50,9 +50,11 @@ import {
getFontSize,
isJetBrains,
isMetaEquivalentKeyPressed,
isWebEnvironment,
} from "../../util";
import { handleMetaKeyPressJetBrains } from "../../util/handleMetaKeyPressJetBrains";
import {
handleJetBrainsMetaKeyPress,
handleMetaKeyPress,
} from "./handleMetaKeyPress";
import { CodeBlockExtension } from "./CodeBlockExtension";
import { SlashCommand } from "./CommandsExtension";
import InputToolbar, { ToolbarOptions } from "./InputToolbar";
@ -78,6 +80,7 @@ const InputBoxDiv = styled.div<{ border?: string }>`
props.border ? props.border : `0.5px solid ${vscInputBorder}`};
outline: none;
font-size: ${getFontSize()}px;
&:focus {
outline: none;
@ -561,7 +564,7 @@ function TipTapEditor(props: TipTapEditorProps) {
/**
* This handles various issues with meta key actions
* - In JetBrains, when using "off screen rendering", there is a bug where using the meta key to
* - In JetBrains, when using OSR in JCEF, there is a bug where using the meta key to
* highlight code using arrow keys is not working
* - In VS Code, while working with .ipynb files there is a problem where copy/paste/cut will affect
* the actual notebook cells, even when performing them in our GUI
@ -571,77 +574,33 @@ function TipTapEditor(props: TipTapEditorProps) {
setActiveKey(e.key);
if (isMetaEquivalentKeyPressed(e)) {
const { key, code } = e;
const isWebEnv = isWebEnvironment();
const text = editor.state.doc.textBetween(
editor.state.selection.from,
editor.state.selection.to,
);
if (isJetBrains()) {
if (code === "KeyJ") {
e.stopPropagation();
e.preventDefault();
setIgnoreHighlightedCode(true);
setTimeout(() => {
setIgnoreHighlightedCode(false);
}, 100);
}
if (isMetaEquivalentKeyPressed(e)) {
e.stopPropagation();
e.preventDefault();
handleMetaKeyPressJetBrains(e, text, editor.commands.setContent);
}
} else {
if (code === "KeyL") {
e.stopPropagation();
e.preventDefault();
setIgnoreHighlightedCode(true);
setTimeout(() => {
setIgnoreHighlightedCode(false);
}, 100);
return;
}
switch (key) {
case "x":
e.stopPropagation();
e.preventDefault();
if (isWebEnv) {
await navigator.clipboard.writeText(text);
editor.commands.deleteSelection();
} else {
document.execCommand("cut");
}
break;
case "c":
e.stopPropagation();
e.preventDefault();
if (isWebEnv) {
await navigator.clipboard.writeText(text);
} else {
document.execCommand("copy");
}
break;
case "v":
e.stopPropagation();
e.preventDefault();
if (isWebEnv) {
const clipboardText = await navigator.clipboard.readText();
editor.commands.insertContent(clipboardText);
} else {
document.execCommand("paste");
}
break;
}
}
} else if (e.key === "Escape") {
if (e.key === "Escape") {
e.stopPropagation();
e.preventDefault();
ideMessenger.post("focusEditor", undefined);
return;
}
if (!isMetaEquivalentKeyPressed(e)) {
return;
}
if (isJetBrains()) {
if (e.code === "KeyJ") {
setTemporaryIgnoreHighlightedCode();
} else {
handleJetBrainsMetaKeyPress(e, editor);
}
}
await handleMetaKeyPress(e, editor);
};
const setTemporaryIgnoreHighlightedCode = () => {
setIgnoreHighlightedCode(true);
setTimeout(() => {
setIgnoreHighlightedCode(false);
}, 100);
};
const handleKeyUp = () => {

View file

@ -0,0 +1,110 @@
import { KeyboardEvent } from "react";
import { isWebEnvironment } from "../../util";
import { Editor } from "@tiptap/react";
const isWebEnv = isWebEnvironment();
export const handleJetBrainsMetaKeyPress = (
e: KeyboardEvent,
editor: Editor,
) => {
const selection = window.getSelection();
const handlers: Record<string, () => void> = {
Backspace: () => handleJetBrainsMetaBackspace(editor),
ArrowLeft: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"backward",
"lineboundary",
),
ArrowRight: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"forward",
"lineboundary",
),
ArrowDown: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"forward",
"documentboundary",
),
ArrowUp: () => {
selection.modify(
e.shiftKey ? "extend" : "move",
"backward",
"documentboundary",
);
},
};
if (e.key in handlers) {
e.stopPropagation();
e.preventDefault();
handlers[e.key]();
}
};
export const handleMetaKeyPress = async (e: KeyboardEvent, editor: Editor) => {
const text = editor.state.doc.textBetween(
editor.state.selection.from,
editor.state.selection.to,
);
const handlers: Record<string, () => Promise<void>> = {
x: () => handleCutOperation(text, editor),
c: () => handleCopyOperation(text),
v: () => handlePasteOperation(editor),
};
if (e.key in handlers) {
e.stopPropagation();
e.preventDefault();
await handlers[e.key]();
}
};
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);
}
}
// Add an empty string so the user can keep typing
editor.commands.createParagraphNear();
};
export const handleCutOperation = async (text: string, editor: Editor) => {
const selection = window.getSelection();
selection.deleteFromDocument();
if (isWebEnv) {
await navigator.clipboard.writeText(text);
editor.commands.deleteSelection();
} else {
document.execCommand("cut");
}
};
export const handleCopyOperation = async (text: string) => {
if (isWebEnv) {
await navigator.clipboard.writeText(text);
} else {
document.execCommand("copy");
}
};
export const handlePasteOperation = async (editor: Editor) => {
if (isWebEnv) {
const clipboardText = await navigator.clipboard.readText();
editor.commands.insertContent(clipboardText);
} else {
document.execCommand("paste");
}
};

View file

@ -1,77 +0,0 @@
import { KeyboardEvent } from "react";
import { getPlatform } from ".";
export const handleMetaKeyPressJetBrains = (
e: KeyboardEvent,
text: string,
setText: (text: string) => void,
) => {
const { selectionStart, selectionEnd } = e.target as HTMLTextAreaElement;
if (e.key === "Backspace") {
handleMetaBackspace(e, text, setText, selectionStart, selectionEnd);
}
if (getPlatform() === "mac") {
if (e.key === "ArrowLeft") {
handleMacArrowLeftKey(e);
} else if (e.key === "ArrowRight") {
handleMacArrowRightKey(e);
}
}
};
const handleMetaBackspace = (
e: KeyboardEvent,
text: string,
setText: (text: string) => void,
start: number,
end: number,
) => {
const target = e.target as HTMLTextAreaElement;
let newStart = start;
if (start !== end) {
setText(text.slice(0, start) + text.slice(end));
} else {
newStart =
getPlatform() === "mac"
? text.lastIndexOf("\n", start - 1) + 1
: text.slice(0, start).trimEnd().lastIndexOf(" ") + 1;
setText(text.slice(0, newStart) + text.slice(start));
}
target.setSelectionRange(newStart, newStart);
};
const handleMacArrowLeftKey = (e: KeyboardEvent) => {
const target = e.target as HTMLDivElement;
const selection = window.getSelection();
const range = document.createRange();
if (e.shiftKey) {
selection.modify("extend", "backward", "character");
} else {
range.setStart(target.firstChild, 0);
range.setEnd(target.firstChild, 0);
selection.removeAllRanges();
selection.addRange(range);
}
};
const handleMacArrowRightKey = (e: KeyboardEvent) => {
const selection = window.getSelection();
const range = document.createRange();
const target = e.target as HTMLDivElement;
if (e.shiftKey) {
selection.modify("extend", "forward", "character");
} else {
range.selectNodeContents(target);
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
}
};