дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-21 17:10:45 +02:00
Порівняти коміти
1
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
e9b11126b2 |
No files matched your search
@@ -81,6 +81,11 @@ export function literalRanges(state: EditorState): readonly LiteralRange[] {
|
||||
|
||||
/** What `pos` lies in: a comment, a string literal or plain code (`null`). */
|
||||
export function literalKindAt(state: EditorState, pos: number): LiteralKind | null {
|
||||
return literalAt(state, pos)?.kind ?? null;
|
||||
}
|
||||
|
||||
/** The comment or string literal `pos` lies in, if any. */
|
||||
export function literalAt(state: EditorState, pos: number): LiteralRange | null {
|
||||
const ranges = literalRanges(state);
|
||||
let lo = 0;
|
||||
let hi = ranges.length - 1;
|
||||
@@ -89,7 +94,7 @@ export function literalKindAt(state: EditorState, pos: number): LiteralKind | nu
|
||||
const r = ranges[mid]!;
|
||||
if (pos < r.from) hi = mid - 1;
|
||||
else if (pos >= r.to) lo = mid + 1;
|
||||
else return r.kind;
|
||||
else return r;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -51,7 +51,14 @@ import {
|
||||
import { enterInLineComment, lineCommentStyle, type LineCommentStyle } from "./comments";
|
||||
import { enterInBlockComment, enterInDocComment } from "./docComment";
|
||||
import { renderDocComments } from "./renderedDocs";
|
||||
import { skipClosingQuote, smartAngleBrackets, smartBackspace, smartBackspaceApplies, tabOut } from "./smartKeys";
|
||||
import {
|
||||
enterInStringLiteral,
|
||||
skipClosingQuote,
|
||||
smartAngleBrackets,
|
||||
smartBackspace,
|
||||
smartBackspaceApplies,
|
||||
tabOut,
|
||||
} from "./smartKeys";
|
||||
import { linkedTags } from "./linkedTags";
|
||||
import { stringClipboard } from "./stringClipboard";
|
||||
import { filterCopiedText } from "./clipboard";
|
||||
@@ -684,7 +691,12 @@ export function prefsExtension(o: PrefsOptions): Extension {
|
||||
{ mac: "Mod-ArrowLeft", run: cursorLineStart, shift: selectLineStart, preventDefault: true },
|
||||
);
|
||||
if (!o.readOnly && k.insertDocStub) keys.push({ key: "Enter", run: enterInDocComment(o.language) });
|
||||
if (!o.readOnly) keys.push({ key: "Enter", run: enterInBlockComment }, { key: "Enter", run: enterInLineComment });
|
||||
if (!o.readOnly)
|
||||
keys.push(
|
||||
{ key: "Enter", run: enterInBlockComment },
|
||||
{ key: "Enter", run: enterInLineComment },
|
||||
{ key: "Enter", run: enterInStringLiteral(o.language ?? "") },
|
||||
);
|
||||
if (!k.smartIndent) keys.push({ key: "Enter", run: insertNewlineKeepIndent });
|
||||
if (k.backspaceUnindent === "none") keys.push({ key: "Backspace", run: deleteCharBackwardStrict });
|
||||
else if (smartBackspaceApplies(k.backspaceUnindent, o.language)) keys.push({ key: "Backspace", run: smartBackspace });
|
||||
|
||||
@@ -10,6 +10,7 @@ import { kotlin, csharp } from "@codemirror/legacy-modes/mode/clike";
|
||||
import { validateEditorExtras } from "@/platform/editorSettings";
|
||||
import {
|
||||
autoIndentBackspace,
|
||||
enterInStringLiteral,
|
||||
insertTab,
|
||||
smartBackspace,
|
||||
smartBackspaceApplies,
|
||||
@@ -72,6 +73,36 @@ function show(view: EditorView): string {
|
||||
return out;
|
||||
}
|
||||
|
||||
describe("Enter in a string literal", () => {
|
||||
const enter = (text: string, language: string, ext: Extension) => {
|
||||
const v = make(text, ext, " ");
|
||||
const handled = enterInStringLiteral(language)(v);
|
||||
return handled ? show(v) : null;
|
||||
};
|
||||
|
||||
it("splits a Java string into two joined with +, keeping the value", () => {
|
||||
expect(enter('class A {\n String t = "hello|world";\n}', "java", java())).toBe(
|
||||
'class A {\n String t = "hello" +\n "|world";\n}',
|
||||
);
|
||||
});
|
||||
|
||||
it("uses the literal's own quote in JavaScript", () => {
|
||||
expect(enter("const t = 'ab|cd';", "javascript", javascript())).toBe("const t = 'ab' +\n '|cd';");
|
||||
});
|
||||
|
||||
it("leaves text blocks, escapes, other languages and positions outside the literal alone", () => {
|
||||
expect(enter('String t = """\n a|b\n """;', "java", java())).toBeNull();
|
||||
expect(enter('String t = "a\\|nb";', "java", java())).toBeNull();
|
||||
expect(enter('String t = |"ab";', "java", java())).toBeNull();
|
||||
expect(enter('t = "a|b"', "python", [])).toBeNull();
|
||||
expect(enter("char c = 'a|';", "java", java())).toBeNull();
|
||||
});
|
||||
|
||||
it("splits after an escaped backslash", () => {
|
||||
expect(enter('String t = "a\\\\|b";', "java", java())).toBe('String t = "a\\\\" +\n "|b";');
|
||||
});
|
||||
});
|
||||
|
||||
describe("tab", () => {
|
||||
it("inserts whitespace up to the next indent stop at each caret", () => {
|
||||
const v = make("ab|c\n|x");
|
||||
|
||||
@@ -14,7 +14,9 @@ import {
|
||||
import { EditorView, keymap, type Command } from "@codemirror/view";
|
||||
import { getIndentUnit, getIndentation, indentString, indentUnit, syntaxTree } from "@codemirror/language";
|
||||
import { indentMore } from "@codemirror/commands";
|
||||
import type { SyntaxNode } from "@lezer/common";
|
||||
import { visibleColumn } from "./visibleColumn";
|
||||
import { literalAt } from "./literals";
|
||||
|
||||
// --- Tab -------------------------------------------------------------------------------
|
||||
|
||||
@@ -387,3 +389,60 @@ export function smartAngleBrackets(language: string | undefined): Extension {
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
/** Languages that join string literals with `+`. */
|
||||
const PLUS_CONCAT_LANGS = new Set([
|
||||
"java",
|
||||
"csharp",
|
||||
"kotlin",
|
||||
"groovy",
|
||||
"javascript",
|
||||
"javascriptreact",
|
||||
"typescript",
|
||||
"typescriptreact",
|
||||
]);
|
||||
|
||||
/** The one-line string literal around `pos` (parse tree, else the highlighted literals). */
|
||||
function stringLiteralAt(state: EditorState, pos: number): { from: number; to: number } | null {
|
||||
for (let n: SyntaxNode | null = syntaxTree(state).resolveInner(pos, -1); n; n = n.parent)
|
||||
if (/^(StringLiteral|String)$/.test(n.name)) return { from: n.from, to: n.to };
|
||||
const lit = literalAt(state, pos);
|
||||
return lit?.kind === "string" ? lit : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter inside a string literal splits it into two joined with `+` (`"hello" +` / `"world"`),
|
||||
* keeping the value, as in IDEA; text blocks, template literals and other languages get a plain newline.
|
||||
*/
|
||||
export function enterInStringLiteral(language: string): Command {
|
||||
return (view) => {
|
||||
if (!PLUS_CONCAT_LANGS.has(language)) return false;
|
||||
const { state } = view;
|
||||
const sel = state.selection.main;
|
||||
if (!sel.empty || state.selection.ranges.length > 1) return false;
|
||||
const pos = sel.head;
|
||||
const lit = stringLiteralAt(state, pos);
|
||||
if (!lit) return false;
|
||||
const text = state.sliceDoc(lit.from, lit.to);
|
||||
const quote = text[0];
|
||||
const singleQuoted = quote === "'" && language !== "java" && language !== "csharp" && language !== "kotlin";
|
||||
if (!(quote === '"' || singleQuoted) || text.startsWith('"""')) return false;
|
||||
// Strictly between the quotes of a literal on one line, and not inside an escape.
|
||||
if (pos <= lit.from || pos >= lit.to || !text.endsWith(quote) || text.length < 2) return false;
|
||||
if (state.doc.lineAt(lit.from).number !== state.doc.lineAt(lit.to).number) return false;
|
||||
const before = state.sliceDoc(lit.from + 1, pos);
|
||||
if (/(^|[^\\])(\\\\)*\\$/.test(before)) return false;
|
||||
// The continuation indent: two levels in the JVM languages and C#, one in JavaScript (IDEA's defaults).
|
||||
const unit = indentString(state, getIndentUnit(state));
|
||||
const base = /^\s*/.exec(state.doc.lineAt(pos).text)![0];
|
||||
const indent = base + (language.startsWith("javascript") || language.startsWith("typescript") ? unit : unit + unit);
|
||||
const insert = `${quote} +${state.lineBreak}${indent}${quote}`;
|
||||
view.dispatch({
|
||||
changes: { from: pos, insert },
|
||||
selection: { anchor: pos + insert.length },
|
||||
scrollIntoView: true,
|
||||
userEvent: "input",
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача