Skip to content

Commit 9521fbd

Browse files
author
Bruce Hauman
committed
Decouple ::pre-formatted? flag from ::form-col in partial formatting pipeline
Add dedicated ::pre-formatted? flag so ::form-col is purely column position and format-source uses an explicit signal to skip whole-file formatting. Soften CONFIG.md language for :partial mode.
1 parent 5c3d4b2 commit 9521fbd

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

CONFIG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Controls cljfmt formatting behavior in editing pipelines (default: `true`). Dete
2323

2424
**When to use each setting:**
2525
- `true` - Best for maintaining consistent code style across your project. Note: this reformats the entire file after each edit, which may introduce formatting changes to code you did not intend to modify.
26-
- `:partial` - Recommended for most workflows. Formats only the form being edited (fixing indentation, spacing, etc.) without touching the rest of the file. Prevents collateral formatting changes to unrelated code.
26+
- `:partial` - Useful when you want the edited form to be formatted but don't want to touch the rest of the file. Prevents collateral formatting changes to unrelated code.
2727
- `false` - Useful when working with files that have specific formatting requirements or when you want to preserve manual formatting completely
2828

2929
### `:write-file-guard`

src/clojure_mcp/tools/form_edit/pipeline.clj

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
(s/def ::dry-run (s/nilable #{"diff" "new-source"}))
4444

4545
(s/def ::form-col pos-int?)
46+
(s/def ::pre-formatted? boolean?)
4647

4748
(s/def ::context
4849
(s/keys :req [::file-path]
@@ -51,7 +52,7 @@
5152
::zloc ::offsets ::lint-result ::docstring
5253
::comment-substring ::new-content ::expand-symbols
5354
::diff ::type ::output-source ::nrepl-client-atom ::dry-run
54-
::form-col]))
55+
::form-col ::pre-formatted?]))
5556

5657
;; Pipeline helper functions
5758

@@ -265,7 +266,9 @@
265266
target-col (::form-col ctx)
266267
formatting-options (core/project-formatting-options nrepl-client-map)
267268
formatted (core/format-form-in-isolation new-source target-col formatting-options)]
268-
(assoc ctx ::new-source-code formatted))
269+
(-> ctx
270+
(assoc ::new-source-code formatted)
271+
(assoc ::pre-formatted? true)))
269272
;; Not :partial mode, or position unavailable — pass through
270273
ctx)))
271274

@@ -310,8 +313,8 @@
310313
If formatting fails but the source is syntactically valid,
311314
returns the original source unchanged.
312315
313-
When cljfmt is :partial AND the form was already formatted in isolation
314-
(indicated by ::form-col in context), skips whole-file formatting.
316+
When ::pre-formatted? is true, skips whole-file formatting (the form
317+
was already formatted in isolation by format-new-source-partial).
315318
Otherwise falls back to full-file formatting (e.g., for sexp-edit-pipeline
316319
which doesn't do pre-insertion partial formatting).
317320
@@ -321,9 +324,8 @@
321324
(let [nrepl-client-map (some-> ctx ::nrepl-client-atom deref)
322325
cljfmt-setting (config/get-cljfmt nrepl-client-map)]
323326
(cond
324-
;; :partial mode with pre-formatted form - skip whole-file formatting
325-
;; ::form-col presence means format-new-source-partial already ran
326-
(and (= :partial cljfmt-setting) (::form-col ctx))
327+
;; Form was already formatted in isolation - skip whole-file formatting
328+
(::pre-formatted? ctx)
327329
ctx
328330

329331
;; true (or :partial without pre-formatting) - full-file formatting

test/clojure_mcp/tools/form_edit/pipeline_test.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,18 @@
399399
(is (nil? (::sut/form-col result)))))))
400400

401401
(deftest format-source-partial-skips-whole-file-test
402-
(testing "format-source skips whole-file formatting when cljfmt is :partial"
402+
(testing "format-source skips whole-file formatting when pre-formatted"
403403
;; Set cljfmt to :partial
404404
(config/set-config! *nrepl-client-atom* :cljfmt :partial)
405405
(try
406406
;; Source with intentional bad formatting in the ns form (should be preserved)
407-
;; ::form-col signals that format-new-source-partial already ran
407+
;; ::pre-formatted? signals that format-new-source-partial already ran
408408
(let [source "(ns test.core)\n\n(defn example-fn [x y]\n (+ x y))"
409409
ctx {::sut/nrepl-client-atom *nrepl-client-atom*
410410
::sut/output-source source
411-
::sut/form-col 1}
411+
::sut/pre-formatted? true}
412412
result (sut/format-source ctx)]
413-
;; With :partial + ::form-col, format-source should return the source unchanged
413+
;; With ::pre-formatted?, format-source should return the source unchanged
414414
(is (= source (::sut/output-source result)))
415415
;; The extra spaces in ns should be preserved
416416
(is (str/includes? (::sut/output-source result) "(ns test.core)")))

0 commit comments

Comments
 (0)