diff --git a/src/editor/core/cm/editing.ts b/src/editor/core/cm/editing.ts index 53533e435..46b4169f5 100644 --- a/src/editor/core/cm/editing.ts +++ b/src/editor/core/cm/editing.ts @@ -417,6 +417,79 @@ function leadingCommentStart(state: EditorState, a: number): number { export type StatementMove = { spec: TransactionSpec } | "noop" | "line"; +/** A `case` / `default` label whose branch body follows as its siblings (Go, JavaScript, Java). */ +const CASE_LABEL = /^(Case|CaseLabel|DefaultLabel|SwitchLabel)$/; + +/** + * Move Statement on a `case` label of a switch whose branches are flat + * (label, then the body as siblings): the branch, from its first stacked + * label to its last non-blank line, swaps with the neighbouring branch, as + * IDEA's case block mover does. `noop` at the first / last branch. + */ +function planCaseMove(state: EditorState, label: Node, up: boolean): StatementMove { + const doc = state.doc; + const block = label.parent!; + const close = doc.lineAt(block.to).number; + const isLabel = (l: number) => { + const n = statementStartingAt(state, l); + return !!n && CASE_LABEL.test(n.type.name) && n.parent?.from === block.from && n.parent.to === block.to; + }; + const start = (l: number) => { + while (l > 1 && isLabel(l - 1)) l--; + return l; + }; + // The line where the branch starting at `l` ends: its last non-blank line before the next branch or the closing brace. + const end = (l: number) => { + let k = l + 1; + while (k < close && isLabel(k)) k++; + while (k < close && !isLabel(k)) k++; + k--; + while (k > l && firstToken(state, k) === null) k--; + return k; + }; + const a = start(doc.lineAt(label.from).number); + const b = end(a); + let s: number; + let e: number; + if (up) { + let p = a - 1; + while (p > 0 && doc.line(p).from > block.from && !isLabel(p)) p--; + if (p < 1 || doc.line(p).from <= block.from || !isLabel(p)) return "noop"; + s = start(p); + e = end(s); + } else { + let n = b + 1; + while (n < close && !isLabel(n)) n++; + if (n >= close) return "noop"; + s = n; + e = end(n); + } + const [first, second] = up + ? [ + { s, e }, + { s: a, e: b }, + ] + : [ + { s: a, e: b }, + { s, e }, + ]; + const from = doc.line(first.s).from; + const to = doc.line(second.e).to; + const textFirst = state.sliceDoc(from, doc.line(first.e).to); + const textSecond = state.sliceDoc(doc.line(second.s).from, to); + const between = state.sliceDoc(doc.line(first.e).to, doc.line(second.s).from); + const head = state.selection.main.head; + const movedFrom = up ? from : from + textSecond.length + between.length; + return { + spec: { + changes: { from, to, insert: textSecond + between + textFirst }, + selection: EditorSelection.cursor(movedFrom + (head - doc.line(a).from)), + scrollIntoView: true, + userEvent: "move.line", + }, + }; +} + /** * Move Statement as in IDEA: the statement around the caret (whole lines) * moves past the next statement; into a block when the neighbour is a block @@ -429,6 +502,7 @@ export function planStatementMove(state: EditorState, up: boolean): StatementMov const head = state.selection.main.head; const cur = statementAround(state, head); if (!cur) return "line"; + if (CASE_LABEL.test(cur.type.name) && cur.parent) return planCaseMove(state, cur, up); const doc = state.doc; if (/^[{}]$/.test(cur.type.name)) { for (let g: Node | null = cur.parent; g; g = g.parent) diff --git a/src/editor/core/cm/ideaParity.test.ts b/src/editor/core/cm/ideaParity.test.ts index 82b17faa4..ed7b40503 100644 --- a/src/editor/core/cm/ideaParity.test.ts +++ b/src/editor/core/cm/ideaParity.test.ts @@ -67,6 +67,7 @@ function show(view: EditorView): string { const javascript = await languages.find((l) => l.name === "JavaScript")!.load(); const yaml = await languages.find((l) => l.name === "YAML")!.load(); +const goLang = await languages.find((l) => l.name === "Go")!.load(); describe("Move Statement", () => { it("moves a multi-line statement as a whole, never one of its lines", () => { @@ -94,6 +95,34 @@ describe("Move Statement", () => { expect(v.state.doc.toString()).toBe("function f() {\n a();\n}\nb();"); }); + it("moves a Go switch case with its whole body past the neighbouring case", () => { + const go = make( + "func f(n int) int {\n\tswitch n {\n\tcase 0:\n\t\treturn 0\n\tca|se 1:\n\t\tv := 1\n\t\treturn v\n\tdefault:\n\t\treturn -1\n\t}\n}", + goLang, + ); + moveStatementUp(go); + expect(show(go)).toBe( + "func f(n int) int {\n\tswitch n {\n\tca|se 1:\n\t\tv := 1\n\t\treturn v\n\tcase 0:\n\t\treturn 0\n\tdefault:\n\t\treturn -1\n\t}\n}", + ); + moveStatementUp(go); + expect(go.state.doc.toString()).toContain("switch n {\n\tcase 1:\n\t\tv := 1"); + moveStatementDown(go); + moveStatementDown(go); + expect(show(go)).toBe( + "func f(n int) int {\n\tswitch n {\n\tcase 0:\n\t\treturn 0\n\tdefault:\n\t\treturn -1\n\tca|se 1:\n\t\tv := 1\n\t\treturn v\n\t}\n}", + ); + moveStatementDown(go); + expect(go.state.doc.toString()).toContain("\t\treturn v\n\t}\n}"); + }); + + it("moves a JavaScript case with its stacked labels and blank line kept between branches", () => { + const v = make("switch (x) {\n case 1:\n a();\n break;\n\n case 2:\n |case 3:\n b();\n}", javascript); + moveStatementUp(v); + expect(v.state.doc.toString()).toBe( + "switch (x) {\n case 2:\n case 3:\n b();\n\n case 1:\n a();\n break;\n}", + ); + }); + it("passes a neighbouring function as a whole", () => { const v = make("|a();\nfunction f() {\n b();\n}", javascript); moveStatementDown(v);