Skip to content

(:replace) - Operators

replace selection with literal text or captured values

clojure
(:replace val? sub? reconcile?)

Where:

  1. val: what to replace (omit to replace whatever is matched at this position) *
  2. sub: replacement text (omit to delete the matched value) *
  3. reconcile: eat syntactic tokens adjacent to the selection (e.g., a comma) when dropping *

Expanding into:

clojure
(:#replace node \1 \2 \3)

Guides

Behavior

  • When sub is omitted, the matched value is dropped (deleted) from the output.
  • Supports regex capture groups ($1, $2, etc.) from pattern-based selectors.
  • Supports named captures via (:capture name ...) which can be referenced as $name in the substitution string.
  • Captures are scoped per match - each match site maintains isolated capture values.

Examples

Replace all instances of a deprecated API call with its modern equivalent.

clojure
(:replace (call oldAPI) "newAPI()")

Selects in lines { 1 }:

typescript
x(oldAPI())
y(process())

Drop the second argument from specific function calls during refactoring.

clojure
(call processData (:replace (arg 2)))

Selects in lines { 1 }:

typescript
processData(input, legacy)
otherCall(a, b)

Rename string literals using regex capture groups.

clojure
(:replace (str /old_(.+)/) "new_$1")

Selects in lines { 1, 2 }:

typescript
const key = "old_users"
const other = "old_settings"

Reorder function arguments by capturing and rearranging them.

clojure
(call log
  (arg 1
    (:and
      (:capture first)
      (:replace _ "$rest, $first")))
  (arg 2.. (:capture rest (:into))))

Selects in lines { 1 }:

typescript
log("a", "b", "c")

Add a parameter to function calls using captures from other arguments.

clojure
(call fetch
  (:and
    (arg 1 (:capture url (str)))
    (arg 2 (:replace _ "$url_config"))))

Selects in lines { 1 }:

typescript
fetch("/api", options)

Arguments

val

  1. inherit

sub

  1. String

Does NOT support: CompositionFree-form SelectionRefinementReplacement

reconcile

  1. "auto"
  2. "right"
  3. "left"

Does NOT support: CompositionFree-form SelectionRefinementReplacement

Copyright © 2022-present Semantic Works, Inc.