diff options
| author | Vineet Kumar <git@vineetk.net> | 2026-05-26 01:20:57 -0400 |
|---|---|---|
| committer | Vineet Kumar <git@vineetk.net> | 2026-05-26 03:21:05 -0400 |
| commit | e256030cf5a23a652bb310bd6f2fbd1f2f7d9ffd (patch) | |
| tree | 3a6d6e9d7b9cdbca669507f3843a2e094748b92b | |
| parent | b289ee4ddbcd791f23537e9a7ad1cc2c1f839eda (diff) | |
fix grep tool
| -rw-r--r-- | llm-tools.el | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/llm-tools.el b/llm-tools.el index c0f859e..905a072 100644 --- a/llm-tools.el +++ b/llm-tools.el | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | ;; read_file (hl-file-read) | 5 | ;; read_file (hl-file-read) |
| 6 | (gptel-make-tool | 6 | (gptel-make-tool |
| 7 | :name "read_file" | 7 | :name "read_file" |
| 8 | :function 'llm-tools--hl-file-read | 8 | :function #'llm-tools--hl-file-read |
| 9 | :description "\ | 9 | :description "\ |
| 10 | Read a file (or a range of lines) and return its contents in hashline | 10 | Read a file (or a range of lines) and return its contents in hashline |
| 11 | format. Each line is represented as LINE_NUMBER HASH|CONTENT, where | 11 | format. Each line is represented as LINE_NUMBER HASH|CONTENT, where |
| @@ -346,23 +346,32 @@ or editing them." | |||
| 346 | :include t) | 346 | :include t) |
| 347 | 347 | ||
| 348 | ;; grep (just grep (or git grep) :D) | 348 | ;; grep (just grep (or git grep) :D) |
| 349 | (defun llm-tools--grep (regexp &optional glob path) | ||
| 350 | (with-temp-buffer | ||
| 351 | (if (magit-gitdir) | ||
| 352 | (let ((pathspec (cond | ||
| 353 | ((and path glob) | ||
| 354 | (concat (directory-file-name path) "/" glob)) | ||
| 355 | (glob glob) | ||
| 356 | (path path) | ||
| 357 | (t ".")))) | ||
| 358 | (call-process "git" nil t nil | ||
| 359 | "grep" "-rnEH" | ||
| 360 | regexp "--" pathspec)) | ||
| 361 | (apply #'call-process | ||
| 362 | (append | ||
| 363 | '("grep" nil t nil) | ||
| 364 | (delq nil | ||
| 365 | `("-rnEH" | ||
| 366 | ,(if glob (concat "--include=" glob)) | ||
| 367 | ,regexp | ||
| 368 | ,(or path ".")))))) | ||
| 369 | (buffer-string))) | ||
| 370 | |||
| 349 | (gptel-make-tool | 371 | (gptel-make-tool |
| 350 | :name "grep" | 372 | :name "grep" |
| 351 | :function (lambda (regexp &optional glob path) | 373 | :function (lambda (regexp &optional glob path) |
| 352 | (with-temp-buffer | 374 | (llm-tools--grep regexp glob path)) |
| 353 | (if (magit-gitdir) | ||
| 354 | (call-process "git" nil t nil | ||
| 355 | "grep" | ||
| 356 | "-rnEH" | ||
| 357 | regexp | ||
| 358 | (and glob (format "--include=%s" glob)) | ||
| 359 | (or path ".")) | ||
| 360 | (call-process "grep" nil t nil | ||
| 361 | "-rnEH" | ||
| 362 | regexp | ||
| 363 | (and glob (format "--include=%s" glob)) | ||
| 364 | (or path "."))) | ||
| 365 | (buffer-string))) | ||
| 366 | :description "\ | 375 | :description "\ |
| 367 | Recursively search for lines matching a regexp pattern in files under a | 376 | Recursively search for lines matching a regexp pattern in files under a |
| 368 | given path. Uses `git grep` when inside a Git repository, otherwise falls | 377 | given path. Uses `git grep` when inside a Git repository, otherwise falls |
| @@ -379,17 +388,17 @@ omit it to search from the current directory." | |||
| 379 | :type string | 388 | :type string |
| 380 | :description "\ | 389 | :description "\ |
| 381 | Regular expression pattern to search for. Uses GNU grep extended syntax.") | 390 | Regular expression pattern to search for. Uses GNU grep extended syntax.") |
| 382 | (:name "path" | ||
| 383 | :type string | ||
| 384 | :description "\ | ||
| 385 | Optional file or directory path to search within, relative to the | ||
| 386 | working directory. Defaults to current directory if omitted." | ||
| 387 | :optional t) | ||
| 388 | (:name "glob" | 391 | (:name "glob" |
| 389 | :type string | 392 | :type string |
| 390 | :description "\ | 393 | :description "\ |
| 391 | Optional restricts search to files matching the glob | 394 | Optional restricts search to files matching the glob |
| 392 | pattern. (e.g. \"*.el\", \"*.org\", \"*.c\")" | 395 | pattern. (e.g. \"*.el\", \"*.org\", \"*.c\")" |
| 396 | :optional t) | ||
| 397 | (:name "path" | ||
| 398 | :type string | ||
| 399 | :description "\ | ||
| 400 | Optional file or directory path to search within, relative to the | ||
| 401 | working directory. Defaults to current directory if omitted." | ||
| 393 | :optional t)) | 402 | :optional t)) |
| 394 | :async nil | 403 | :async nil |
| 395 | :confirm nil | 404 | :confirm nil |
