Skip to content

Commit

Permalink
feat(autocmd): deprecate ^<.+> pattern in sym/list to set Lua callb…
Browse files Browse the repository at this point in the history
…ack (#197)

* refactor(utils): add `vim-callback-format?`

* refactor(autocmd): split `<command>` detection to deprecate it later

* feat(autocmd): deprecate `^<.+>` pattern as Lua callback

* test(autocmd): ensure compatibility on `<Cmd>format-callback`
  • Loading branch information
aileot authored Feb 3, 2023
1 parent 7330968 commit 5b970cf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
40 changes: 37 additions & 3 deletions fnl/nvim-laurel/macros.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@
(and (list? x) ;
(contains? [`fn `hashfn `lambda `partial] (first x))))

(fn vim-callback-format? [callback]
"Tell if `callback` is to be interpreted in Vim script just by the
`callback` format.
@param callback any
@return boolean"
;; TODO: Include the `str?` check after removing the deprecated formats.
(or ;; (str? callback) ;
(and (sym? callback) ;
(-> (->str callback) (: :match "^<.+>")))
(and (list? callback) ;
(-> (->str (first callback)) (: :match "^<.+>")))))

;; Specific Utils ///1

(lambda error* [msg]
Expand Down Expand Up @@ -350,8 +362,16 @@
(when (and (or extra-opts.<command> extra-opts.ex)
(or extra-opts.<callback> extra-opts.cb))
(error* "cannot set both <command>/ex and <callback>/cb."))
(if (or extra-opts.<command> extra-opts.ex vim?)
(var vim-cb? false)
(if ;; TODO: Deprecate `<command>` option.
(or extra-opts.<command> extra-opts.ex)
(set extra-opts.command callback)
vim?
(set extra-opts.command callback)
(vim-callback-format? callback)
;; TODO: Remove vim-cb? check on v0.6.0.
;; (set extra-opts.command callback)
(set vim-cb? true)
(or extra-opts.<callback> extra-opts.cb ;
(sym? callback) ;
(anonymous-function? callback) ;
Expand All @@ -371,8 +391,22 @@
(assert-compile (nand extra-opts.pattern extra-opts.buffer)
"cannot set both pattern and buffer for the same autocmd"
extra-opts)
(let [api-opts (merge-api-opts (autocmd/->compatible-opts! extra-opts)
?api-opts)]
(let [api-opts ;
(merge-api-opts (autocmd/->compatible-opts! extra-opts)
;; TODO: Remove all the if-expr except `?api-opts` here to set `callback` to `command`.
(if vim-cb?
`(vim.tbl_extend :keep (or ,?api-opts {})
(let [cb# ,callback
str?# (= :string
(type cb#))]
{:command (when str?#
cb#)
:callback (when (not str?#)
,(deprecate "symbol which, or list whose first symbol, matches \"^<.+>\" to set Lua callback"
"another name"
:v0.6.0
`cb#))}))
?api-opts))]
`(vim.api.nvim_create_autocmd ,events ,api-opts)))))

(fn autocmd? [args]
Expand Down
27 changes: 26 additions & 1 deletion tests/spec/autocmd_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
(local default-command :default-command)
(local default {:multi {:sym #:default.multi.sym}})

(local <default>-command :<default>-command)
;; TODO: Remove it on removing the support for Lua callback.
(local <default>-str-callback #:<default>-str-callback)
(local <default>-callback-callback ##:<default>-callback-callback)

(lambda get-autocmds [?opts]
(let [opts (collect [k v (pairs (or ?opts {})) ;
&into {:group default-augroup}]
Expand Down Expand Up @@ -187,12 +192,32 @@
(let [cb :callback
opts {:nested true}]
(autocmd! default-augroup default-event cb opts)))))
(describe :<Cmd>pattern
(it "symbol will be set to 'command'"
(au! default-augroup default-event [:pat1] <default>-command)
(let [au (get-first-autocmd {:pattern :pat1})]
(assert.is.same <default>-command au.command)))
(it "list will be set to 'command'"
(au! default-augroup default-event [:pat1] (<default>-str-callback))
(let [au (get-first-autocmd {:pattern :pat1})]
(assert.is.same (<default>-str-callback) au.command))))
(describe "with &vim indicator"
(it "set callback to `command`"
(au! default-augroup default-event [:pat1] &vim default-command)
(let [[autocmd1] (get-autocmds {:pattern :pat1})]
(assert.is.same default-command autocmd1.command))))
(describe "(deprecated)"
(describe "(Deprecated, v0.6.0 will not support it)"
(describe :<Cmd>pattern
(it "symbol can set Lua function as callback"
(au! default-augroup default-event [:pat1] <default>-str-callback)
(let [au (get-first-autocmd {:pattern :pat1})]
(assert.is.same <default>-str-callback au.callback)))
(it "list can set Lua function as callback"
(let [desc :<default>]
(au! default-augroup default-event [:pat1] [:desc desc]
(<default>-callback-callback))
(let [au (get-first-autocmd {:pattern :pat1})]
(assert.is.same desc au.desc)))))
(describe :augroup+
(it "gets an existing augroup id"
(let [id (augroup! default-augroup)]
Expand Down

0 comments on commit 5b970cf

Please sign in to comment.