@@ -364,10 +364,14 @@ export function apply_context_command(
364364 value : 'clipboard' | 'internal' | 'file'
365365 } ) [ ] = [
366366 {
367- label : 'Find file paths in the clipboard text' ,
368- description : 'Useful when asking AI for a list of relevant files ' ,
367+ label : 'Paths from clipboard text' ,
368+ description : 'utility ' ,
369369 value : 'clipboard'
370- }
370+ } ,
371+ {
372+ label : 'Save location' ,
373+ kind : vscode . QuickPickItemKind . Separator
374+ } as any
371375 ]
372376
373377 if ( internal_contexts . length > 0 ) {
@@ -541,6 +545,7 @@ export function apply_context_command(
541545 }
542546
543547 let is_showing_dialog = false
548+ let go_back_after_delete = false
544549 const quick_pick_promise = new Promise <
545550 | ( vscode . QuickPickItem & {
546551 context ?: SavedContext
@@ -568,6 +573,10 @@ export function apply_context_command(
568573 if ( is_showing_dialog ) {
569574 return
570575 }
576+ if ( go_back_after_delete ) {
577+ resolve ( { label : BACK_LABEL } )
578+ return
579+ }
571580 if ( ! is_accepted ) {
572581 resolve ( undefined )
573582 }
@@ -703,9 +712,6 @@ export function apply_context_command(
703712 }
704713
705714 if ( context_updated ) {
706- vscode . window . showInformationMessage (
707- `Renamed context to "${ trimmed_name } ".`
708- )
709715 await extension_context . workspaceState . update (
710716 last_selected_context_name_key ,
711717 trimmed_name
@@ -730,107 +736,162 @@ export function apply_context_command(
730736 }
731737
732738 if ( event . button === delete_button ) {
733- is_showing_dialog = true
734- const confirm_delete = await vscode . window . showWarningMessage (
735- dictionary . warning_message . CONFIRM_DELETE_CONTEXT (
736- item . context . name
737- ) ,
738- { modal : true } ,
739- 'Delete'
740- )
741- is_showing_dialog = false
742-
743- if ( confirm_delete == 'Delete' ) {
744- if ( context_source == 'internal' ) {
745- const updated_contexts = internal_contexts . filter (
746- ( c ) => c . name != item . context . name
747- )
748-
739+ const deleted_context = item . context
740+ const deleted_context_name = item . context . name
741+ const deleted_index = item . index
742+
743+ if ( context_source == 'internal' ) {
744+ const updated_contexts = internal_contexts . filter (
745+ ( c ) => c . name != deleted_context_name
746+ )
747+ await extension_context . workspaceState . update (
748+ SAVED_CONTEXTS_STATE_KEY ,
749+ updated_contexts
750+ )
751+ internal_contexts = updated_contexts
752+ quick_pick . items = create_quick_pick_items ( internal_contexts )
753+
754+ is_showing_dialog = true
755+ const choice = await vscode . window . showInformationMessage (
756+ `Deleted context "${ deleted_context_name } " from workspace state` ,
757+ 'Revert'
758+ )
759+ is_showing_dialog = false
760+
761+ if ( choice === 'Revert' ) {
762+ internal_contexts . splice ( deleted_index , 0 , deleted_context )
749763 await extension_context . workspaceState . update (
750764 SAVED_CONTEXTS_STATE_KEY ,
751- updated_contexts
765+ internal_contexts
752766 )
753- internal_contexts = updated_contexts
754-
755767 vscode . window . showInformationMessage (
756- `Deleted context "${ item . context . name } " from workspace state `
768+ `Restored context "${ deleted_context_name } ". `
757769 )
770+ quick_pick . items =
771+ create_quick_pick_items ( internal_contexts )
772+ }
758773
759- if ( internal_contexts . length == 0 ) {
760- quick_pick . hide ( )
761- vscode . window . showInformationMessage (
762- dictionary . information_message
763- . NO_SAVED_CONTEXTS_IN_WORKSPACE_STATE
774+ if ( internal_contexts . length === 0 ) {
775+ await vscode . window . showInformationMessage (
776+ dictionary . information_message
777+ . NO_SAVED_CONTEXTS_IN_WORKSPACE_STATE
778+ )
779+ go_back_after_delete = true
780+ quick_pick . hide ( )
781+ } else {
782+ quick_pick . show ( )
783+ }
784+ } else if ( context_source == 'file' ) {
785+ const roots = context_to_roots . get ( deleted_context_name ) || [ ]
786+ const original_file_contents = new Map < string , string > ( )
787+
788+ for ( const root of roots ) {
789+ const contexts_file_path = path . join (
790+ root ,
791+ '.vscode' ,
792+ 'contexts.json'
793+ )
794+ if ( fs . existsSync ( contexts_file_path ) ) {
795+ original_file_contents . set (
796+ contexts_file_path ,
797+ fs . readFileSync ( contexts_file_path , 'utf8' )
764798 )
765- } else {
766- quick_pick . items =
767- create_quick_pick_items ( internal_contexts )
768- quick_pick . show ( )
769799 }
770- } else if ( context_source == 'file' ) {
771- const roots = context_to_roots . get ( item . context . name ) || [ ]
772- for ( const root of roots ) {
773- const contexts_file_path = path . join (
774- root ,
775- '.vscode' ,
776- 'contexts.json'
777- )
778-
779- try {
780- if ( fs . existsSync ( contexts_file_path ) ) {
781- const content = fs . readFileSync (
782- contexts_file_path ,
783- 'utf8'
784- )
785- let root_contexts = JSON . parse ( content )
800+ }
786801
787- if ( ! Array . isArray ( root_contexts ) ) {
788- root_contexts = [ ]
789- }
802+ for ( const root of roots ) {
803+ const contexts_file_path = path . join (
804+ root ,
805+ '.vscode' ,
806+ 'contexts.json'
807+ )
808+ try {
809+ if ( fs . existsSync ( contexts_file_path ) ) {
810+ const content = fs . readFileSync (
811+ contexts_file_path ,
812+ 'utf8'
813+ )
814+ let root_contexts = JSON . parse ( content )
815+ if ( ! Array . isArray ( root_contexts ) ) root_contexts = [ ]
790816
791- root_contexts = root_contexts . filter (
792- ( c : SavedContext ) => c . name !== item . context . name
793- )
817+ root_contexts = root_contexts . filter (
818+ ( c : SavedContext ) => c . name !== deleted_context_name
819+ )
820+ await save_contexts_to_file (
821+ root_contexts ,
822+ contexts_file_path
823+ )
824+ }
825+ } catch ( error : any ) {
826+ vscode . window . showErrorMessage (
827+ `Error deleting context from ${ path . basename ( root ) } : ${
828+ error . message
829+ } `
830+ )
831+ }
832+ }
794833
795- await save_contexts_to_file (
796- root_contexts ,
797- contexts_file_path
798- )
799- }
834+ let reloaded = await load_and_merge_file_contexts ( )
835+ file_contexts . length = 0
836+ file_contexts . push ( ...reloaded . merged )
837+ context_to_roots . clear ( )
838+ reloaded . context_to_roots . forEach ( ( v , k ) =>
839+ context_to_roots . set ( k , v )
840+ )
841+ quick_pick . items = create_quick_pick_items ( file_contexts )
842+
843+ is_showing_dialog = true
844+ const choice = await vscode . window . showInformationMessage (
845+ `Deleted context "${ deleted_context_name } " from all workspace roots` ,
846+ 'Revert'
847+ )
848+ is_showing_dialog = false
849+
850+ if ( choice === 'Revert' ) {
851+ let success = true
852+ for ( const [
853+ file_path ,
854+ content
855+ ] of original_file_contents . entries ( ) ) {
856+ try {
857+ const original_contexts = JSON . parse ( content )
858+ await save_contexts_to_file (
859+ original_contexts ,
860+ file_path
861+ )
800862 } catch ( error : any ) {
801863 vscode . window . showErrorMessage (
802- `Error deleting context from ${ path . basename (
803- root
804- ) } : ${ error . message } `
864+ `Failed to revert context deletion in ${ file_path } : ${ error . message } `
805865 )
866+ success = false
806867 }
807868 }
808869
809- const reloaded = await load_and_merge_file_contexts ( )
810- file_contexts . length = 0
811- file_contexts . push ( ...reloaded . merged )
812- context_to_roots . clear ( )
813- reloaded . context_to_roots . forEach ( ( v , k ) =>
814- context_to_roots . set ( k , v )
815- )
816-
817- vscode . window . showInformationMessage (
818- `Deleted context "${ item . context . name } " from all workspace roots`
819- )
820-
821- if ( file_contexts . length == 0 ) {
822- quick_pick . hide ( )
870+ if ( success ) {
871+ reloaded = await load_and_merge_file_contexts ( )
872+ file_contexts . length = 0
873+ file_contexts . push ( ...reloaded . merged )
874+ context_to_roots . clear ( )
875+ reloaded . context_to_roots . forEach ( ( v , k ) =>
876+ context_to_roots . set ( k , v )
877+ )
823878 vscode . window . showInformationMessage (
824- dictionary . information_message
825- . NO_SAVED_CONTEXTS_IN_JSON_FILE
879+ `Restored context "${ deleted_context_name } ".`
826880 )
827- } else {
828881 quick_pick . items = create_quick_pick_items ( file_contexts )
829- quick_pick . show ( )
830882 }
831883 }
832- } else {
833- quick_pick . show ( )
884+
885+ if ( file_contexts . length === 0 ) {
886+ await vscode . window . showInformationMessage (
887+ dictionary . information_message
888+ . NO_SAVED_CONTEXTS_IN_JSON_FILE
889+ )
890+ go_back_after_delete = true
891+ quick_pick . hide ( )
892+ } else {
893+ quick_pick . show ( )
894+ }
834895 }
835896 return
836897 }
0 commit comments