Skip to content

Commit

Permalink
Implement guard on task write to file
Browse files Browse the repository at this point in the history
Currently, the unique identifier (UID) for tasks
are a source file, a section, and an index into
the array of global-filter-matching tasks within
that section.

This diff does not change how tasks are uniquely
identified.

However, it does implement a guard when overwriting a
task in some file to ensure the line that is being
overwritten has similar content to the line that
was used to create the Task in the first place.

The world "similar" is used since the line is permitted
to have different content, so long as that
line represents a Task with identical data as per the
definition of Task.identicalTo
  • Loading branch information
BluBloos committed Feb 15, 2023
1 parent 0bd2d9a commit 97497a7
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { MetadataCache, TFile, Vault } from 'obsidian';
import type { ListItemCache } from 'obsidian';

import { getSettings } from './Config/Settings';
import type { Task } from './Task';
import { Task } from './Task';

import { DateFallback } from './DateFallback';
import { Lazy } from './lib/Lazy';

let metadataCache: MetadataCache | undefined;
let vault: Vault | undefined;
Expand All @@ -24,6 +27,10 @@ export const initializeFile = ({
* If you pass more than one replacement task, all subsequent tasks in the same
* section must be re-rendered, as their section indexes change. Assuming that
* this is done faster than user interaction in practice.
*
* In addition, this function is meant to be called with reasonable confidence
* that the {@code originalTask} is unmodified and at the exact same line in the
* source file it was originally found in. It will fail otherwise.
*/
export const replaceTaskWithTasks = async ({
originalTask,
Expand Down Expand Up @@ -125,10 +132,20 @@ const tryRepetitive = async ({
}

const line = fileLines[listItemCache.position.start.line];

if (line.includes(globalFilter)) {
if (sectionIndex === originalTask.sectionIndex) {
listItem = listItemCache;
const dateFromFileName = new Lazy(() => DateFallback.fromPath(originalTask.path));
const taskFromLine = Task.fromLine({
line,
path: originalTask.path,
precedingHeader: originalTask.precedingHeader,
sectionStart: originalTask.sectionStart,
sectionIndex: originalTask.sectionIndex,
fallbackDate: dateFromFileName.value,
});
if (taskFromLine?.identicalTo(originalTask) === true) {
listItem = listItemCache;
}
break;
}

Expand Down

0 comments on commit 97497a7

Please sign in to comment.