Skip to content

Commit a84a8f1

Browse files
author
Bruce Hauman
committed
WIP: the dry run is not working on the clojure_edit yet
1 parent 0a41ebe commit a84a8f1

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/clojure_mcp/tools/form_edit/combined_edit_tool.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,21 @@ Note: For `defmethod` forms, be sure to include the dispatch value (`area :recta
154154

155155
;; Execute tool implementation
156156
(defmethod tool-system/execute-tool :clojure-edit-form [{:keys [nrepl-client-atom] :as tool} inputs]
157-
(let [{:keys [file_path form_name form_type operation content]} inputs
157+
(let [{:keys [file_path form_name form_type operation content dry-run]} inputs
158158
edit-type (case operation
159159
"replace" :replace
160160
"insert_before" :before
161161
"insert_after" :after)
162-
result (pipeline/edit-form-pipeline file_path form_name form_type content edit-type tool)
162+
result (pipeline/edit-form-pipeline file_path form_name form_type content edit-type dry-run tool)
163163
formatted-result (pipeline/format-result result)]
164164
formatted-result))
165165

166166
;; Format results implementation
167-
(defmethod tool-system/format-results :clojure-edit-form [_ {:keys [error message diff]}]
167+
(defmethod tool-system/format-results :clojure-edit-form [_ {:keys [error message diff new-source]}]
168168
(if error
169169
{:result [message]
170170
:error true}
171-
{:result [diff]
171+
{:result [(or new-source diff)]
172172
:error false}))
173173

174174
;; Tool registration function

src/clojure_mcp/tools/form_edit/pipeline.clj

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@
4343

4444
(s/def ::nrepl-client-atom (s/nilable #(instance? clojure.lang.Atom %)))
4545

46-
;; Context map that flows through the pipeline
46+
(s/def ::dry-run (s/nilable #{"diff" "new-source"}))
47+
4748
(s/def ::context
4849
(s/keys :req [::file-path]
4950
:opt [::source ::old-content ::new-source-code ::top-level-def-name
5051
::top-level-def-type ::edit-type ::error ::message
5152
::zloc ::offsets ::lint-result ::docstring
5253
::comment-substring ::new-content ::expand-symbols
53-
::diff ::type ::output-source ::nrepl-client-atom]))
54+
::diff ::type ::output-source ::nrepl-client-atom ::dry-run]))
5455

5556
;; Pipeline helper functions
5657

@@ -536,16 +537,27 @@
536537
- ctx: The final context map from the pipeline
537538
538539
Returns:
539-
- A map with :error, :message, and possibly :offsets and :result keys"
540+
- A map with :error, :message, and possibly :diff or :new-source, and :offsets keys"
540541
[ctx]
541542
(if (::error ctx)
542543
{:error true
543544
:message (::message ctx)}
544-
(let [result-map {:error false}]
545+
(let [dry-run (::dry-run ctx)
546+
result-map {:error false}]
545547
(cond-> result-map
546548
(::offsets ctx) (assoc :offsets (::offsets ctx))
547-
(::output-source ctx) (assoc :result [(::output-source ctx)])
548-
(::diff ctx) (assoc :diff (::diff ctx))))))
549+
550+
;; Return new-source if dry-run is "new-source"
551+
(= dry-run "new-source")
552+
(assoc :new-source (::output-source ctx))
553+
554+
;; Otherwise return diff (default behavior and for "diff" dry-run)
555+
(and (::diff ctx) (not= dry-run "new-source"))
556+
(assoc :diff (::diff ctx))
557+
558+
;; Legacy: include :result if output-source exists
559+
(and (::output-source ctx) (not dry-run))
560+
(assoc :result [(::output-source ctx)])))))
549561

550562
;; Pipeline function definitions
551563

@@ -558,16 +570,17 @@
558570
- form-type: Type of form (e.g., 'defn', 'defmethod')
559571
- content-str: New content to insert
560572
- edit-type: Type of edit (:replace, :before, :after)
561-
- nrepl-client-atom: Atom containing the nREPL client (optional)
562-
- config: Optional tool configuration map
573+
- dry-run: Optional string, either \"diff\" or \"new-source\" to skip actual file write
574+
- config: Optional tool configuration map with :nrepl-client-atom
563575
564576
Returns a context map with the result of the operation"
565-
[file-path form-name form-type content-str edit-type {:keys [nrepl-client-atom] :as config}]
577+
[file-path form-name form-type content-str edit-type dry-run {:keys [nrepl-client-atom] :as config}]
566578
(let [ctx {::file-path file-path
567579
::top-level-def-name form-name
568580
::top-level-def-type form-type
569581
::new-source-code content-str
570582
::edit-type edit-type
583+
::dry-run dry-run
571584
::nrepl-client-atom nrepl-client-atom
572585
::config config}]
573586
(thread-ctx
@@ -587,9 +600,15 @@
587600
format-source
588601
determine-file-type
589602
generate-diff
590-
save-file
591-
update-file-timestamp
592-
highlight-form)))
603+
;; Skip file operations if dry-run is set
604+
(fn [ctx]
605+
(log/info [:CONTEXT (pr-str (keys ctx))])
606+
(if (::dry-run ctx)
607+
ctx
608+
(-> ctx
609+
save-file
610+
update-file-timestamp
611+
highlight-form))))))
593612

594613
(defn docstring-edit-pipeline
595614
"Pipeline for editing a docstring in a file.

0 commit comments

Comments
 (0)