Skip to content

Commit df2b66b

Browse files
authored
Merge pull request #3374 from obsidian-tasks-group/refactor
refactor: Finish implementing ListItem.fromListItemLine()
2 parents 34f3a09 + aec16d1 commit df2b66b

File tree

4 files changed

+100
-67
lines changed

4 files changed

+100
-67
lines changed

src/Obsidian/FileParser.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ export class FileParser {
158158
}
159159

160160
private createListItem(listItem: ListItemCache, line: string, lineNumber: number, taskLocation: TaskLocation) {
161-
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
162-
this.line2ListItem.set(lineNumber, ListItem.fromListItemLine(line, parentListItem, taskLocation));
161+
const parentListItem = this.line2ListItem.get(listItem.parent) ?? null;
162+
const newListItem = ListItem.fromListItemLine(line, parentListItem, taskLocation);
163+
if (newListItem === null) {
164+
// This should be unreachable.
165+
this.logger.warn(
166+
'Unexpected failure to create a list item from line: ' + line + ' in file: ' + this.filePath,
167+
);
168+
return;
169+
}
170+
this.line2ListItem.set(lineNumber, newListItem);
163171
}
164172
}

src/Task/ListItem.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,21 @@ export class ListItem {
4747
this.taskLocation = taskLocation;
4848
}
4949

50-
public static fromListItemLine(originalMarkdown: string, parent: ListItem | null, taskLocation: TaskLocation) {
50+
/**
51+
* Takes the given line from an Obsidian note and returns a ListItem object.
52+
*
53+
* @static
54+
* @param {string} originalMarkdown - The full line in the note to parse.
55+
* @param {ListItem | null} parent - The optional parent Task or ListItem of the new instance.
56+
* @param {TaskLocation} taskLocation - The location of the ListItem.
57+
* @return {ListItem | null}
58+
* @see Task.fromLine
59+
*/
60+
public static fromListItemLine(
61+
originalMarkdown: string,
62+
parent: ListItem | null,
63+
taskLocation: TaskLocation,
64+
): ListItem | null {
5165
const nonTaskMatch = RegExp(TaskRegularExpressions.nonTaskRegex).exec(originalMarkdown);
5266
let indentation = '';
5367
let listMarker = '';
@@ -207,13 +221,25 @@ export class ListItem {
207221
}
208222

209223
public checkOrUncheck(): ListItem {
224+
if (this.statusCharacter === null) {
225+
return this;
226+
}
227+
210228
const newStatusCharacter = this.statusCharacter === ' ' ? 'x' : ' ';
211229
const newMarkdown = this.originalMarkdown.replace(
212230
RegExp(TaskRegularExpressions.checkboxRegex),
213231
`[${newStatusCharacter}]`,
214232
);
215233

216-
return ListItem.fromListItemLine(newMarkdown, null, this.taskLocation);
234+
return new ListItem({
235+
...this,
236+
originalMarkdown: newMarkdown,
237+
statusCharacter: newStatusCharacter,
238+
// The purpose of this method is just to update the status character on one single line in the file.
239+
// This will trigger an update, making Cache re-read the whole file,
240+
// which will then identify and re-create any parent-child relationships.
241+
parent: null,
242+
});
217243
}
218244

219245
public toFileLineString(): string {

src/Task/Task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export class Task extends ListItem {
163163
* @param {(Moment | null)} fallbackDate - The date to use as the scheduled date if no other date is set
164164
* @return {*} {(Task | null)}
165165
* @see parseTaskSignifiers
166+
* @see ListItem.fromListItemLine
166167
*/
167168
public static fromLine({
168169
line,

0 commit comments

Comments
 (0)