|
580 | 580 | (filter #(match-multi-sexp match-sexprs %)) |
581 | 581 | first)) |
582 | 582 |
|
583 | | -(defn find-and-edit-one-multi-sexp [zloc operation match-form new-form] |
584 | | - {:pre [(#{:insert-before :insert-after :replace} operation) zloc (string? match-form) (string? new-form)]} |
585 | | - ;; no-op |
586 | | - (when-not (and (str/blank? new-form) (#{:insert-before :insert-after} operation)) |
587 | | - (let [new-node (when-not (str/blank? new-form) (p/parse-string-all new-form)) |
588 | | - match-sexprs (str-forms->sexps match-form)] |
589 | | - (when-let [found-loc (find-multi-sexp zloc match-sexprs)] |
590 | | - (condp = operation |
591 | | - :insert-before (insert-before-multi found-loc match-sexprs new-node) |
592 | | - :insert-after (insert-after-multi found-loc match-sexprs new-node) |
593 | | - (replace-multi found-loc match-sexprs new-form)))))) |
594 | | - |
595 | | -(defn find-and-edit-all-multi-sexp [zloc operation match-form new-form] |
596 | | - {:pre [(#{:insert-before :insert-after :replace} operation) zloc (string? match-form) (string? new-form)]} |
597 | | - (when-not (and (str/blank? new-form) (#{:insert-before :insert-after} operation)) |
598 | | - (loop [loc zloc |
599 | | - locations []] |
600 | | - (if-let [{:keys [after-loc edit-span-loc]} |
601 | | - (find-and-edit-one-multi-sexp loc operation match-form new-form)] |
602 | | - (recur after-loc (conj locations edit-span-loc)) |
603 | | - (when-not (empty? locations) |
604 | | - ;; this is a location after the last match |
605 | | - ;; z/root-string on this will produce the final edited form |
606 | | - {:zloc loc |
607 | | - :locations locations}))))) |
608 | | - |
609 | | -(defn find-and-edit-multi-sexp* [zloc match-form new-form {:keys [operation all?]}] |
| 583 | +(defn find-and-edit-one-multi-sexp |
| 584 | + ([zloc operation match-form new-form] |
| 585 | + (find-and-edit-one-multi-sexp zloc operation match-form new-form nil)) |
| 586 | + ([zloc operation match-form new-form reindent-fn] |
| 587 | + {:pre [(#{:insert-before :insert-after :replace} operation) zloc (string? match-form) (string? new-form)]} |
| 588 | + ;; no-op |
| 589 | + (when-not (and (str/blank? new-form) (#{:insert-before :insert-after} operation)) |
| 590 | + (let [match-sexprs (str-forms->sexps match-form)] |
| 591 | + (when-let [found-loc (find-multi-sexp zloc match-sexprs)] |
| 592 | + (let [adjusted-form (if reindent-fn |
| 593 | + (if-let [col (some-> (z/position found-loc) second)] |
| 594 | + (reindent-fn new-form col) |
| 595 | + new-form) |
| 596 | + new-form) |
| 597 | + new-node (when-not (str/blank? adjusted-form) |
| 598 | + (p/parse-string-all adjusted-form))] |
| 599 | + (condp = operation |
| 600 | + :insert-before (insert-before-multi found-loc match-sexprs new-node) |
| 601 | + :insert-after (insert-after-multi found-loc match-sexprs new-node) |
| 602 | + (replace-multi found-loc match-sexprs adjusted-form)))))))) |
| 603 | + |
| 604 | +(defn find-and-edit-all-multi-sexp |
| 605 | + ([zloc operation match-form new-form] |
| 606 | + (find-and-edit-all-multi-sexp zloc operation match-form new-form nil)) |
| 607 | + ([zloc operation match-form new-form reindent-fn] |
| 608 | + {:pre [(#{:insert-before :insert-after :replace} operation) zloc (string? match-form) (string? new-form)]} |
| 609 | + (when-not (and (str/blank? new-form) (#{:insert-before :insert-after} operation)) |
| 610 | + (loop [loc zloc |
| 611 | + locations []] |
| 612 | + (if-let [{:keys [after-loc edit-span-loc]} |
| 613 | + (find-and-edit-one-multi-sexp loc operation match-form new-form reindent-fn)] |
| 614 | + (recur after-loc (conj locations edit-span-loc)) |
| 615 | + (when-not (empty? locations) |
| 616 | + ;; this is a location after the last match |
| 617 | + ;; z/root-string on this will produce the final edited form |
| 618 | + {:zloc loc |
| 619 | + :locations locations})))))) |
| 620 | + |
| 621 | +(defn find-and-edit-multi-sexp* [zloc match-form new-form {:keys [operation all? reindent-fn]}] |
610 | 622 | (if all? |
611 | | - (find-and-edit-all-multi-sexp zloc operation match-form new-form) |
612 | | - (when-let [{:keys [after-loc edit-span-loc]} (find-and-edit-one-multi-sexp zloc operation match-form new-form)] |
| 623 | + (find-and-edit-all-multi-sexp zloc operation match-form new-form reindent-fn) |
| 624 | + (when-let [{:keys [after-loc edit-span-loc]} (find-and-edit-one-multi-sexp zloc operation match-form new-form reindent-fn)] |
613 | 625 | ;; this is a location after the last match |
614 | 626 | ;; z/root-string on this will produce the final edited form |
615 | 627 | {:zloc after-loc |
|
0 commit comments