@@ -8,13 +8,28 @@ local M = {}
88
99--- Terminal buffer and window management
1010-- @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
1212-- @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
1314M .terminal = {
14- bufnr = nil ,
15+ instances = {} ,
1516 saved_updatetime = nil ,
17+ current_instance = nil ,
1618}
1719
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+
1833--- Create a split window according to the specified position configuration
1934--- @param position string Window position configuration
2035--- @param config table Plugin configuration containing window settings
4964--- @param claude_code table The main plugin module
5065--- @param config table The plugin configuration
5166function 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
73+ and bufnr == current_bufnr
74+ and vim .api .nvim_buf_is_valid (bufnr )
75+ then
76+ is_claude_instance = true
77+ break
78+ end
79+ end
80+
81+ if is_claude_instance then
5482 -- Only enter insert mode if we're in the terminal buffer and not already in insert mode
5583 -- and not configured to stay in normal mode
5684 if config .window .start_in_normal_mode then
72100--- @param config table The plugin configuration
73101--- @param git table The git module
74102function M .toggle (claude_code , config , git )
75- -- Check if Claude Code is already running
76- local bufnr = claude_code .claude_code .bufnr
103+ -- Determine instance ID based on config
104+ local instance_id
105+ if config .git .multi_instance then
106+ if config .git .use_git_root then
107+ instance_id = get_instance_identifier (git )
108+ else
109+ instance_id = vim .fn .getcwd ()
110+ end
111+ else
112+ -- Use a fixed ID for single instance mode
113+ instance_id = " global"
114+ end
115+
116+ claude_code .claude_code .current_instance = instance_id
117+
118+ -- Check if this Claude Code instance is already running
119+ local bufnr = claude_code .claude_code .instances [instance_id ]
77120 if bufnr and vim .api .nvim_buf_is_valid (bufnr ) then
78- -- Check if there's a window displaying Claude Code buffer
121+ -- Check if there's a window displaying this Claude Code buffer
79122 local win_ids = vim .fn .win_findbuf (bufnr )
80123 if # win_ids > 0 then
81124 -- Claude Code is visible, close the window
@@ -93,7 +136,11 @@ function M.toggle(claude_code, config, git)
93136 end
94137 end
95138 else
96- -- Claude Code is not running, start it in a new split
139+ -- Prune invalid buffer entries
140+ if bufnr and not vim .api .nvim_buf_is_valid (bufnr ) then
141+ claude_code .claude_code .instances [instance_id ] = nil
142+ end
143+ -- This Claude Code instance is not running, start it in a new split
97144 create_split (config .window .position , config )
98145
99146 -- Determine if we should use the git root directory
@@ -108,7 +155,15 @@ function M.toggle(claude_code, config, git)
108155
109156 vim .cmd (cmd )
110157 vim .cmd ' setlocal bufhidden=hide'
111- vim .cmd ' file claude-code'
158+
159+ -- Create a unique buffer name (or a standard one in single instance mode)
160+ local buffer_name
161+ if config .git .multi_instance then
162+ buffer_name = ' claude-code-' .. instance_id :gsub (' [^%w%-_]' , ' -' )
163+ else
164+ buffer_name = ' claude-code'
165+ end
166+ vim .cmd (' file ' .. buffer_name )
112167
113168 if config .window .hide_numbers then
114169 vim .cmd ' setlocal nonumber norelativenumber'
@@ -118,8 +173,8 @@ function M.toggle(claude_code, config, git)
118173 vim .cmd ' setlocal signcolumn=no'
119174 end
120175
121- -- Store buffer number for future reference
122- claude_code .claude_code .bufnr = vim .fn .bufnr ' %'
176+ -- Store buffer number for this instance
177+ claude_code .claude_code .instances [ instance_id ] = vim .fn .bufnr ( ' %' )
123178
124179 -- Automatically enter insert mode in terminal unless configured to start in normal mode
125180 if config .window .enter_insert and not config .window .start_in_normal_mode then
0 commit comments