@@ -5,14 +5,19 @@ import { Task } from '@shared/types/task'
55
66const GLOBAL_TASKS_FILENAME = 'tasks.json'
77
8- export const get_tasks_file_path = ( context : vscode . ExtensionContext ) => {
9- return path . join ( context . globalStorageUri . fsPath , GLOBAL_TASKS_FILENAME )
8+ export const get_tasks_file_path = ( params : {
9+ context : vscode . ExtensionContext
10+ } ) => {
11+ return path . join (
12+ params . context . globalStorageUri . fsPath ,
13+ GLOBAL_TASKS_FILENAME
14+ )
1015}
1116
1217export const load_all_tasks = (
1318 context : vscode . ExtensionContext
1419) : Record < string , Task [ ] > => {
15- const file_path = get_tasks_file_path ( context )
20+ const file_path = get_tasks_file_path ( { context } )
1621 try {
1722 if ( fs . existsSync ( file_path ) ) {
1823 const content = fs . readFileSync ( file_path , 'utf8' )
@@ -24,19 +29,19 @@ export const load_all_tasks = (
2429 return { }
2530}
2631
27- export const save_all_tasks = (
28- context : vscode . ExtensionContext ,
32+ export const save_all_tasks = ( params : {
33+ context : vscode . ExtensionContext
2934 tasks : Record < string , Task [ ] >
30- ) => {
35+ } ) => {
3136 const filtered_tasks : Record < string , Task [ ] > = { }
3237
33- for ( const root in tasks ) {
38+ for ( const root in params . tasks ) {
3439 if ( fs . existsSync ( root ) ) {
35- filtered_tasks [ root ] = tasks [ root ]
40+ filtered_tasks [ root ] = params . tasks [ root ]
3641 }
3742 }
3843
39- const file_path = get_tasks_file_path ( context )
44+ const file_path = get_tasks_file_path ( { context : params . context } )
4045 const dir_path = path . dirname ( file_path )
4146
4247 if ( ! fs . existsSync ( dir_path ) ) {
@@ -50,69 +55,81 @@ export const save_all_tasks = (
5055 }
5156}
5257
53- export const delete_task_from_tree = (
54- tasks : Task [ ] ,
58+ export const delete_task_from_tree = ( params : {
59+ tasks : Task [ ]
5560 timestamp : number
56- ) : Task [ ] => {
57- return tasks
58- . filter ( ( t ) => t . created_at !== timestamp )
61+ } ) : Task [ ] => {
62+ return params . tasks
63+ . filter ( ( t ) => t . created_at !== params . timestamp )
5964 . map ( ( t ) => ( {
6065 ...t ,
61- children : t . children ? delete_task_from_tree ( t . children , timestamp ) : [ ]
66+ children : t . children
67+ ? delete_task_from_tree ( {
68+ tasks : t . children ,
69+ timestamp : params . timestamp
70+ } )
71+ : [ ]
6272 } ) )
6373}
6474
65- export const find_task_in_tree_with_location = (
66- tasks : Task [ ] ,
67- id : number ,
68- parent_id : number | null = null
69- ) : { task : Task ; parent_id : number | null ; index : number } | null => {
70- for ( let i = 0 ; i < tasks . length ; i ++ ) {
71- if ( tasks [ i ] . created_at === id ) {
72- return { task : tasks [ i ] , parent_id, index : i }
75+ export const find_task_in_tree_with_location = ( params : {
76+ tasks : Task [ ]
77+ id : number
78+ parent_id ?: number | null
79+ } ) : { task : Task ; parent_id : number | null ; index : number } | null => {
80+ const effective_parent_id =
81+ params . parent_id !== undefined ? params . parent_id : null
82+ for ( let i = 0 ; i < params . tasks . length ; i ++ ) {
83+ if ( params . tasks [ i ] . created_at === params . id ) {
84+ return { task : params . tasks [ i ] , parent_id : effective_parent_id , index : i }
7385 }
74- if ( tasks [ i ] . children ) {
75- const result = find_task_in_tree_with_location (
76- tasks [ i ] . children ! ,
77- id ,
78- tasks [ i ] . created_at
79- )
86+ if ( params . tasks [ i ] . children ) {
87+ const result = find_task_in_tree_with_location ( {
88+ tasks : params . tasks [ i ] . children ! ,
89+ id : params . id ,
90+ parent_id : params . tasks [ i ] . created_at
91+ } )
8092 if ( result ) return result
8193 }
8294 }
8395 return null
8496}
8597
86- export const insert_task_in_tree = (
87- tasks : Task [ ] ,
88- task : Task ,
89- parent_id : number | null ,
98+ export const insert_task_in_tree = ( params : {
99+ tasks : Task [ ]
100+ task : Task
101+ parent_id : number | null
90102 index : number
91- ) : { tasks : Task [ ] ; success : boolean } => {
92- if ( parent_id === null ) {
93- const new_tasks = [ ...tasks ]
94- if ( index >= 0 && index <= new_tasks . length ) {
95- new_tasks . splice ( index , 0 , task )
103+ } ) : { tasks : Task [ ] ; success : boolean } => {
104+ if ( params . parent_id === null ) {
105+ const new_tasks = [ ...params . tasks ]
106+ if ( params . index >= 0 && params . index <= new_tasks . length ) {
107+ new_tasks . splice ( params . index , 0 , params . task )
96108 } else {
97- new_tasks . push ( task )
109+ new_tasks . push ( params . task )
98110 }
99111 return { tasks : new_tasks , success : true }
100112 }
101113
102114 let success = false
103- const new_tasks = tasks . map ( ( t ) => {
104- if ( t . created_at === parent_id ) {
115+ const new_tasks = params . tasks . map ( ( t ) => {
116+ if ( t . created_at === params . parent_id ) {
105117 success = true
106118 const new_children = t . children ? [ ...t . children ] : [ ]
107- if ( index >= 0 && index <= new_children . length ) {
108- new_children . splice ( index , 0 , task )
119+ if ( params . index >= 0 && params . index <= new_children . length ) {
120+ new_children . splice ( params . index , 0 , params . task )
109121 } else {
110- new_children . push ( task )
122+ new_children . push ( params . task )
111123 }
112124 return { ...t , children : new_children }
113125 }
114126 if ( t . children ) {
115- const result = insert_task_in_tree ( t . children , task , parent_id , index )
127+ const result = insert_task_in_tree ( {
128+ tasks : t . children ,
129+ task : params . task ,
130+ parent_id : params . parent_id ,
131+ index : params . index
132+ } )
116133 if ( result . success ) {
117134 success = true
118135 return { ...t , children : result . tasks }
@@ -123,21 +140,21 @@ export const insert_task_in_tree = (
123140 return { tasks : new_tasks , success }
124141}
125142
126- export const mark_task_as_completed_if_matches_prompt = (
127- context : vscode . ExtensionContext ,
143+ export const mark_task_as_completed_if_matches_prompt = ( params : {
144+ context : vscode . ExtensionContext
128145 prompt_text : string
129- ) : Record < string , Task [ ] > | null => {
130- if ( ! prompt_text . trim ( ) ) return null
146+ } ) : Record < string , Task [ ] > | null => {
147+ if ( ! params . prompt_text . trim ( ) ) return null
131148
132- const all_tasks = load_all_tasks ( context )
149+ const all_tasks = load_all_tasks ( params . context )
133150 if ( Object . keys ( all_tasks ) . length == 0 ) return null
134151
135152 let file_changed = false
136153 const relevant_tasks : Record < string , Task [ ] > = { }
137154
138155 const process_tasks = ( tasks : Task [ ] ) => {
139156 for ( const task of tasks ) {
140- if ( ! task . is_checked && task . text . trim ( ) == prompt_text . trim ( ) ) {
157+ if ( ! task . is_checked && task . text . trim ( ) == params . prompt_text . trim ( ) ) {
141158 task . is_checked = true
142159 file_changed = true
143160 }
@@ -158,7 +175,7 @@ export const mark_task_as_completed_if_matches_prompt = (
158175 }
159176
160177 if ( file_changed ) {
161- save_all_tasks ( context , all_tasks )
178+ save_all_tasks ( { context : params . context , tasks : all_tasks } )
162179 return relevant_tasks
163180 }
164181
0 commit comments