@@ -273,25 +273,38 @@ function M._create_commands()
273
273
--- @param file_path string The file path to broadcast
274
274
--- @return boolean success Whether the broadcast was successful
275
275
--- @return string | nil error Error message if broadcast failed
276
- local function broadcast_at_mention (file_path )
276
+ local function broadcast_at_mention (file_path , start_line , end_line )
277
277
if not M .state .server then
278
278
return false , " Claude Code integration is not running"
279
279
end
280
280
281
281
local formatted_path , is_directory = format_path_for_at_mention (file_path )
282
282
283
+ if is_directory and (start_line or end_line ) then
284
+ logger .debug (" command" , " Line numbers ignored for directory: " .. formatted_path )
285
+ start_line = nil
286
+ end_line = nil
287
+ end
288
+
283
289
local params = {
284
290
filePath = formatted_path ,
285
- lineStart = nil ,
286
- lineEnd = nil ,
291
+ lineStart = start_line ,
292
+ lineEnd = end_line ,
287
293
}
288
294
289
295
local broadcast_success = M .state .server .broadcast (" at_mentioned" , params )
290
296
if broadcast_success then
291
- logger .debug (
292
- " command" ,
293
- " Broadcast success: Added " .. (is_directory and " directory" or " file" ) .. " " .. formatted_path
294
- )
297
+ local message = " Broadcast success: Added " .. (is_directory and " directory" or " file" ) .. " " .. formatted_path
298
+ if not is_directory and (start_line or end_line ) then
299
+ local range_info = " "
300
+ if start_line and end_line then
301
+ range_info = " (lines " .. start_line .. " -" .. end_line .. " )"
302
+ elseif start_line then
303
+ range_info = " (from line " .. start_line .. " )"
304
+ end
305
+ message = message .. range_info
306
+ end
307
+ logger .debug (" command" , message )
295
308
return true , nil
296
309
else
297
310
local error_msg = " Failed to broadcast " .. (is_directory and " directory" or " file" ) .. " " .. formatted_path
@@ -545,35 +558,83 @@ function M._create_commands()
545
558
vim .api .nvim_create_user_command (" ClaudeCodeAdd" , function (opts )
546
559
if not M .state .server then
547
560
logger .error (" command" , " ClaudeCodeAdd: Claude Code integration is not running." )
548
- vim .notify (" Claude Code integration is not running" , vim .log .levels .ERROR )
549
561
return
550
562
end
551
563
552
- local file_path = opts .args
553
- if not file_path or file_path == " " then
564
+ if not opts .args or opts .args == " " then
554
565
logger .error (" command" , " ClaudeCodeAdd: No file path provided" )
555
- vim .notify (" ClaudeCodeAdd: Please provide a file path" , vim .log .levels .ERROR )
566
+ return
567
+ end
568
+
569
+ local args = vim .split (opts .args , " %s+" )
570
+ local file_path = args [1 ]
571
+ local start_line = args [2 ] and tonumber (args [2 ]) or nil
572
+ local end_line = args [3 ] and tonumber (args [3 ]) or nil
573
+
574
+ if # args > 3 then
575
+ logger .error (
576
+ " command" ,
577
+ " ClaudeCodeAdd: Too many arguments. Usage: ClaudeCodeAdd <file-path> [start-line] [end-line]"
578
+ )
579
+ return
580
+ end
581
+
582
+ if args [2 ] and not start_line then
583
+ logger .error (" command" , " ClaudeCodeAdd: Invalid start line number: " .. args [2 ])
584
+ return
585
+ end
586
+
587
+ if args [3 ] and not end_line then
588
+ logger .error (" command" , " ClaudeCodeAdd: Invalid end line number: " .. args [3 ])
589
+ return
590
+ end
591
+
592
+ if start_line and start_line < 1 then
593
+ logger .error (" command" , " ClaudeCodeAdd: Start line must be positive: " .. start_line )
594
+ return
595
+ end
596
+
597
+ if end_line and end_line < 1 then
598
+ logger .error (" command" , " ClaudeCodeAdd: End line must be positive: " .. end_line )
599
+ return
600
+ end
601
+
602
+ if start_line and end_line and start_line > end_line then
603
+ logger .error (
604
+ " command" ,
605
+ " ClaudeCodeAdd: Start line (" .. start_line .. " ) must be <= end line (" .. end_line .. " )"
606
+ )
556
607
return
557
608
end
558
609
559
610
file_path = vim .fn .expand (file_path )
560
611
if vim .fn .filereadable (file_path ) == 0 and vim .fn .isdirectory (file_path ) == 0 then
561
612
logger .error (" command" , " ClaudeCodeAdd: File or directory does not exist: " .. file_path )
562
- vim .notify (" ClaudeCodeAdd: File or directory does not exist: " .. file_path , vim .log .levels .ERROR )
563
613
return
564
614
end
565
615
566
- local success , error_msg = broadcast_at_mention (file_path )
616
+ -- Convert 1-indexed user input to 0-indexed for Claude
617
+ local claude_start_line = start_line and (start_line - 1 ) or nil
618
+ local claude_end_line = end_line and (end_line - 1 ) or nil
619
+
620
+ local success , error_msg = broadcast_at_mention (file_path , claude_start_line , claude_end_line )
567
621
if not success then
568
622
logger .error (" command" , " ClaudeCodeAdd: " .. (error_msg or " Failed to add file" ))
569
- vim .notify (" ClaudeCodeAdd: " .. (error_msg or " Failed to add file" ), vim .log .levels .ERROR )
570
623
else
571
- logger .debug (" command" , " ClaudeCodeAdd: Successfully added " .. file_path )
624
+ local message = " ClaudeCodeAdd: Successfully added " .. file_path
625
+ if start_line or end_line then
626
+ if start_line and end_line then
627
+ message = message .. " (lines " .. start_line .. " -" .. end_line .. " )"
628
+ elseif start_line then
629
+ message = message .. " (from line " .. start_line .. " )"
630
+ end
631
+ end
632
+ logger .debug (" command" , message )
572
633
end
573
634
end , {
574
- nargs = 1 ,
635
+ nargs = " + " ,
575
636
complete = " file" ,
576
- desc = " Add specified file or directory to Claude Code context" ,
637
+ desc = " Add specified file or directory to Claude Code context with optional line range " ,
577
638
})
578
639
579
640
local terminal_ok , terminal = pcall (require , " claudecode.terminal" )
0 commit comments