Skip to content

Commit a1be7ea

Browse files
authored
Merge pull request #3370 from ilandikov/refactor-list-item-construction
refactor: `ListItem` construction
2 parents 5da56b2 + e95a757 commit a1be7ea

File tree

6 files changed

+127
-75
lines changed

6 files changed

+127
-75
lines changed

src/Obsidian/FileParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ export class FileParser {
159159

160160
private createListItem(listItem: ListItemCache, line: string, lineNumber: number, taskLocation: TaskLocation) {
161161
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
162-
this.line2ListItem.set(lineNumber, new ListItem(line, parentListItem, taskLocation));
162+
this.line2ListItem.set(lineNumber, ListItem.fromListItemLine(line, parentListItem, taskLocation));
163163
}
164164
}

src/Task/ListItem.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,70 @@ export class ListItem {
77
// The original line read from file.
88
public readonly originalMarkdown: string;
99

10-
public readonly parent: ListItem | null = null;
10+
public readonly parent: ListItem | null;
1111
public readonly children: ListItem[] = [];
12-
public readonly indentation: string = '';
13-
public readonly listMarker: string = '';
12+
public readonly indentation: string;
13+
public readonly listMarker: string;
1414
public readonly description: string;
15-
public readonly statusCharacter: string | null = null;
15+
public readonly statusCharacter: string | null;
1616

1717
public readonly taskLocation: TaskLocation;
1818

19-
constructor(originalMarkdown: string, parent: ListItem | null, taskLocation: TaskLocation) {
20-
this.description = originalMarkdown.replace(TaskRegularExpressions.listItemRegex, '').trim();
21-
const nonTaskMatch = RegExp(TaskRegularExpressions.nonTaskRegex).exec(originalMarkdown);
22-
if (nonTaskMatch) {
23-
this.indentation = nonTaskMatch[1];
24-
this.listMarker = nonTaskMatch[2];
25-
this.description = nonTaskMatch[5].trim();
26-
this.statusCharacter = nonTaskMatch[4] ?? null;
27-
}
19+
constructor({
20+
originalMarkdown,
21+
indentation,
22+
listMarker,
23+
statusCharacter,
24+
description,
25+
parent,
26+
taskLocation,
27+
}: {
28+
originalMarkdown: string;
29+
indentation: string;
30+
listMarker: string;
31+
statusCharacter: string | null;
32+
description: string;
33+
parent: ListItem | null;
34+
taskLocation: TaskLocation;
35+
}) {
36+
this.indentation = indentation;
37+
this.listMarker = listMarker;
38+
this.statusCharacter = statusCharacter;
39+
this.description = description;
2840
this.originalMarkdown = originalMarkdown;
29-
this.parent = parent;
3041

42+
this.parent = parent;
3143
if (parent !== null) {
3244
parent.children.push(this);
3345
}
3446

3547
this.taskLocation = taskLocation;
3648
}
3749

50+
public static fromListItemLine(originalMarkdown: string, parent: ListItem | null, taskLocation: TaskLocation) {
51+
const nonTaskMatch = RegExp(TaskRegularExpressions.nonTaskRegex).exec(originalMarkdown);
52+
let indentation = '';
53+
let listMarker = '';
54+
let statusCharacter = null;
55+
let description = '';
56+
if (nonTaskMatch) {
57+
indentation = nonTaskMatch[1];
58+
listMarker = nonTaskMatch[2];
59+
statusCharacter = nonTaskMatch[4] ?? null;
60+
description = nonTaskMatch[5].trim();
61+
}
62+
63+
return new ListItem({
64+
originalMarkdown,
65+
indentation,
66+
listMarker,
67+
statusCharacter,
68+
description,
69+
taskLocation,
70+
parent,
71+
});
72+
}
73+
3874
/**
3975
* Return the top-level parent of this list item or task,
4076
* which will not be indented.
@@ -184,7 +220,7 @@ export class ListItem {
184220
`[${newStatusCharacter}]`,
185221
);
186222

187-
return new ListItem(newMarkdown, null, this.taskLocation);
223+
return ListItem.fromListItemLine(newMarkdown, null, this.taskLocation);
188224
}
189225

190226
public toFileLineString(): string {

src/Task/Task.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,15 @@ export class Task extends ListItem {
117117
scheduledDateIsInferred: boolean;
118118
parent?: ListItem | null;
119119
}) {
120-
super(originalMarkdown, parent, taskLocation);
120+
super({
121+
originalMarkdown,
122+
indentation,
123+
listMarker,
124+
statusCharacter: status.symbol,
125+
description,
126+
taskLocation,
127+
parent,
128+
});
121129
// NEW_TASK_FIELD_EDIT_REQUIRED
122130
this.status = status;
123131
this.description = description;

0 commit comments

Comments
 (0)