@@ -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 == 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
5479 -- Only enter insert mode if we're in the terminal buffer and not already in insert mode
5580 -- and not configured to stay in normal mode
5681 if config .window .start_in_normal_mode then
7297--- @param config table The plugin configuration
7398--- @param git table The git module
7499function 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 ]
77114 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
79116 local win_ids = vim .fn .win_findbuf (bufnr )
80117 if # win_ids > 0 then
81118 -- Claude Code is visible, close the window
@@ -93,7 +130,7 @@ function M.toggle(claude_code, config, git)
93130 end
94131 end
95132 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
97134 create_split (config .window .position , config )
98135
99136 -- Determine if we should use the git root directory
@@ -108,7 +145,15 @@ function M.toggle(claude_code, config, git)
108145
109146 vim .cmd (cmd )
110147 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 )
112157
113158 if config .window .hide_numbers then
114159 vim .cmd ' setlocal nonumber norelativenumber'
@@ -118,8 +163,8 @@ function M.toggle(claude_code, config, git)
118163 vim .cmd ' setlocal signcolumn=no'
119164 end
120165
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 ( ' %' )
123168
124169 -- Automatically enter insert mode in terminal unless configured to start in normal mode
125170 if config .window .enter_insert and not config .window .start_in_normal_mode then
0 commit comments