Repository: obsidian-tasks-group/obsidian-tasks
Pull request #1388 (2022-12-11 11:19:53)

refactor: Add new listMarker field to Task


<!--- Provide a general summary of your changes in the Title above -->

# Description

Add new listMarker field to Task

It is not yet parsed, but is used when converting tasks to markdown lines.

The behaviour is completely unchanged.

## Motivation and Context

This is in preparation for working on #1371:
'Tasks should retain knowledge of whether the task line starts with hyphen or asterisk'

## How has this been tested?

By running all the existing tests

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

Internal changes:

- [x] **Refactor** (prefix: `refactor` - non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [ ] My change requires a change to the documentation.
- [ ] I have [updated the documentation](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md#updating-documentation) accordingly.
- [x] My change has adequate [Unit Test coverage](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md#maintaining-the-tests).

## Terms

<!--
By submitting this pull request, you must agree to follow our
[contributing guide](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md) and
[Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md).
Put an x in the boxes to confirm you agree.
-->

- [x] My contribution follow this project's [contributing guide](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md)
- [x] I agree to follow this project's [Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md)



Patch:
diff --git a/src/Commands/CreateOrEdit.ts b/src/Commands/CreateOrEdit.ts
index 65b28e94..0aa2a5a1 100644
--- a/src/Commands/CreateOrEdit.ts
+++ b/src/Commands/CreateOrEdit.ts
@@ -66,6 +66,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
             description: '',
             path,
             indentation: '',
+            listMarker: '-',
             originalStatusCharacter: ' ',
             priority: Priority.None,
             startDate: null,
@@ -85,6 +86,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
     }
 
     const indentation: string = nonTaskMatch[1];
+    const listMarker = '-';
     const statusString: string = nonTaskMatch[3] ?? ' ';
     const status = statusString === ' ' ? Status.TODO : Status.DONE;
     let description: string = nonTaskMatch[4];
@@ -101,6 +103,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
         description,
         path,
         indentation,
+        listMarker,
         originalStatusCharacter: statusString,
         blockLink,
         priority: Priority.None,
diff --git a/src/Task.ts b/src/Task.ts
index 5dc43005..b009e78f 100644
--- a/src/Task.ts
+++ b/src/Task.ts
@@ -126,6 +126,7 @@ export class Task {
     public readonly description: string;
     public readonly path: string;
     public readonly indentation: string;
+    public readonly listMarker: string;
     /** Line number where the section starts that contains this task. */
     public readonly sectionStart: number;
     /** The index of the nth task in its section. */
@@ -165,6 +166,7 @@ export class Task {
         description,
         path,
         indentation,
+        listMarker,
         sectionStart,
         sectionIndex,
         originalStatusCharacter,
@@ -184,6 +186,7 @@ export class Task {
         description: string;
         path: string;
         indentation: string;
+        listMarker: string;
         sectionStart: number;
         sectionIndex: number;
         originalStatusCharacter: string;
@@ -203,6 +206,7 @@ export class Task {
         this.description = description;
         this.path = path;
         this.indentation = indentation;
+        this.listMarker = listMarker;
         this.sectionStart = sectionStart;
         this.sectionIndex = sectionIndex;
         this.originalStatusCharacter = originalStatusCharacter;
@@ -270,6 +274,7 @@ export class Task {
 
         let description = body;
         const indentation = regexMatch[1];
+        const listMarker = '-';
 
         // Get the status of the task, only todo and done supported.
         // But custom ones are retained and displayed as-is.
@@ -421,6 +426,7 @@ export class Task {
             description,
             path,
             indentation,
+            listMarker,
             sectionStart,
             sectionIndex,
             originalStatusCharacter: statusString,
@@ -600,7 +606,7 @@ export class Task {
      * @memberof Task
      */
     public toFileLineString(): string {
-        return `${this.indentation}- [${this.originalStatusCharacter}] ${this.toString()}`;
+        return `${this.indentation}${this.listMarker} [${this.originalStatusCharacter}] ${this.toString()}`;
     }
 
     /**
@@ -753,6 +759,7 @@ export class Task {
             'description',
             'path',
             'indentation',
+            'listMarker',
             'sectionStart',
             'sectionIndex',
             'originalStatusCharacter',


Test patch:
diff --git a/tests/Query.test.ts b/tests/Query.test.ts
index 5395a376..d8aa880d 100644
--- a/tests/Query.test.ts
+++ b/tests/Query.test.ts
@@ -255,6 +255,7 @@ describe('Query', () => {
                     description: 'description',
                     path: 'Ab/C D',
                     indentation: '',
+                    listMarker: '-',
                     sectionStart: 0,
                     sectionIndex: 0,
                     originalStatusCharacter: ' ',
@@ -275,6 +276,7 @@ describe('Query', () => {
                     description: 'description',
                     path: 'FF/C D',
                     indentation: '',
+                    listMarker: '-',
                     sectionStart: 0,
                     sectionIndex: 0,
                     originalStatusCharacter: ' ',
diff --git a/tests/Task.test.ts b/tests/Task.test.ts
index b14215e2..c449c013 100644
--- a/tests/Task.test.ts
+++ b/tests/Task.test.ts
@@ -404,6 +404,12 @@ describe('to string', () => {
         expect(task).not.toBeNull();
         expect(task.toFileLineString()).toStrictEqual(line);
     });
+
+    it('retains the asterisk', () => {
+        const task = new TaskBuilder().listMarker('*').build();
+        expect(task.toFileLineString()).toStrictEqual('* [ ] my description');
+    });
+
     it('retains the block link', () => {
         // Arrange
         const line = '- [ ] this is a task 📅 2021-09-12 ^my-precious';
@@ -877,6 +883,12 @@ describe('identicalTo', () => {
         expect(lhs).not.toBeIdenticalTo(new TaskBuilder().indentation('    '));
     });
 
+    it('should check listMarker', () => {
+        const lhs = new TaskBuilder().listMarker('*');
+        expect(lhs).toBeIdenticalTo(new TaskBuilder().listMarker('*'));
+        expect(lhs).not.toBeIdenticalTo(new TaskBuilder().listMarker('-'));
+    });
+
     it('should check sectionStart', () => {
         const lhs = new TaskBuilder().sectionStart(0);
         expect(lhs).toBeIdenticalTo(new TaskBuilder().sectionStart(0));
diff --git a/tests/TestingTools/TaskBuilder.ts b/tests/TestingTools/TaskBuilder.ts
index afc8abdb..e94085ad 100644
--- a/tests/TestingTools/TaskBuilder.ts
+++ b/tests/TestingTools/TaskBuilder.ts
@@ -21,6 +21,7 @@ export class TaskBuilder {
     private _description: string = 'my description';
     private _path: string = '';
     private _indentation: string = '';
+    private _listMarker: string = '-';
 
     private _sectionStart: number = 0;
     private _sectionIndex: number = 0;
@@ -62,6 +63,7 @@ export class TaskBuilder {
             description: description,
             path: this._path,
             indentation: this._indentation,
+            listMarker: this._listMarker,
             sectionStart: this._sectionStart,
             sectionIndex: this._sectionIndex,
             originalStatusCharacter: this._originalStatusCharacter,
@@ -116,6 +118,11 @@ export class TaskBuilder {
         return this;
     }
 
+    public listMarker(listMarker: string): TaskBuilder {
+        this._listMarker = listMarker;
+        return this;
+    }
+
     public sectionStart(sectionStart: number): TaskBuilder {
         this._sectionStart = sectionStart;
         return this;