@@ -8,13 +8,28 @@ local M = {}
8
8
9
9
--- Terminal buffer and window management
10
10
-- @table ClaudeCodeTerminal
11
- -- @field bufnr number|nil Buffer number of the Claude Code terminal
11
+ -- @field instances table Key-value store of git root to buffer number
12
12
-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened
13
+ -- @field current_instance string|nil Current git root path for active instance
13
14
M .terminal = {
14
- bufnr = nil ,
15
+ instances = {} ,
15
16
saved_updatetime = nil ,
17
+ current_instance = nil ,
16
18
}
17
19
20
+ --- Get the current git root or a fallback identifier
21
+ --- @param git table The git module
22
+ --- @return string identifier Git root path or fallback identifier
23
+ local function get_instance_identifier (git )
24
+ local git_root = git .get_git_root ()
25
+ if git_root then
26
+ return git_root
27
+ else
28
+ -- Fallback to current working directory if not in a git repo
29
+ return vim .fn .getcwd ()
30
+ end
31
+ end
32
+
18
33
--- Create a split window according to the specified position configuration
19
34
--- @param position string Window position configuration
20
35
--- @param config table Plugin configuration containing window settings
49
64
--- @param claude_code table The main plugin module
50
65
--- @param config table The plugin configuration
51
66
function M .force_insert_mode (claude_code , config )
52
- local bufnr = claude_code .claude_code .bufnr
53
- if bufnr and vim .api .nvim_buf_is_valid (bufnr ) and vim .fn .bufnr ' %' == bufnr then
67
+ local current_bufnr = vim .fn .bufnr (' %' )
68
+
69
+ -- Check if current buffer is any of our Claude instances
70
+ local is_claude_instance = false
71
+ for _ , bufnr in pairs (claude_code .claude_code .instances ) do
72
+ if bufnr == current_bufnr and vim .api .nvim_buf_is_valid (bufnr ) then
73
+ is_claude_instance = true
74
+ break
75
+ end
76
+ end
77
+
78
+ if is_claude_instance then
54
79
-- Only enter insert mode if we're in the terminal buffer and not already in insert mode
55
80
-- and not configured to stay in normal mode
56
81
if config .window .start_in_normal_mode then
72
97
--- @param config table The plugin configuration
73
98
--- @param git table The git module
74
99
function M .toggle (claude_code , config , git )
75
- -- Check if Claude Code is already running
76
- local bufnr = claude_code .claude_code .bufnr
100
+ -- Determine instance ID based on config
101
+ local instance_id
102
+ if config .git .multi_instance then
103
+ -- Use git root or current directory as instance identifier
104
+ instance_id = get_instance_identifier (git )
105
+ else
106
+ -- Use a fixed ID for single instance mode
107
+ instance_id = " global"
108
+ end
109
+
110
+ claude_code .claude_code .current_instance = instance_id
111
+
112
+ -- Check if this Claude Code instance is already running
113
+ local bufnr = claude_code .claude_code .instances [instance_id ]
77
114
if bufnr and vim .api .nvim_buf_is_valid (bufnr ) then
78
- -- Check if there's a window displaying Claude Code buffer
115
+ -- Check if there's a window displaying this Claude Code buffer
79
116
local win_ids = vim .fn .win_findbuf (bufnr )
80
117
if # win_ids > 0 then
81
118
-- Claude Code is visible, close the window
@@ -93,7 +130,7 @@ function M.toggle(claude_code, config, git)
93
130
end
94
131
end
95
132
else
96
- -- Claude Code is not running, start it in a new split
133
+ -- This Claude Code instance is not running, start it in a new split
97
134
create_split (config .window .position , config )
98
135
99
136
-- Determine if we should use the git root directory
@@ -108,7 +145,15 @@ function M.toggle(claude_code, config, git)
108
145
109
146
vim .cmd (cmd )
110
147
vim .cmd ' setlocal bufhidden=hide'
111
- vim .cmd ' file claude-code'
148
+
149
+ -- Create a unique buffer name (or a standard one in single instance mode)
150
+ local buffer_name
151
+ if config .git .multi_instance then
152
+ buffer_name = ' claude-code-' .. instance_id :gsub (' [/\\\\ ]' , ' -' )
153
+ else
154
+ buffer_name = ' claude-code'
155
+ end
156
+ vim .cmd (' file ' .. buffer_name )
112
157
113
158
if config .window .hide_numbers then
114
159
vim .cmd ' setlocal nonumber norelativenumber'
@@ -118,8 +163,8 @@ function M.toggle(claude_code, config, git)
118
163
vim .cmd ' setlocal signcolumn=no'
119
164
end
120
165
121
- -- Store buffer number for future reference
122
- claude_code .claude_code .bufnr = vim .fn .bufnr ' %'
166
+ -- Store buffer number for this instance
167
+ claude_code .claude_code .instances [ instance_id ] = vim .fn .bufnr ( ' %' )
123
168
124
169
-- Automatically enter insert mode in terminal unless configured to start in normal mode
125
170
if config .window .enter_insert and not config .window .start_in_normal_mode then
0 commit comments