Skip to content

Commit

Permalink
refactor: . Extract FileParser.parseLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
claremacrae committed Dec 6, 2024
1 parent e34cd28 commit 8d28482
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions src/Obsidian/FileParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,43 +102,54 @@ export class FileParser {
sectionIndex,
Cache.getPrecedingHeader(lineNumber, this.fileCache.headings),
);
if (listItem.task !== undefined) {
let task;
try {
task = Task.fromLine({
line,
taskLocation: taskLocation,
fallbackDate: this.dateFromFileName.value,
});

if (task !== null) {
// listItem.parent could be negative if the parent is not found (in other words, it is a root task).
// That is not a problem, as we never put a negative number in line2ListItem map, so parent will be null.
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
if (parentListItem !== null) {
task = new Task({
...task,
parent: parentListItem,
});
}

this.line2ListItem.set(lineNumber, task);
}
} catch (e) {
this.errorReporter(e, this.filePath, listItem, line);
continue;
}
sectionIndex = this.parseLine(listItem, line, taskLocation, lineNumber, sectionIndex);
}

return this.tasks;
}

private parseLine(
listItem: ListItemCache,
line: string,
taskLocation: TaskLocation,
lineNumber: number,
sectionIndex: number,
) {
if (listItem.task !== undefined) {
let task;
try {
task = Task.fromLine({
line,
taskLocation: taskLocation,
fallbackDate: this.dateFromFileName.value,
});

if (task !== null) {
sectionIndex++;
this.tasks.push(task);
// listItem.parent could be negative if the parent is not found (in other words, it is a root task).
// That is not a problem, as we never put a negative number in line2ListItem map, so parent will be null.
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
if (parentListItem !== null) {
task = new Task({
...task,
parent: parentListItem,
});
}

this.line2ListItem.set(lineNumber, task);
}
} else {
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
this.line2ListItem.set(lineNumber, new ListItem(this.fileLines[lineNumber], parentListItem));
} catch (e) {
this.errorReporter(e, this.filePath, listItem, line);
return sectionIndex;
}
}

return this.tasks;
if (task !== null) {
sectionIndex++;
this.tasks.push(task);
}
} else {
const parentListItem: ListItem | null = this.line2ListItem.get(listItem.parent) ?? null;
this.line2ListItem.set(lineNumber, new ListItem(this.fileLines[lineNumber], parentListItem));
}
return sectionIndex;
}
}

0 comments on commit 8d28482

Please sign in to comment.