-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathintegrations.lua
More file actions
186 lines (159 loc) · 6.29 KB
/
integrations.lua
File metadata and controls
186 lines (159 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
---
-- Tree integration module for ClaudeCode.nvim
-- Handles detection and selection of files from nvim-tree and neo-tree
-- @module claudecode.integrations
local M = {}
local logger = require("claudecode.logger")
--- Get selected files from the current tree explorer
--- @return table|nil files List of file paths, or nil if error
--- @return string|nil error Error message if operation failed
function M.get_selected_files_from_tree()
local current_ft = vim.bo.filetype
if current_ft == "NvimTree" then
return M._get_nvim_tree_selection()
elseif current_ft == "neo-tree" then
return M._get_neotree_selection()
else
return nil, "Not in a supported tree buffer (current filetype: " .. current_ft .. ")"
end
end
--- Get selected files from nvim-tree
--- Supports both multi-selection (marks) and single file under cursor
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_nvim_tree_selection()
local success, nvim_tree_api = pcall(require, "nvim-tree.api")
if not success then
logger.warn("integrations", "nvim-tree API not available")
return {}, "nvim-tree not available"
end
local files = {}
-- Check for multi-selection first (marked files)
local marks = nvim_tree_api.marks.list()
if marks and #marks > 0 then
logger.debug("integrations", "Found " .. #marks .. " marked files in nvim-tree")
for _, mark in ipairs(marks) do
if mark.type == "file" and mark.absolute_path then
table.insert(files, mark.absolute_path)
logger.debug("integrations", "Added marked file: " .. mark.absolute_path)
end
end
if #files > 0 then
return files, nil
end
end
-- Fall back to node under cursor
local node = nvim_tree_api.tree.get_node_under_cursor()
if node then
if node.type == "file" and node.absolute_path then
logger.debug("integrations", "Found file under cursor: " .. node.absolute_path)
return { node.absolute_path }, nil
elseif node.type == "directory" then
return {}, "Cannot add directory to Claude context. Please select a file."
end
end
return {}, "No file found under cursor"
end
--- Get selected files from neo-tree
--- Uses neo-tree's own visual selection method when in visual mode
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_neotree_selection()
local success, manager = pcall(require, "neo-tree.sources.manager")
if not success then
logger.warn("integrations", "neo-tree manager not available")
return {}, "neo-tree not available"
end
local state = manager.get_state("filesystem")
if not state then
logger.warn("integrations", "neo-tree filesystem state not available")
return {}, "neo-tree filesystem state not available"
end
local files = {}
-- Method 1: Use neo-tree's own visual selection method (like their copy/paste feature)
-- This matches how neo-tree internally handles visual selection
local mode = vim.fn.mode()
if mode == "V" or mode == "v" or mode == "\22" then -- Visual modes
logger.debug("integrations", "Visual mode detected: " .. mode)
-- Check if we're in the neo-tree window
if state.winid and state.winid == vim.api.nvim_get_current_win() then
logger.debug("integrations", "In neo-tree window, getting visual selection using neo-tree method")
-- Use neo-tree's method to get selected nodes from visual range
local start_pos = vim.fn.getpos("'<")[2]
local end_pos = vim.fn.getpos("'>")[2]
if end_pos < start_pos then
start_pos, end_pos = end_pos, start_pos
end
logger.debug("integrations", "Visual selection from line " .. start_pos .. " to " .. end_pos)
local selected_nodes = {}
for line = start_pos, end_pos do
local node = state.tree:get_node(line)
if node then
table.insert(selected_nodes, node)
logger.debug(
"integrations",
"Found node at line " .. line .. ": type=" .. (node.type or "nil") .. ", path=" .. (node.path or "nil")
)
else
logger.debug("integrations", "No node found at line " .. line)
end
end
logger.debug("integrations", "Found " .. #selected_nodes .. " selected nodes from visual range")
-- Extract file paths from selected nodes
for _, node in ipairs(selected_nodes) do
if node.type == "file" and node.path then
table.insert(files, node.path)
logger.debug("integrations", "Added file from visual selection: " .. node.path)
end
end
if #files > 0 then
return files, nil
else
logger.debug("integrations", "No files found in visual selection (" .. #selected_nodes .. " nodes total)")
end
else
logger.debug("integrations", "Not in neo-tree window, visual selection method not applicable")
end
end
-- Method 2: Try neo-tree's built-in selection methods
if state.tree then
local selection = nil
if state.tree.get_selection then
selection = state.tree:get_selection()
if selection and #selection > 0 then
logger.debug("integrations", "Found selection via get_selection: " .. #selection)
end
end
-- Method 3: Check state-level selection
if (not selection or #selection == 0) and state.selected_nodes then
selection = state.selected_nodes
logger.debug("integrations", "Found selection via state.selected_nodes: " .. #selection)
end
-- Process selection if found
if selection and #selection > 0 then
for _, node in ipairs(selection) do
if node.type == "file" and node.path then
table.insert(files, node.path)
logger.debug("integrations", "Added selected file: " .. node.path)
end
end
if #files > 0 then
return files, nil
end
end
end
-- Fall back to current node under cursor
if state.tree then
local node = state.tree:get_node()
if node then
if node.type == "file" and node.path then
logger.debug("integrations", "Found file under cursor: " .. node.path)
return { node.path }, nil
elseif node.type == "directory" then
return {}, "Cannot add directory to Claude context. Please select a file."
end
end
end
return {}, "No file found under cursor"
end
return M