Skip to content

Commit 9a297dc

Browse files
committed
Keep spaces after the caret out of a new list item
Enter in a list item moved the spaces after the caret into the new item: "1. one‸ two" gave "2. ‸ two". The old editor trimmed them. The move to CodeMirror lost that, and no test noticed. CodeMirror's insertNewlineContinueMarkup drops the spaces before the caret, but not those after it. Enter now deletes those first, then lets CodeMirror continue the list or quote. Layers map through the two steps one at a time. So a ghost that began with a space moves into the new item, not over the line break. - An empty item with spaces after the caret now ends the list, as in the old editor. - A caret right after a marker, as in "1.‸ a", keeps the space that ends the marker. - keys.test.ts has every old list test, plus cases for quotes, tabs, a ghost at the caret, undo, and a selection. - The fix-bug skill no longer calls this bug open. Two old renumbering cases stay out of the tests for now. Today's editor follows CommonMark there and renumbers further than the old one did. That choice is open.
1 parent bb65595 commit 9a297dc

3 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎.claude/skills/fix-bug/SKILL.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ description: Fixes a bug so it stays fixed, with a regression test that proves t
1010
2. **Reproduce it at the lowest layer that shows it.** A test on `EditorState` in
1111
`src/editor/*.test.ts` beats one through React. A test in the Rust file's `mod tests` beats the
1212
app. Run it: it must fail, for the reason in the report. Done when you have that failing test.
13-
- Example, for "Enter in a list item leaves a stray space", in `src/editor/keys.test.ts`. This
14-
bug is real and still open:
13+
- Example, for "Enter in a list item leaves a stray space", in `src/editor/keys.test.ts`:
1514
`expect(run(stateFor("1. one‸ two"), enter).text).toBe("1. one\n2. ‸two")`
1615
- Some bugs show only in WebKit: caret, IME, dead keys. Reproduce those in `pnpm tauri dev` and
1716
write the steps down. Still add a test for any part the editor state can show.

‎src/editor/keys.test.ts‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { redo, undo } from "@codemirror/commands";
12
import { EditorSelection, EditorState, StateCommand } from "@codemirror/state";
23
import { EditorView } from "@codemirror/view";
34
import { describe, expect, it } from "vitest";
4-
import { parsePage } from "./format";
5+
import { parsePage, serializePage } from "./format";
56
import { backspace, enter, setHeading, toggleBold, toggleItalic, toggleLink, toggleList } from "./keys";
67
import { layersOf } from "./layers";
78
import { createEditorState } from "./setup";
@@ -58,6 +59,87 @@ describe("Enter", () => {
5859
});
5960
});
6061

