From 26e5762210fb7c4bdbb62ff137849dfe73a8cd4b Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Tue, 29 Oct 2024 16:15:46 +0000 Subject: [PATCH] fix: ! Prevent Tasks getting stuck 'Loading Tasks...' (again) Fixes #3156 Fixes #3153 Fixes #3157 This essentially moves the code from PR #1524 to an outer scope, so that it is also applied in the new parsing code for creating ListItem objects. --- src/Obsidian/Cache.ts | 68 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Obsidian/Cache.ts b/src/Obsidian/Cache.ts index 3f3387dd6d..454b45d694 100644 --- a/src/Obsidian/Cache.ts +++ b/src/Obsidian/Cache.ts @@ -43,43 +43,43 @@ export function getTasksFromFileContent2( let sectionIndex = 0; const line2ListItem: Map = new Map(); for (const listItem of listItems) { - if (listItem.task !== undefined) { - const lineNumber = listItem.position.start.line; - if (lineNumber >= linesInFile) { - /* - Obsidian CachedMetadata has told us that there is a task on lineNumber, but there are - not that many lines in the file. - - This was the underlying cause of all the 'Stuck on "Loading Tasks..."' messages, - as it resulted in the line 'undefined' being parsed. - - Somehow the file had been shortened whilst Obsidian was closed, meaning that - when Obsidian started up, it got the new file content, but still had the old cached - data about locations of list items in the file. - */ - logger.debug( - `${filePath} Obsidian gave us a line number ${lineNumber} past the end of the file. ${linesInFile}.`, - ); - return tasks; - } - if (currentSection === null || currentSection.position.end.line < lineNumber) { - // We went past the current section (or this is the first task). - // Find the section that is relevant for this task and the following of the same section. - currentSection = Cache.getSection(lineNumber, fileCache.sections); - sectionIndex = 0; - } + const lineNumber = listItem.position.start.line; + if (lineNumber >= linesInFile) { + /* + Obsidian CachedMetadata has told us that there is a task on lineNumber, but there are + not that many lines in the file. + + This was the underlying cause of all the 'Stuck on "Loading Tasks..."' messages, + as it resulted in the line 'undefined' being parsed. + + Somehow the file had been shortened whilst Obsidian was closed, meaning that + when Obsidian started up, it got the new file content, but still had the old cached + data about locations of list items in the file. + */ + logger.debug( + `${filePath} Obsidian gave us a line number ${lineNumber} past the end of the file. ${linesInFile}.`, + ); + return tasks; + } + if (currentSection === null || currentSection.position.end.line < lineNumber) { + // We went past the current section (or this is the first task). + // Find the section that is relevant for this task and the following of the same section. + currentSection = Cache.getSection(lineNumber, fileCache.sections); + sectionIndex = 0; + } - if (currentSection === null) { - // Cannot process a task without a section. - continue; - } + if (currentSection === null) { + // Cannot process a task without a section. + continue; + } - const line = fileLines[lineNumber]; - if (line === undefined) { - logger.debug(`${filePath}: line ${lineNumber} - ignoring 'undefined' line.`); - continue; - } + const line = fileLines[lineNumber]; + if (line === undefined) { + logger.debug(`${filePath}: line ${lineNumber} - ignoring 'undefined' line.`); + continue; + } + if (listItem.task !== undefined) { let task; try { task = Task.fromLine({