22--- Handles detection and selection of files from nvim-tree, neo-tree, mini.files, and oil.nvim
33--- @module ' claudecode.integrations'
44local M = {}
5+ local logger = require (" claudecode.logger" )
56
67--- Get selected files from the current tree explorer
78--- @return table | nil files List of file paths , or nil if error
7576function M ._get_neotree_selection ()
7677 local success , manager = pcall (require , " neo-tree.sources.manager" )
7778 if not success then
79+ logger .debug (" integrations/neotree" , " neo-tree not available (require failed)" )
7880 return {}, " neo-tree not available"
7981 end
8082
8183 local state = manager .get_state (" filesystem" )
8284 if not state then
85+ logger .debug (" integrations/neotree" , " filesystem state not available from manager" )
8386 return {}, " neo-tree filesystem state not available"
8487 end
8588
8689 local files = {}
8790
8891 -- Use neo-tree's own visual selection method (like their copy/paste feature)
8992 local mode = vim .fn .mode ()
93+ local current_win = vim .api .nvim_get_current_win ()
94+ logger .debug (
95+ " integrations/neotree" ,
96+ " begin selection" ,
97+ " mode=" ,
98+ mode ,
99+ " current_win=" ,
100+ current_win ,
101+ " state.winid=" ,
102+ tostring (state .winid )
103+ )
90104
91105 if mode == " V" or mode == " v" or mode == " \22 " then
92- local current_win = vim .api .nvim_get_current_win ()
93-
94106 if state .winid and state .winid == current_win then
95107 -- Use neo-tree's exact method to get visual range (from their get_selected_nodes implementation)
96108 local start_pos = vim .fn .getpos (" '<" )[2 ]
@@ -113,6 +125,8 @@ function M._get_neotree_selection()
113125 start_pos , end_pos = end_pos , start_pos
114126 end
115127
128+ logger .debug (" integrations/neotree" , " visual selection range" , start_pos , " to" , end_pos )
129+
116130 local selected_nodes = {}
117131
118132 for line = start_pos , end_pos do
@@ -121,22 +135,59 @@ function M._get_neotree_selection()
121135 -- Add validation for node types before adding to selection
122136 if node .type and node .type ~= " message" then
123137 table.insert (selected_nodes , node )
138+ local depth = (node .get_depth and node :get_depth ()) and node :get_depth () or 0
139+ logger .debug (
140+ " integrations/neotree" ,
141+ " line" ,
142+ line ,
143+ " node type=" ,
144+ tostring (node .type ),
145+ " depth=" ,
146+ depth ,
147+ " path=" ,
148+ tostring (node .path )
149+ )
150+ else
151+ logger .debug (" integrations/neotree" , " line" , line , " node rejected (type)" , tostring (node and node .type ))
124152 end
153+ else
154+ logger .debug (" integrations/neotree" , " line" , line , " no node returned from state.tree:get_node" )
125155 end
126156 end
127157
158+ logger .debug (" integrations/neotree" , " selected_nodes count=" , # selected_nodes )
159+
128160 for _ , node in ipairs (selected_nodes ) do
129161 -- Enhanced validation: check for file type and valid path
130162 if node .type == " file" and node .path and node .path ~= " " then
131163 -- Additional check: ensure it's not a root node (depth protection)
132164 local depth = (node .get_depth and node :get_depth ()) and node :get_depth () or 0
133165 if depth > 1 then
134166 table.insert (files , node .path )
167+ logger .debug (" integrations/neotree" , " accepted file" , node .path )
168+ else
169+ logger .debug (" integrations/neotree" , " rejected file (depth<=1)" , node .path )
135170 end
171+ elseif node .type == " directory" and node .path and node .path ~= " " then
172+ local depth = (node .get_depth and node :get_depth ()) and node :get_depth () or 0
173+ if depth > 1 then
174+ table.insert (files , node .path )
175+ logger .debug (" integrations/neotree" , " accepted directory" , node .path )
176+ else
177+ logger .debug (" integrations/neotree" , " rejected directory (depth<=1)" , node .path )
178+ end
179+ else
180+ logger .debug (
181+ " integrations/neotree" ,
182+ " rejected node (missing path or unsupported type)" ,
183+ tostring (node and node .type ),
184+ tostring (node and node .path )
185+ )
136186 end
137187 end
138188
139189 if # files > 0 then
190+ logger .debug (" integrations/neotree" , " files from visual selection:" , files )
140191 return files , nil
141192 end
142193 end
@@ -154,13 +205,23 @@ function M._get_neotree_selection()
154205 end
155206
156207 if selection and # selection > 0 then
208+ logger .debug (" integrations/neotree" , " using state selection count=" , # selection )
157209 for _ , node in ipairs (selection ) do
158210 if node .type == " file" and node .path then
159211 table.insert (files , node .path )
212+ logger .debug (" integrations/neotree" , " accepted file from state selection" , node .path )
213+ else
214+ logger .debug (
215+ " integrations/neotree" ,
216+ " ignored non-file in state selection" ,
217+ tostring (node and node .type ),
218+ tostring (node and node .path )
219+ )
160220 end
161221 end
162222
163223 if # files > 0 then
224+ logger .debug (" integrations/neotree" , " files from state selection:" , files )
164225 return files , nil
165226 end
166227 end
@@ -170,6 +231,14 @@ function M._get_neotree_selection()
170231 local node = state .tree :get_node ()
171232
172233 if node then
234+ logger .debug (
235+ " integrations/neotree" ,
236+ " fallback single node" ,
237+ " type=" ,
238+ tostring (node .type ),
239+ " path=" ,
240+ tostring (node .path )
241+ )
173242 if node .type == " file" and node .path then
174243 return { node .path }, nil
175244 elseif node .type == " directory" and node .path then
@@ -178,6 +247,7 @@ function M._get_neotree_selection()
178247 end
179248 end
180249
250+ logger .debug (" integrations/neotree" , " no file found under cursor/selection" )
181251 return {}, " No file found under cursor"
182252end
183253
0 commit comments