@@ -48,6 +48,8 @@ import {
48
48
EVAL_REPL ,
49
49
MOVE_CURSOR ,
50
50
REMOVE_EDITOR_TAB ,
51
+ REMOVE_EDITOR_TAB_FOR_FILE ,
52
+ REMOVE_EDITOR_TABS_FOR_DIRECTORY ,
51
53
RESET_TESTCASE ,
52
54
RESET_WORKSPACE ,
53
55
SEND_REPL_INPUT_TO_OUTPUT ,
@@ -815,28 +817,78 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
815
817
) ;
816
818
817
819
const activeEditorTabIndex = state [ workspaceLocation ] . activeEditorTabIndex ;
818
- const newActiveEditorTabIndex =
819
- activeEditorTabIndex !== editorTabIndex
820
- ? // If the active editor tab is not the one that is removed,
821
- // the active editor tab remains the same if its index is
822
- // less than the removed editor tab index or null.
823
- activeEditorTabIndex === null || activeEditorTabIndex < editorTabIndex
824
- ? activeEditorTabIndex
825
- : // Otherwise, the active editor tab index needs to have 1
826
- // subtracted because every tab to the right of the editor
827
- // tab being removed has their index decremented by 1.
828
- activeEditorTabIndex - 1
829
- : newEditorTabs . length === 0
830
- ? // If there are no editor tabs after removal, there cannot
831
- // be an active editor tab.
832
- null
833
- : editorTabIndex === 0
834
- ? // If the removed editor tab is the leftmost tab, the active
835
- // editor tab will be the new leftmost tab.
836
- 0
837
- : // Otherwise, the active editor tab will be the tab to the
838
- // left of the removed tab.
839
- editorTabIndex - 1 ;
820
+ const newActiveEditorTabIndex = getNextActiveEditorTabIndexAfterTabRemoval (
821
+ activeEditorTabIndex ,
822
+ editorTabIndex ,
823
+ newEditorTabs . length
824
+ ) ;
825
+
826
+ return {
827
+ ...state ,
828
+ [ workspaceLocation ] : {
829
+ ...state [ workspaceLocation ] ,
830
+ activeEditorTabIndex : newActiveEditorTabIndex ,
831
+ editorTabs : newEditorTabs
832
+ }
833
+ } ;
834
+ }
835
+ case REMOVE_EDITOR_TAB_FOR_FILE : {
836
+ const removedFilePath = action . payload . removedFilePath ;
837
+
838
+ const editorTabs = state [ workspaceLocation ] . editorTabs ;
839
+ const editorTabIndexToRemove = editorTabs . findIndex (
840
+ ( editorTab : EditorTabState ) => editorTab . filePath === removedFilePath
841
+ ) ;
842
+ if ( editorTabIndexToRemove === - 1 ) {
843
+ return state ;
844
+ }
845
+ const newEditorTabs = editorTabs . filter (
846
+ ( editorTab : EditorTabState , index : number ) => index !== editorTabIndexToRemove
847
+ ) ;
848
+
849
+ const activeEditorTabIndex = state [ workspaceLocation ] . activeEditorTabIndex ;
850
+ const newActiveEditorTabIndex = getNextActiveEditorTabIndexAfterTabRemoval (
851
+ activeEditorTabIndex ,
852
+ editorTabIndexToRemove ,
853
+ newEditorTabs . length
854
+ ) ;
855
+
856
+ return {
857
+ ...state ,
858
+ [ workspaceLocation ] : {
859
+ ...state [ workspaceLocation ] ,
860
+ activeEditorTabIndex : newActiveEditorTabIndex ,
861
+ editorTabs : newEditorTabs
862
+ }
863
+ } ;
864
+ }
865
+ case REMOVE_EDITOR_TABS_FOR_DIRECTORY : {
866
+ const removedDirectoryPath = action . payload . removedDirectoryPath ;
867
+
868
+ const editorTabs = state [ workspaceLocation ] . editorTabs ;
869
+ const editorTabIndicesToRemove = editorTabs
870
+ . map ( ( editorTab : EditorTabState , index : number ) => {
871
+ if ( editorTab . filePath ?. startsWith ( removedDirectoryPath ) ) {
872
+ return index ;
873
+ }
874
+ return null ;
875
+ } )
876
+ . filter ( ( index : number | null ) : index is number => index !== null ) ;
877
+ if ( editorTabIndicesToRemove . length === 0 ) {
878
+ return state ;
879
+ }
880
+
881
+ let newActiveEditorTabIndex = state [ workspaceLocation ] . activeEditorTabIndex ;
882
+ const newEditorTabs = [ ...editorTabs ] ;
883
+ for ( let i = editorTabIndicesToRemove . length - 1 ; i >= 0 ; i -- ) {
884
+ const editorTabIndexToRemove = editorTabIndicesToRemove [ i ] ;
885
+ newEditorTabs . splice ( editorTabIndexToRemove , 1 ) ;
886
+ newActiveEditorTabIndex = getNextActiveEditorTabIndexAfterTabRemoval (
887
+ newActiveEditorTabIndex ,
888
+ editorTabIndexToRemove ,
889
+ newEditorTabs . length
890
+ ) ;
891
+ }
840
892
841
893
return {
842
894
...state ,
@@ -894,3 +946,31 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
894
946
return state ;
895
947
}
896
948
} ;
949
+
950
+ const getNextActiveEditorTabIndexAfterTabRemoval = (
951
+ activeEditorTabIndex : number | null ,
952
+ removedEditorTabIndex : number ,
953
+ newEditorTabsLength : number
954
+ ) => {
955
+ return activeEditorTabIndex !== removedEditorTabIndex
956
+ ? // If the active editor tab is not the one that is removed,
957
+ // the active editor tab remains the same if its index is
958
+ // less than the removed editor tab index or null.
959
+ activeEditorTabIndex === null || activeEditorTabIndex < removedEditorTabIndex
960
+ ? activeEditorTabIndex
961
+ : // Otherwise, the active editor tab index needs to have 1
962
+ // subtracted because every tab to the right of the editor
963
+ // tab being removed has their index decremented by 1.
964
+ activeEditorTabIndex - 1
965
+ : newEditorTabsLength === 0
966
+ ? // If there are no editor tabs after removal, there cannot
967
+ // be an active editor tab.
968
+ null
969
+ : removedEditorTabIndex === 0
970
+ ? // If the removed editor tab is the leftmost tab, the active
971
+ // editor tab will be the new leftmost tab.
972
+ 0
973
+ : // Otherwise, the active editor tab will be the tab to the
974
+ // left of the removed tab.
975
+ removedEditorTabIndex - 1 ;
976
+ } ;
0 commit comments