Skip to content

Commit 6d03577

Browse files
committed
use terminal CWD when sending paths
1 parent 1552086 commit 6d03577

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

lua/claudecode/init.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,9 +1094,10 @@ end
10941094

10951095
---Format file path for at mention (exposed for testing)
10961096
---@param file_path string The file path to format
1097+
---@param base_cwd string|nil Optional base working directory (defaults to Neovim's CWD)
10971098
---@return string formatted_path The formatted path
10981099
---@return boolean is_directory Whether the path is a directory
1099-
function M._format_path_for_at_mention(file_path)
1100+
function M._format_path_for_at_mention(file_path, base_cwd)
11001101
-- Input validation
11011102
if not file_path or type(file_path) ~= "string" or file_path == "" then
11021103
error("format_path_for_at_mention: file_path must be a non-empty string")
@@ -1112,9 +1113,9 @@ function M._format_path_for_at_mention(file_path)
11121113

11131114
local is_directory = vim.fn.isdirectory(file_path) == 1
11141115
local formatted_path = file_path
1116+
local cwd = base_cwd or vim.fn.getcwd()
11151117

11161118
if is_directory then
1117-
local cwd = vim.fn.getcwd()
11181119
if string.find(file_path, cwd, 1, true) == 1 then
11191120
local relative_path = string.sub(file_path, #cwd + 2)
11201121
if relative_path ~= "" then
@@ -1127,7 +1128,6 @@ function M._format_path_for_at_mention(file_path)
11271128
formatted_path = formatted_path .. "/"
11281129
end
11291130
else
1130-
local cwd = vim.fn.getcwd()
11311131
if string.find(file_path, cwd, 1, true) == 1 then
11321132
local relative_path = string.sub(file_path, #cwd + 2)
11331133
if relative_path ~= "" then
@@ -1145,9 +1145,13 @@ function M._broadcast_at_mention(file_path, start_line, end_line)
11451145
return false, "Claude Code integration is not running"
11461146
end
11471147

1148+
-- Get terminal CWD for path formatting (falls back to Neovim CWD if nil)
1149+
local terminal = require("claudecode.terminal")
1150+
local terminal_cwd = terminal.get_terminal_cwd()
1151+
11481152
-- Safely format the path and handle validation errors
11491153
local formatted_path, is_directory
1150-
local format_success, format_result, is_dir_result = pcall(M._format_path_for_at_mention, file_path)
1154+
local format_success, format_result, is_dir_result = pcall(M._format_path_for_at_mention, file_path, terminal_cwd)
11511155
if not format_success then
11521156
return false, format_result -- format_result contains the error message
11531157
end

lua/claudecode/terminal.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ local M = {}
66

77
local claudecode_server_module = require("claudecode.server.init")
88

9+
-- Cache the terminal's working directory (set when terminal is opened)
10+
local last_terminal_cwd = nil
11+
912
---@type ClaudeCodeTerminalConfig
1013
local defaults = {
1114
split_side = "right",
@@ -498,6 +501,7 @@ end
498501
---@param cmd_args string? Arguments to append to the claude command.
499502
function M.open(opts_override, cmd_args)
500503
local effective_config = build_config(opts_override)
504+
last_terminal_cwd = effective_config.cwd
501505
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
502506

503507
get_provider().open(cmd_string, claude_env_table, effective_config)
@@ -513,6 +517,7 @@ end
513517
---@param cmd_args string? Arguments to append to the claude command.
514518
function M.simple_toggle(opts_override, cmd_args)
515519
local effective_config = build_config(opts_override)
520+
last_terminal_cwd = effective_config.cwd
516521
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
517522

518523
get_provider().simple_toggle(cmd_string, claude_env_table, effective_config)
@@ -523,6 +528,7 @@ end
523528
---@param cmd_args string|nil (optional) Arguments to append to the claude command.
524529
function M.focus_toggle(opts_override, cmd_args)
525530
local effective_config = build_config(opts_override)
531+
last_terminal_cwd = effective_config.cwd
526532
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
527533

528534
get_provider().focus_toggle(cmd_string, claude_env_table, effective_config)
@@ -569,4 +575,11 @@ function M._get_managed_terminal_for_test()
569575
return nil
570576
end
571577

578+
---Gets the cached terminal working directory.
579+
---This returns the CWD that was resolved when the terminal was opened.
580+
---@return string|nil The terminal's working directory, or nil if no terminal has been opened.
581+
function M.get_terminal_cwd()
582+
return last_terminal_cwd
583+
end
584+
572585
return M

tests/unit/at_mention_edge_cases_spec.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,56 @@ describe("At Mention Edge Cases", function()
326326
assert_contains(result, "file.lua")
327327
end)
328328
end)
329+
330+
describe("custom base_cwd parameter", function()
331+
it("should use custom base_cwd when provided", function()
332+
mock_vim.fn.filereadable = function(path)
333+
return path == "/git/repo/src/main.lua" and 1 or 0
334+
end
335+
336+
-- Default behavior uses vim.fn.getcwd() which returns /Users/test/project
337+
local result_default = init_module._format_path_for_at_mention("/git/repo/src/main.lua")
338+
expect(result_default).to_be("/git/repo/src/main.lua") -- Not relative to /Users/test/project
339+
340+
-- With custom base_cwd, should be relative to /git/repo
341+
local result_custom = init_module._format_path_for_at_mention("/git/repo/src/main.lua", "/git/repo")
342+
expect(result_custom).to_be("src/main.lua")
343+
end)
344+
345+
it("should use custom base_cwd for directories", function()
346+
mock_vim.fn.isdirectory = function(path)
347+
return path == "/git/repo/src" and 1 or 0
348+
end
349+
mock_vim.fn.filereadable = function()
350+
return 0
351+
end
352+
353+
-- With custom base_cwd, directory should be relative
354+
local result_custom = init_module._format_path_for_at_mention("/git/repo/src", "/git/repo")
355+
expect(result_custom).to_be("src/")
356+
end)
357+
358+
it("should fall back to vim.fn.getcwd() when base_cwd is nil", function()
359+
mock_vim.fn.filereadable = function(path)
360+
return path == "/Users/test/project/config.lua" and 1 or 0
361+
end
362+
363+
-- When base_cwd is nil, should use vim.fn.getcwd()
364+
local result = init_module._format_path_for_at_mention("/Users/test/project/config.lua", nil)
365+
expect(result).to_be("config.lua") -- Relative to /Users/test/project (getcwd)
366+
end)
367+
368+
it("should handle root directory with custom base_cwd", function()
369+
mock_vim.fn.isdirectory = function(path)
370+
return path == "/git/repo" and 1 or 0
371+
end
372+
mock_vim.fn.filereadable = function()
373+
return 0
374+
end
375+
376+
-- Path is exactly the base_cwd
377+
local result = init_module._format_path_for_at_mention("/git/repo", "/git/repo")
378+
expect(result).to_be("./")
379+
end)
380+
end)
329381
end)

0 commit comments

Comments
 (0)