[emacs] Fix search and replace without regex
This commit is contained in:
parent
1d1fc38354
commit
1a9ccb2554
1 changed files with 50 additions and 17 deletions
|
|
@ -37,6 +37,15 @@
|
||||||
(require 'project)
|
(require 'project)
|
||||||
(require 'treesit)
|
(require 'treesit)
|
||||||
|
|
||||||
|
(defun doty-tools-bool (v)
|
||||||
|
"Convert V into a boolean, treating :json-false as nil."
|
||||||
|
(and v (not (eq v :json-false))))
|
||||||
|
|
||||||
|
(ert-deftest doty-tools--test--bool ()
|
||||||
|
(should (doty-tools-bool t))
|
||||||
|
(should (not (doty-tools-bool nil)))
|
||||||
|
(should (not (doty-tools-bool :json-false))))
|
||||||
|
|
||||||
;; === Testing Support
|
;; === Testing Support
|
||||||
|
|
||||||
(defun doty-tools--test--find-tool (name)
|
(defun doty-tools--test--find-tool (name)
|
||||||
|
|
@ -51,7 +60,9 @@
|
||||||
This is kinda like what happens inside gptel but that's not accessible."
|
This is kinda like what happens inside gptel but that's not accessible."
|
||||||
(let* ((tool (doty-tools--test--find-tool name))
|
(let* ((tool (doty-tools--test--find-tool name))
|
||||||
;; Ensure we have the correct JSON encoding.
|
;; Ensure we have the correct JSON encoding.
|
||||||
(arg-plist (gptel--json-read-string (gptel--json-encode arg-plist)))
|
(arg-plist (progn
|
||||||
|
;; (message "ARGS: %S" (gptel--json-encode arg-plist))
|
||||||
|
(gptel--json-read-string (gptel--json-encode arg-plist))))
|
||||||
(arg-values (-map (lambda (arg)
|
(arg-values (-map (lambda (arg)
|
||||||
(let ((key (intern (concat ":" (plist-get arg :name)))))
|
(let ((key (intern (concat ":" (plist-get arg :name)))))
|
||||||
(plist-get arg-plist key)))
|
(plist-get arg-plist key)))
|
||||||
|
|
@ -377,10 +388,11 @@ run."
|
||||||
(save-restriction
|
(save-restriction
|
||||||
(widen)
|
(widen)
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(let ((count 0)
|
(let* ((use-regex (doty-tools-bool use-regex))
|
||||||
(matches "")
|
(count 0)
|
||||||
(search-pattern (if use-regex (doty-tools--convert-regex pattern) pattern))
|
(matches "")
|
||||||
(search-fn (if use-regex 're-search-forward 'search-forward)))
|
(search-pattern (if use-regex (doty-tools--convert-regex pattern) pattern))
|
||||||
|
(search-fn (if use-regex 're-search-forward 'search-forward)))
|
||||||
(while (and (funcall search-fn search-pattern nil t)
|
(while (and (funcall search-fn search-pattern nil t)
|
||||||
(< count max-matches))
|
(< count max-matches))
|
||||||
(setq count (1+ count))
|
(setq count (1+ count))
|
||||||
|
|
@ -435,7 +447,7 @@ run."
|
||||||
:pattern "- [ ] Do it!"
|
:pattern "- [ ] Do it!"
|
||||||
:context_lines 0
|
:context_lines 0
|
||||||
:max_matches 1
|
:max_matches 1
|
||||||
:use_regex nil))))
|
:use_regex :json-false))))
|
||||||
|
|
||||||
(should
|
(should
|
||||||
(string-equal "Match 1 (line 1):
|
(string-equal "Match 1 (line 1):
|
||||||
|
|
@ -450,7 +462,7 @@ Match 2 (line 3):
|
||||||
:pattern "- [ ]"
|
:pattern "- [ ]"
|
||||||
:context_lines 0
|
:context_lines 0
|
||||||
:max_matches 1000
|
:max_matches 1000
|
||||||
:use_regex nil)))))))
|
:use_regex :json-false)))))))
|
||||||
|
|
||||||
(ert-deftest doty-tools--test--emacs_search_text-regex ()
|
(ert-deftest doty-tools--test--emacs_search_text-regex ()
|
||||||
"Tests for emacs_search_text without regular expressions."
|
"Tests for emacs_search_text without regular expressions."
|
||||||
|
|
@ -704,7 +716,7 @@ If AT-END is non-nil, insert at end of line, otherwise at beginning."
|
||||||
(gptel-make-tool
|
(gptel-make-tool
|
||||||
:name "emacs_insert_at_line"
|
:name "emacs_insert_at_line"
|
||||||
:function #'doty-tools--insert-at-line
|
:function #'doty-tools--insert-at-line
|
||||||
:description "Insert text at the beginning or end of specified line. Be sure to carefully consider the context of the insertion point when modifying files, to make sure that the line you specify is where the text should actually go."
|
:description "Insert text at the beginning or end of specified line. Blanks are *not* added automatically, so be sure to include newlines where appropriate. Be sure to carefully consider the context of the insertion point when modifying files, to make sure that the line you specify is where the text should actually go."
|
||||||
:args '((:name "buffer_or_file"
|
:args '((:name "buffer_or_file"
|
||||||
:type string
|
:type string
|
||||||
:description "Buffer name or file path")
|
:description "Buffer name or file path")
|
||||||
|
|
@ -719,7 +731,7 @@ If AT-END is non-nil, insert at end of line, otherwise at beginning."
|
||||||
:optional t
|
:optional t
|
||||||
:description "If true, insert at end of line; otherwise at beginning (optional)"))
|
:description "If true, insert at end of line; otherwise at beginning (optional)"))
|
||||||
:category "editing"
|
:category "editing"
|
||||||
:confirm t
|
:confirm nil
|
||||||
:include t)
|
:include t)
|
||||||
|
|
||||||
(ert-deftest doty-tools--test--emacs_insert_line ()
|
(ert-deftest doty-tools--test--emacs_insert_line ()
|
||||||
|
|
@ -750,12 +762,13 @@ If REPLACE-ALL is non-nil, replace all occurrences, otherwise just the
|
||||||
(save-restriction
|
(save-restriction
|
||||||
(widen)
|
(widen)
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(let ((count 0)
|
(let* ((use-regex (doty-tools-bool use-regex))
|
||||||
(search-pattern (if use-regex
|
(count 0)
|
||||||
(doty-tools--convert-regex from-text)
|
(search-pattern (if use-regex
|
||||||
from-text))
|
(doty-tools--convert-regex from-text)
|
||||||
(search-fn (if use-regex 're-search-forward 'search-forward))
|
from-text))
|
||||||
(case-fold-search nil))
|
(search-fn (if use-regex 're-search-forward 'search-forward))
|
||||||
|
(case-fold-search nil))
|
||||||
(while (and (funcall search-fn search-pattern nil t)
|
(while (and (funcall search-fn search-pattern nil t)
|
||||||
(or replace-all (= count 0)))
|
(or replace-all (= count 0)))
|
||||||
(setq count (1+ count))
|
(setq count (1+ count))
|
||||||
|
|
@ -789,9 +802,29 @@ If REPLACE-ALL is non-nil, replace all occurrences, otherwise just the
|
||||||
:type boolean
|
:type boolean
|
||||||
:description "Replace all occurrences if true"))
|
:description "Replace all occurrences if true"))
|
||||||
:category "editing"
|
:category "editing"
|
||||||
:confirm t
|
:confirm nil
|
||||||
:include t)
|
:include t)
|
||||||
|
|
||||||
|
(ert-deftest doty-tools--test--emacs_replace_text-no-regex ()
|
||||||
|
"Test emacs_replace_text with no regex."
|
||||||
|
(with-temp-buffer
|
||||||
|
(insert "- [X] `size: Int`\n")
|
||||||
|
(insert "- [ ] `knownSize: Int`\n")
|
||||||
|
(insert "- [ ] `apply(i: Int): Char`\n")
|
||||||
|
(doty-tools--test--invoke-tool
|
||||||
|
"emacs_replace_text" (list :buffer_or_file (buffer-name)
|
||||||
|
:from_text "- [ ] `knownSize: Int`"
|
||||||
|
:to_text "- [X] `knownSize: Int`"
|
||||||
|
:use_regex :json-false
|
||||||
|
:replace_all :json-false))
|
||||||
|
(should (equal (buffer-string)
|
||||||
|
"- [X] `size: Int`
|
||||||
|
- [X] `knownSize: Int`
|
||||||
|
- [ ] `apply(i: Int): Char`
|
||||||
|
"))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defun doty-tools--delete-lines (buffer-or-file start-line &optional end-line)
|
(defun doty-tools--delete-lines (buffer-or-file start-line &optional end-line)
|
||||||
"Delete lines from START-LINE to END-LINE in BUFFER-OR-FILE.
|
"Delete lines from START-LINE to END-LINE in BUFFER-OR-FILE.
|
||||||
If END-LINE is not provided, only delete START-LINE."
|
If END-LINE is not provided, only delete START-LINE."
|
||||||
|
|
@ -826,7 +859,7 @@ If END-LINE is not provided, only delete START-LINE."
|
||||||
:optional t
|
:optional t
|
||||||
:description "Last line to delete (optional - single line if omitted)"))
|
:description "Last line to delete (optional - single line if omitted)"))
|
||||||
:category "editing"
|
:category "editing"
|
||||||
:confirm t
|
:confirm nil
|
||||||
:include t)
|
:include t)
|
||||||
|
|
||||||
;; === System tools
|
;; === System tools
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue