|
43 | 43 |
|
44 | 44 | (s/def ::nrepl-client-atom (s/nilable #(instance? clojure.lang.Atom %))) |
45 | 45 |
|
46 | | -;; Context map that flows through the pipeline |
| 46 | +(s/def ::dry-run (s/nilable #{"diff" "new-source"})) |
| 47 | + |
47 | 48 | (s/def ::context |
48 | 49 | (s/keys :req [::file-path] |
49 | 50 | :opt [::source ::old-content ::new-source-code ::top-level-def-name |
50 | 51 | ::top-level-def-type ::edit-type ::error ::message |
51 | 52 | ::zloc ::offsets ::lint-result ::docstring |
52 | 53 | ::comment-substring ::new-content ::expand-symbols |
53 | | - ::diff ::type ::output-source ::nrepl-client-atom])) |
| 54 | + ::diff ::type ::output-source ::nrepl-client-atom ::dry-run])) |
54 | 55 |
|
55 | 56 | ;; Pipeline helper functions |
56 | 57 |
|
|
536 | 537 | - ctx: The final context map from the pipeline |
537 | 538 | |
538 | 539 | 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" |
540 | 541 | [ctx] |
541 | 542 | (if (::error ctx) |
542 | 543 | {:error true |
543 | 544 | :message (::message ctx)} |
544 | | - (let [result-map {:error false}] |
| 545 | + (let [dry-run (::dry-run ctx) |
| 546 | + result-map {:error false}] |
545 | 547 | (cond-> result-map |
546 | 548 | (::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)]))))) |
549 | 561 |
|
550 | 562 | ;; Pipeline function definitions |
551 | 563 |
|
|
558 | 570 | - form-type: Type of form (e.g., 'defn', 'defmethod') |
559 | 571 | - content-str: New content to insert |
560 | 572 | - 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 |
563 | 575 | |
564 | 576 | 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}] |
566 | 578 | (let [ctx {::file-path file-path |
567 | 579 | ::top-level-def-name form-name |
568 | 580 | ::top-level-def-type form-type |
569 | 581 | ::new-source-code content-str |
570 | 582 | ::edit-type edit-type |
| 583 | + ::dry-run dry-run |
571 | 584 | ::nrepl-client-atom nrepl-client-atom |
572 | 585 | ::config config}] |
573 | 586 | (thread-ctx |
|
587 | 600 | format-source |
588 | 601 | determine-file-type |
589 | 602 | 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)))))) |
593 | 612 |
|
594 | 613 | (defn docstring-edit-pipeline |
595 | 614 | "Pipeline for editing a docstring in a file. |
|
0 commit comments