Skip to content

Commit 0bd5771

Browse files
authored
Merge pull request #3328 from ilandikov/refactor-list-item-getters
refactor: make ListItem class more similar to Task
2 parents bed959b + 5efefb8 commit 0bd5771

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

src/Task/ListItem.ts

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { TasksFile } from '../Scripting/TasksFile';
2+
import type { Task } from './Task';
13
import type { TaskLocation } from './TaskLocation';
24
import { TaskRegularExpressions } from './TaskRegularExpressions';
3-
import type { Task } from './Task';
45

56
export class ListItem {
67
// The original line read from file.
@@ -94,12 +95,24 @@ export class ListItem {
9495
return false;
9596
}
9697

97-
if (this.originalMarkdown !== other.originalMarkdown) {
98-
return false;
98+
// Note: sectionStart changes every time a line is added or deleted before
99+
// any of the tasks in a file. This does mean that redrawing of tasks blocks
100+
// happens more often than is ideal.
101+
const args: Array<keyof ListItem> = [
102+
'originalMarkdown',
103+
'description',
104+
'statusCharacter',
105+
'path',
106+
'lineNumber',
107+
'sectionStart',
108+
'sectionIndex',
109+
'precedingHeader',
110+
];
111+
112+
for (const el of args) {
113+
if (this[el]?.toString() !== other[el]?.toString()) return false;
99114
}
100115

101-
// Not testing status character as it is implied from the original markdown
102-
103116
return ListItem.listsAreIdentical(this.children, other.children);
104117
}
105118

@@ -123,4 +136,40 @@ export class ListItem {
123136

124137
return list1.every((item, index) => item.identicalTo(list2[index]));
125138
}
139+
140+
public get path(): string {
141+
return this.taskLocation.path;
142+
}
143+
144+
public get file(): TasksFile {
145+
return this.taskLocation.tasksFile;
146+
}
147+
148+
/**
149+
* Return the name of the file containing this object, with the .md extension removed.
150+
*/
151+
public get filename(): string | null {
152+
const fileNameMatch = this.path.match(/([^/]+)\.md$/);
153+
if (fileNameMatch !== null) {
154+
return fileNameMatch[1];
155+
} else {
156+
return null;
157+
}
158+
}
159+
160+
public get lineNumber(): number {
161+
return this.taskLocation.lineNumber;
162+
}
163+
164+
public get sectionStart(): number {
165+
return this.taskLocation.sectionStart;
166+
}
167+
168+
public get sectionIndex(): number {
169+
return this.taskLocation.sectionIndex;
170+
}
171+
172+
public get precedingHeader(): string | null {
173+
return this.taskLocation.precedingHeader;
174+
}
126175
}

src/Task/Task.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Status } from '../Statuses/Status';
66
import { compareByDate } from '../DateTime/DateTools';
77
import { TasksDate } from '../DateTime/TasksDate';
88
import { StatusType } from '../Statuses/StatusConfiguration';
9-
import type { TasksFile } from '../Scripting/TasksFile';
109
import { PriorityTools } from '../lib/PriorityTools';
1110
import { logging } from '../lib/logging';
1211
import { logEndOfTaskEdit, logStartOfTaskEdit } from '../lib/LogTasksHelper';
@@ -46,8 +45,6 @@ export class Task extends ListItem {
4645
public readonly indentation: string;
4746
public readonly listMarker: string;
4847

49-
public readonly taskLocation: TaskLocation;
50-
5148
public readonly tags: string[];
5249

5350
public readonly priority: Priority;
@@ -126,7 +123,6 @@ export class Task extends ListItem {
126123
this.description = description;
127124
this.indentation = indentation;
128125
this.listMarker = listMarker;
129-
this.taskLocation = taskLocation;
130126

131127
this.tags = tags;
132128

@@ -619,10 +615,6 @@ export class Task extends ListItem {
619615
return this._urgency;
620616
}
621617

622-
public get path(): string {
623-
return this.taskLocation.path;
624-
}
625-
626618
/**
627619
* Return {@link cancelledDate} as a {@link TasksDate}, so the field names in scripting docs are consistent with the existing search instruction names, and null values are easy to deal with.
628620
*/
@@ -729,38 +721,6 @@ export class Task extends ListItem {
729721
return this.precedingHeader !== null;
730722
}
731723

732-
public get file(): TasksFile {
733-
return this.taskLocation.tasksFile;
734-
}
735-
736-
/**
737-
* Return the name of the file containing the task, with the .md extension removed.
738-
*/
739-
public get filename(): string | null {
740-
const fileNameMatch = this.path.match(/([^/]+)\.md$/);
741-
if (fileNameMatch !== null) {
742-
return fileNameMatch[1];
743-
} else {
744-
return null;
745-
}
746-
}
747-
748-
get lineNumber(): number {
749-
return this.taskLocation.lineNumber;
750-
}
751-
752-
get sectionStart(): number {
753-
return this.taskLocation.sectionStart;
754-
}
755-
756-
get sectionIndex(): number {
757-
return this.taskLocation.sectionIndex;
758-
}
759-
760-
public get precedingHeader(): string | null {
761-
return this.taskLocation.precedingHeader;
762-
}
763-
764724
/**
765725
* Returns the text that should be displayed to the user when linking to the origin of the task
766726
*
@@ -817,13 +777,8 @@ export class Task extends ListItem {
817777
// happens more often than is ideal.
818778
let args: Array<keyof Task> = [
819779
'description',
820-
'path',
821780
'indentation',
822781
'listMarker',
823-
'lineNumber',
824-
'sectionStart',
825-
'sectionIndex',
826-
'precedingHeader',
827782
'priority',
828783
'blockLink',
829784
'scheduledDateIsInferred',

tests/Task/ListItem.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('identicalTo', () => {
173173
expect(listItem1.identicalTo(listItem2)).toEqual(true);
174174
});
175175

176-
it('should test different markdown', () => {
176+
it('should recognise different descriptions', () => {
177177
const listItem1 = new ListItem('- description', null, taskLocation);
178178
const listItem2 = new ListItem('- description two', null, taskLocation);
179179
expect(listItem1.identicalTo(listItem2)).toEqual(false);
@@ -211,6 +211,13 @@ describe('identicalTo', () => {
211211

212212
expect(listItem.identicalTo(task)).toEqual(false);
213213
});
214+
215+
it('should recognise different path', () => {
216+
const item1 = new ListItem('- same', null, TaskLocation.fromUnknownPosition(new TasksFile('anything.md')));
217+
const item2 = new ListItem('- same', null, TaskLocation.fromUnknownPosition(new TasksFile('something.md')));
218+
219+
expect(item2.identicalTo(item1)).toEqual(false);
220+
});
214221
});
215222

216223
describe('checking if list item lists are identical', () => {

0 commit comments

Comments
 (0)