Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 listContinuationForEnter,
5 listIndentForTab,
6 listOutdentForTab,
7 nextOrderedMarkerText,
8} from "./composer-list-continuation";
9
10function applyEdit(value: string, edit: { start: number; end: number; replacement: string }) {
11 return value.slice(0, edit.start) + edit.replacement + value.slice(edit.end);
12}
13
14describe("composer list continuation", () => {
15 it("continues bullets and keeps indentation", () => {
16 expect(applyEdit("- foo", listContinuationForEnter("- foo", 5)!)).toBe("- foo\n- ");
17 expect(applyEdit(" * foo", listContinuationForEnter(" * foo", 7)!)).toBe(" * foo\n * ");
18 });
19
20 it("continues tasks unchecked", () => {
21 expect(applyEdit("- [x] done", listContinuationForEnter("- [x] done", 10)!)).toBe(
22 "- [x] done\n- [ ] ",
23 );
24 });
25
26 it.each([
27 ["* [x] done", "* [x] done\n* [ ] "],
28 ["1. [x] done", "1. [x] done\n2. [ ] "],
29 ])("continues %j as an unchecked task with the same marker", (value, expected) => {
30 expect(applyEdit(value, listContinuationForEnter(value, value.length)!)).toBe(expected);
31 });
32
33 it("renumbers following items after a numbered task", () => {
34 const value = "1. [x] a\n2. [ ] b";
35 expect(applyEdit(value, listContinuationForEnter(value, "1. [x] a".length)!)).toBe(
36 "1. [x] a\n2. [ ] \n3. [ ] b",
37 );
38 });
39
40 it("splits mid-line items", () => {
41 expect(applyEdit("1. foobar", listContinuationForEnter("1. foobar", 5)!)).toBe(
42 "1. fo\n2. obar",
43 );
44 });
45
46 it("exits the list on an empty item", () => {
47 expect(applyEdit("1. foo\n2. ", listContinuationForEnter("1. foo\n2. ", 10)!)).toBe("1. foo\n");
48 expect(applyEdit("- ", listContinuationForEnter("- ", 2)!)).toBe("");
49 expect(applyEdit("1)\t", listContinuationForEnter("1)\t", 3)!)).toBe("");
50 expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
51 expect(applyEdit("* [ ] ", listContinuationForEnter("* [ ] ", 6)!)).toBe("");
52 expect(applyEdit("1. [x] a\n2. [ ] ", listContinuationForEnter("1. [x] a\n2. [ ] ", 16)!)).toBe(
53 "1. [x] a\n",
54 );
55 });
56
57 it("lifts an empty nested item one level per press before leaving the list", () => {
58 let value = "1. parent\n 1. child\n 1. ";
59 for (const expected of [
60 "1. parent\n 1. child\n 2. ",
61 "1. parent\n 1. child\n2. ",
62 "1. parent\n 1. child\n",
63 ]) {
64 value = applyEdit(value, listContinuationForEnter(value, value.length)!);
65 expect(value).toBe(expected);
66 }
67 });
68
69 it("renumbers the parent list after lifting an empty nested item", () => {
70 const value = "1. a\n 1. \n2. b";
71 const edit = listContinuationForEnter(value, value.indexOf("\n2."))!;
72 expect(applyEdit(value, edit)).toBe("1. a\n2. \n3. b");
73 expect(edit.cursorAfter).toBe("1. a\n2. ".length);
74 });
75
76 it("refuses to split a supplementary currency skill chip", () => {
77 const value = "1. 𑿝review go";
78 const cursor = value.indexOf(" go") - 1;
79 expect(listContinuationForEnter(value, cursor)).toBeNull();
80 });
81
82 it("refuses to split an inline chip", () => {
83 const value = "1. @README.md go";
84 const cursor = value.indexOf("README") + 2;
85 expect(listContinuationForEnter(value, cursor)).toBeNull();
86 });
87});
88
89describe("composer list indentation", () => {
90 // regression: #17124 Tab must nest a numbered item at its parent's content column
91 it.each([
92 ["1. parent\n2. child", "1. parent\n 1. child"],
93 ["10. parent\n11. child", "10. parent\n 1. child"],
94 ["1.\tparent\n2. child", "1.\tparent\n 1. child"],
95 ['1. @"file\n 9. name"\n2. child', '1. @"file\n 9. name"\n 1. child'],
96 ])("nests %j as a first child and keeps the caret on its text", (value, expected) => {
97 const cursor = value.lastIndexOf("child") + 2;
98 const edit = listIndentForTab(value, cursor, cursor)!;
99 expect(applyEdit(value, edit)).toBe(expected);
100 expect(edit.cursorAfter).toBe(expected.lastIndexOf("child") + 2);
101 });
102
103 it("continues the numbering of an existing nested sibling", () => {
104 const value = "1. parent\n 1. first\n2. second";
105 const edit = listIndentForTab(value, value.length, value.length)!;
106 expect(applyEdit(value, edit)).toBe("1. parent\n 1. first\n 2. second");
107 });
108
109 it.each([
110 ["1. a\n2. b\n3. c\n4. d", "1. a\n 1. b\n2. c\n3. d"],
111 ["1. a\n2. b\n3) c", "1. a\n 1. b\n3) c"],
112 ["1. a\n2. b\n - x\n3. c", "1. a\n 1. b\n - x\n2. c"],
113 ])("renumbers following siblings when indenting %j to %j", (value, expected) => {
114 const cursor = value.indexOf("b");
115 const edit = listIndentForTab(value, cursor, cursor)!;
116 expect(applyEdit(value, edit)).toBe(expected);
117 expect(edit.cursorAfter).toBe(expected.indexOf("b"));
118 });
119
120 it.each(["- parent\n- child", "1. parent\n2. child", "1. a\n2. child\n3. c", " 7. child"])(
121 "undoes Tab on %j with Shift+Tab and keeps the caret on its text",
122 (value) => {
123 const cursor = value.lastIndexOf("child") + 2;
124 const nest = listIndentForTab(value, cursor, cursor)!;
125 const nested = applyEdit(value, nest);
126 const edit = listOutdentForTab(nested, nest.cursorAfter, nest.cursorAfter)!;
127 expect(applyEdit(nested, edit)).toBe(value);
128 expect(edit.cursorAfter).toBe(cursor);
129 },
130 );
131
132 it.each([
133 ["1. a\n 1. b\n2. c", "1. a\n2. b\n3. c"],
134 ["- a\n 5. b", "- a\n5. b"],
135 ])("outdents %j to %j and renumbers only within the parent's list", (value, expected) => {
136 const edit = listOutdentForTab(value, value.indexOf("b"), value.indexOf("b"))!;
137 expect(applyEdit(value, edit)).toBe(expected);
138 });
139
140 it("leaves a top-level item alone", () => {
141 expect(listOutdentForTab("- item", 6, 6)).toBeNull();
142 });
143});
144
145describe("nextOrderedMarkerText", () => {
146 it.each([
147 ["1.", "2."],
148 ["1)", "2)"],
149 ["09.", "10."],
150 ["001)", "002)"],
151 ["99.", "100."],
152 ["99999999999999999999.", "99999999999999999999."],
153 ["999999999.", "999999999."],
154 ])("counts %s up to %s", (marker, expected) => {
155 expect(nextOrderedMarkerText(marker)).toBe(expected);
156 });
157});
158
159describe("composer list renumbering", () => {
160 it("keeps generated markers within Markdown's nine digits", () => {
161 expect(applyEdit("999999999. a", listContinuationForEnter("999999999. a", 12)!)).toBe(
162 "999999999. a\n999999999. ",
163 );
164 const value = "999999998. a\n999999999. b";
165 expect(applyEdit(value, listContinuationForEnter(value, value.indexOf("\n"))!)).toBe(
166 "999999998. a\n999999999. \n999999999. b",
167 );
168 });
169
170 it("renumbers following items with their padding and leaves the caret after the inserted marker", () => {
171 const value = "1. @README.md go\n2. two\n03. three\n10. four";
172 const edit = listContinuationForEnter(value, value.indexOf("go"))!;
173 expect(applyEdit(value, edit)).toBe("1. @README.md \n2. go\n3. two\n04. three\n5. four");
174 expect(edit.cursorAfter).toBe("1. @README.md \n2. ".length);
175 });
176
177 it("renumbers indented paren siblings across tab-indented children and stops at a parent", () => {
178 const value = " 1) a\n\t1) child\n 2) b\n1) parent\n 3) untouched";
179 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
180 expect(applyEdit(value, edit)).toBe(
181 " 1) a\n 2) \n\t1) child\n 3) b\n1) parent\n 3) untouched",
182 );
183 expect(edit.cursorAfter).toBe(" 1) a\n 2) ".length);
184 });
185
186 // regression: #14332 continuing a numbered list must renumber following items only up to the list boundary
187 it.each([
188 ["a blank line", ""],
189 ["plain text", "text"],
190 ["a different delimiter", "1) other list"],
191 ])("stops renumbering at %s", (_label, boundary) => {
192 const value = `1. a\n2. b\n${boundary}\n3. untouched`;
193 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
194 expect(applyEdit(value, edit)).toBe(`1. a\n2. \n3. b\n${boundary}\n3. untouched`);
195 });
196
197 it("preserves multiline chip contents while renumbering surrounding items", () => {
198 const value = '1. a\n2. @"file\n2. name" \n3. b';
199 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
200 expect(applyEdit(value, edit)).toBe('1. a\n2. \n3. @"file\n2. name" \n4. b');
201 });
202});