@@ -85,15 +85,49 @@ struct ProjectNavigatorOutlineView: NSViewControllerRepresentable {
8585 guard let outlineView = controller? . outlineView else { return }
8686 let selectedRows = outlineView. selectedRowIndexes. compactMap ( { outlineView. item ( atRow: $0) } )
8787
88- // If some text view inside the outline view is first responder right now, push the update off
89- // until editing is finished using the `shouldReloadAfterDoneEditing` flag.
88+ // Check if we're currently editing a phantom file and capture its text
89+ var editingPhantomFile : CEWorkspaceFile ?
90+ var capturedText : String ?
91+ var capturedSelectionRange : NSRange ?
92+
9093 if outlineView. window? . firstResponder !== outlineView
9194 && outlineView. window? . firstResponder is NSTextView
9295 && ( outlineView. window? . firstResponder as? NSView ) ? . isDescendant ( of: outlineView) == true {
93- controller? . shouldReloadAfterDoneEditing = true
94- } else {
95- for item in updatedItems {
96- outlineView. reloadItem ( item, reloadChildren: true )
96+ // Find the cell being edited by traversing up from the text view
97+ var currentView = outlineView. window? . firstResponder as? NSView
98+ capturedSelectionRange = ( outlineView. window? . firstResponder as? NSTextView ) ? . selectedRange
99+
100+ while let view = currentView {
101+ if let cell = view as? ProjectNavigatorTableViewCell ,
102+ let fileItem = cell. fileItem,
103+ fileItem. phantomFile != nil {
104+ editingPhantomFile = fileItem
105+ capturedText = cell. textField? . stringValue
106+ break
107+ }
108+ currentView = view. superview
109+ }
110+ }
111+
112+ // Reload all items with children
113+ for item in updatedItems {
114+ outlineView. reloadItem ( item, reloadChildren: true )
115+ }
116+
117+ // If we were editing a phantom file, restore the text field and focus
118+ if let phantomFile = editingPhantomFile, let text = capturedText {
119+ let row = outlineView. row ( forItem: phantomFile)
120+ if row >= 0 ,
121+ let cell = outlineView. view (
122+ atColumn: 0 ,
123+ row: row,
124+ makeIfNecessary: false
125+ ) as? ProjectNavigatorTableViewCell {
126+ cell. textField? . stringValue = text
127+ outlineView. window? . makeFirstResponder ( cell. textField)
128+ if let selectionRange = capturedSelectionRange {
129+ cell. textField? . currentEditor ( ) ? . selectedRange = selectionRange
130+ }
97131 }
98132 }
99133
@@ -102,6 +136,12 @@ struct ProjectNavigatorOutlineView: NSViewControllerRepresentable {
102136 controller? . shouldSendSelectionUpdate = false
103137 outlineView. selectRowIndexes ( IndexSet ( selectedIndexes) , byExtendingSelection: false )
104138 controller? . shouldSendSelectionUpdate = true
139+
140+ // Reselect the file that is currently active in the editor so it still appears highlighted
141+ if outlineView. selectedRowIndexes. isEmpty,
142+ let activeFileID = workspace? . editorManager? . activeEditor. selectedTab? . file. id {
143+ controller? . updateSelection ( itemID: activeFileID)
144+ }
105145 }
106146
107147 deinit {
0 commit comments