Skip to content

Commit

Permalink
Also macroexpand some->, some->>
Browse files Browse the repository at this point in the history
  • Loading branch information
vemv committed Sep 29, 2023
1 parent f7d872d commit ea7d9a0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

- [#109](https://github.com/alexander-yakushev/compliment/issues/109): Introduce
support for offering completions within the context of a `some->`
and `some->>` call.

### 0.4.2 (2023-09-17)

- [#107](https://github.com/alexander-yakushev/compliment/issues/107): Infer the
Expand Down
26 changes: 21 additions & 5 deletions src/compliment/context.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,30 @@

(defn macroexpand-form [ns form]
(postwalk (fn [x]
(if (and (seq? x)
(-> x first symbol?)
(let [call? (and (seq? x)
(-> x first symbol?))
resolved (when call?
(ns-resolve ns (first x)))]
(cond
(and call?
(contains? #{#'clojure.core/->
#'clojure.core/->>
#'clojure.core/doto}
(ns-resolve ns (first x))))
(macroexpand-1 x)
x))
resolved))
(macroexpand-1 x)

;; The macroexpansion of some-> is trickier than that of ->,
;; so we macroexpand -> instead,
;; which is equivalent for our purposes:
(and call?
(= resolved #'clojure.core/some->))
(macroexpand-1 (cons `-> (rest x)))

(and call?
(= resolved #'clojure.core/some->>))
(macroexpand-1 (cons `->> (rest x)))

:else x)))
form))

(defn parse-context
Expand Down
12 changes: 10 additions & 2 deletions test/compliment/sources/t_class_members.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@

(deftest thread-first-test
(in-ns 'compliment.sources.t-class-members)
(fact "`->` works with Compliment"
(fact "`->` and `some->` work with Compliment"
(strip-tags (src/members-candidates ".int" (-ns) (ctx/cache-context
"(-> thread __prefix__)")))
=> (just '(".interrupt"))

(strip-tags (src/members-candidates ".int" (-ns) (ctx/cache-context
"(some-> thread __prefix__)")))
=> (just '(".interrupt"))

(strip-tags (src/members-candidates ".int" (-ns) (ctx/cache-context
"(-> thread __prefix__ FOO)")))
=> (just '(".interrupt"))
Expand Down Expand Up @@ -46,11 +50,15 @@
=> (just '(".interrupt"))))

(deftest thread-last-test
(fact "`->>` works with Compliment"
(fact "`->>` and `some->>` work with Compliment"
(strip-tags (src/members-candidates ".su" (-ns) (ctx/cache-context
"(->> [] (clojure.string/join \"a\") __prefix__)")))
=> (just '(".subSequence" ".substring") :in-any-order)

(strip-tags (src/members-candidates ".su" (-ns) (ctx/cache-context
"(some->> [] (clojure.string/join \"a\") __prefix__)")))
=> (just '(".subSequence" ".substring") :in-any-order)

(strip-tags (src/members-candidates ".su" (-ns) (ctx/cache-context
"(->> [] (clojure.string/join \"a\") __prefix__ FOO)")))
=> (just '(".subSequence" ".substring") :in-any-order)
Expand Down

0 comments on commit ea7d9a0

Please sign in to comment.