62+
describe("Enter in a list", () => {
63+
it("starts the next numbered item on the next line", () => {
64+
expect(run(stateFor("1. First list item‸"), enter).text).toBe("1. First list item\n2. ‸");
65+
});
66+
67+
it.each([
68+
["- a‸", "- a\n- ‸"],
69+
[" * a‸", " * a\n * ‸"],
70+
["9) a‸", "9) a\n10) ‸"],
71+
])("continues %j", (before, after) => {
72+
expect(run(stateFor(before), enter).text).toBe(after);
73+
});
74+
75+
it.each([
76+
["1. one‸ two", "1. one\n2. ‸two"],
77+
["- one ‸ two", "- one\n- ‸two"],
78+
["1. a‸ ", "1. a\n2. ‸"],
79+
["> a‸ b", "> a\n> ‸b"],
80+
["- one‸\t two", "- one\n- ‸two"],
81+
])("splits %j without stray spaces", (before, after) => {
82+
expect(run(stateFor(before), enter).text).toBe(after);
83+
});
84+
85+
it.each([
86+
["1.‸ a", "1.\n2. ‸ a"],
87+
["- [ ]‸ a", "- [ ]\n- [ ] ‸ a"],
88+
[">‸ a", ">\n> ‸ a"],
89+
])("keeps the space that ends the marker in %j", (before, after) => {
90+
expect(run(stateFor(before), enter).text).toBe(after);
91+
});
92+
93+
it("undoes a split in one step, apart from the typing before it", () => {
94+
const typed = stateFor("1. on‸ two").update({
95+
changes: { from: 5, insert: "e" },
96+
selection: EditorSelection.cursor(6),
97+
userEvent: "input.type",
98+
}).state;
99+
const split = run(typed, enter);
100+
expect(split.text).toBe("1. one\n2. ‸two");
101+
expect(run(split.state, undo).text).toBe("1. one‸ two");
102+
});
103+
104+
it("moves a ghost that starts at the caret into the new item", () => {
105+
const page = parsePage("1. Cut this{- , frankly,-} now.\n");
106+
const state = createEditorState(page.text, page).update({ selection: EditorSelection.cursor(11) }).state;
107+
const save = (s: EditorState) => serializePage({ text: s.doc.toString(), ...layersOf(s), overflow: "" });
108+
const split = run(state, enter).state;
109+
expect(save(split)).toBe("1. Cut this\n2. {-, frankly,-} now.\n");
110+
const undone = run(split, undo).state;
111+
expect(save(undone)).toBe("1. Cut this{- , frankly,-} now.\n");
112+
expect(save(run(undone, redo).state)).toBe("1. Cut this\n2. {-, frankly,-} now.\n");
113+
});
114+
115+
it("leaves a selection to the default Enter", () => {
116+
expect(enter({ state: stateFor("- one «two» three"), dispatch: () => {} })).toBe(false);
117+
});
118+
119+
it("renumbers the items below", () => {
120+
expect(run(stateFor("1. a‸\n2. b\n3. c"), enter).text).toBe("1. a\n2. ‸\n3. b\n4. c");
121+
expect(run(stateFor("8. a‸\n9. b"), enter).text).toBe("8. a\n9. ‸\n10. b");
122+
});
123+
124+
it("renumbers past nested lines and stops at the end of the list", () => {
125+
expect(run(stateFor("1. a‸\n - x\n2. b"), enter).text).toBe("1. a\n2. ‸\n - x\n3. b");
126+
expect(run(stateFor("1. a‸\n2) b"), enter).text).toBe("1. a\n2. ‸\n2) b");
127+
});
128+
129+
it("drops an empty item and leaves a paragraph to type in", () => {
130+
expect(run(stateFor("1. a\n2. ‸"), enter).text).toBe("1. a\n\n‸");
131+
expect(run(stateFor("1. a\n2. ‸ "), enter).text).toBe("1. a\n\n‸");
132+
expect(run(stateFor("- a\n- ‸ \n- b"), enter).text).toBe("- a\n\n‸\n\n- b");
133+
});
134+
135+
it("leaves plain text and the marker itself alone", () => {
136+
expect(run(stateFor("Just‸ text"), enter).text).toBe("Just\n\n‸text");
137+
expect(run(stateFor("1.5‸ million"), enter).text).toBe("1.5\n\n‸million");
138+
expect(run(stateFor("1‸. a"), enter).text).toBe("1‸. a");
139+
expect(enter({ state: stateFor("1‸. a"), dispatch: () => {} })).toBe(false);
140+
});
141+
});
142+
61143
describe("Backspace", () => {
62144
it("joins a paragraph to the one above", () => {
63145
expect(runView(stateFor("One.\n\n‸Two."), backspace)).toBe("One.‸Two.");

‎src/editor/keys.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function nodeAt(state: EditorState, pos: number, names: string[]): SyntaxNode |
2222

2323
const continueMarkup = insertNewlineContinueMarkupCommand({ nonTightLists: false });
2424
const EMPTY_ITEM = /^\s*(?:>\s*)*(?:[-*+]|\d+[.)])\s*(?:\[[ xX]\]\s*)?$/;
25+
/** Quote and list markup up to, not including, the space after it. */
26+
const BARE_MARKER = /^[\s>]*(?:(?:[-*+]|\d+[.)])(?:\s+\[[ xX]\])?)?$/;
2527

2628
/** Enter: continue a list or quote, a plain line in code, else a new paragraph. */
2729
export const enter: StateCommand = (target) => {
@@ -31,10 +33,15 @@ export const enter: StateCommand = (target) => {
3133
const range = state.selection.main;
3234
const line = state.doc.lineAt(range.from);
3335
if (nodeAt(state, head, ["ListItem", "Blockquote"])) {
36+
// CodeMirror drops the spaces before the caret, but not those after it. A
37+
// caret right after a marker, as in `1.‸ a`, keeps the space that ends it.
38+
let to = head;
39+
const bare = BARE_MARKER.test(state.doc.sliceString(line.from, head));
40+
while (!bare && to < line.to && /[ \t]/.test(state.doc.sliceString(to, to + 1))) to++;
3441
// An empty top-level item ends the list: drop it, and leave a paragraph to
3542
// type in, with blank lines around it. Items below become a list of their
3643
// own. A nested one goes to CodeMirror, which outdents it.
37-
if (range.empty && EMPTY_ITEM.test(line.text) && !/^\s/.test(line.text) && head === line.to) {
44+
if (range.empty && EMPTY_ITEM.test(line.text) && !/^\s/.test(line.text) && to === line.to) {
3845
const hasAfter = line.number < state.doc.lines && state.doc.line(line.number + 1).text.trim() !== "";
3946
dispatch(
4047
state.update({
@@ -46,7 +53,24 @@ export const enter: StateCommand = (target) => {
4653
);
4754
return true;
4855
}
49-
return continueMarkup(target);
56+
// Delete the spaces after the caret first, so the new item doesn't begin with one.
57+
if (to === head) return continueMarkup(target);
58+
const spaces = state.changes({ from: head, to });
59+
const trimmed = state.update({ changes: spaces }).state;
60+
return continueMarkup({
61+
state: trimmed,
62+
dispatch: (tr) =>
63+
dispatch(
64+
state.update({
65+
changes: spaces.compose(tr.changes),
66+
selection: tr.selection,
67+
// Mapped in two steps, so a ghost that began with those spaces starts in the new item.
68+
effects: setLayers.of(mapLayers(layersOf(trimmed), tr.changes)),
69+
scrollIntoView: true,
70+
userEvent: "input",
71+
}),
72+
),
73+
});
5074
}
5175

5276
if (!line.text.trim()) return insertNewline(target);

0 commit comments

Comments
 (0)