Skip to content

Commit fc1cece

Browse files
committed
fix: get cwd in contexts directly
Storing it does not work in the end so have to do it like this. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 5e2dd6d commit fc1cece

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

lua/CopilotChat/config.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local utils = require('CopilotChat.utils')
66
--- @class CopilotChat.config.source
77
--- @field bufnr number
88
--- @field winnr number
9-
--- @field cwd string
109

1110
---@class CopilotChat.config.selection.diagnostic
1211
---@field content string
@@ -190,9 +189,10 @@ return {
190189
file = {
191190
description = 'Includes content of provided file in chat context. Supports input.',
192191
input = function(callback, source)
192+
local cwd = utils.win_cwd(source.winnr)
193193
local files = vim.tbl_filter(function(file)
194194
return vim.fn.isdirectory(file) == 0
195-
end, vim.fn.glob(source.cwd .. '/**/*', false, true))
195+
end, vim.fn.glob(cwd .. '/**/*', false, true))
196196

197197
vim.ui.select(files, {
198198
prompt = 'Select a file> ',
@@ -213,7 +213,7 @@ return {
213213
}, callback)
214214
end,
215215
resolve = function(input, source)
216-
return context.files(input, source.cwd)
216+
return context.files(input, source.winnr)
217217
end,
218218
},
219219
git = {
@@ -225,7 +225,7 @@ return {
225225
end,
226226
resolve = function(input, source)
227227
return {
228-
context.gitdiff(input, source.cwd),
228+
context.gitdiff(input, source.winnr),
229229
}
230230
end,
231231
},

lua/CopilotChat/context.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ end
191191

192192
--- Get list of all files in workspace
193193
---@param pattern string?
194-
---@param cwd string
194+
---@param winnr number
195195
---@return table<CopilotChat.copilot.embed>
196-
function M.files(pattern, cwd)
196+
function M.files(pattern, winnr)
197+
local cwd = utils.win_cwd(winnr)
197198
local search = cwd .. '/' .. (pattern or '**/*')
198199
local files = vim.tbl_filter(function(file)
199200
return vim.fn.isdirectory(file) == 0
@@ -265,10 +266,11 @@ end
265266

266267
--- Get current git diff
267268
---@param type string?
268-
---@param cwd string
269+
---@param winnr number
269270
---@return CopilotChat.copilot.embed?
270-
function M.gitdiff(type, cwd)
271+
function M.gitdiff(type, winnr)
271272
type = type or 'unstaged'
273+
local cwd = utils.win_cwd(winnr)
272274
local cmd = 'git -C ' .. cwd .. ' diff --no-color --no-ext-diff'
273275

274276
if type == 'staged' then

lua/CopilotChat/init.lua

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,9 @@ end
9292
local function update_selection()
9393
local prev_winnr = vim.fn.win_getid(vim.fn.winnr('#'))
9494
if prev_winnr ~= state.chat.winnr and vim.fn.win_gettype(prev_winnr) == '' then
95-
-- TODO: This is a hack to get the cwd of the previous window properly, its actually baffling I have to do this
96-
local current_win = vim.api.nvim_get_current_win()
97-
vim.api.nvim_set_current_win(prev_winnr)
98-
local cwd = vim.fn.getcwd()
99-
vim.api.nvim_set_current_win(current_win)
100-
10195
state.source = {
10296
bufnr = vim.api.nvim_win_get_buf(prev_winnr),
10397
winnr = prev_winnr,
104-
cwd = cwd,
10598
}
10699
end
107100

lua/CopilotChat/utils.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,25 @@ function M.quick_hash(str)
168168
return #str .. str:sub(1, 32) .. str:sub(-32)
169169
end
170170

171+
--- Get current working directory for target window
172+
--- TODO: This is a hack to get the cwd of the previous window properly, its actually baffling I have to do this
173+
---@param winnr number The buffer number
174+
---@return string
175+
function M.win_cwd(winnr)
176+
if not winnr or not vim.api.nvim_win_is_valid(winnr) then
177+
return '.'
178+
end
179+
180+
local current_win = vim.api.nvim_get_current_win()
181+
vim.api.nvim_set_current_win(winnr)
182+
local dir = vim.fn.getcwd()
183+
vim.api.nvim_set_current_win(current_win)
184+
185+
if not dir or dir == '' then
186+
return '.'
187+
end
188+
189+
return dir
190+
end
191+
171192
return M

0 commit comments

Comments
 (0)