Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp-defun' is the same as -defun' #2306

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 1 addition & 54 deletions lsp-protocol.el
Original file line number Diff line number Diff line change
Expand Up @@ -174,60 +174,7 @@ Allowed params: %s" interface (reverse (-map #'cl-first params)))
(defalias 'lsp-empty? 'ht-empty?)
(defalias 'lsp-copy 'ht-copy))

(defmacro lsp-defun (name match-form &rest body)
"Define a function named NAME.
The function destructures its input as MATCH-FORM then executes BODY.

Note that you have to enclose the MATCH-FORM in a pair of parens,
such that:

(-defun (x) body)
(-defun (x y ...) body)

has the usual semantics of `defun'. Furthermore, these get
translated into a normal `defun', so there is no performance
penalty.

See `-let' for a description of the destructuring mechanism."
(declare (doc-string 3) (indent defun)
(debug (&define name sexp
[&optional stringp]
[&optional ("declare" &rest sexp)]
[&optional ("interactive" interactive)]
def-body)))
(cond
((nlistp match-form)
(signal 'wrong-type-argument (list #'listp match-form)))
;; no destructuring, so just return regular defun to make things faster
((-all? #'symbolp match-form)
`(defun ,name ,match-form ,@body))
(t
(-let* ((inputs (--map-indexed (list it (make-symbol (format "input%d" it-index))) match-form))
((body docs) (cond
;; only docs
((and (stringp (car body))
(not (cdr body)))
(list body (car body)))
;; docs + body
((stringp (car body))
(list (cdr body) (car body)))
;; no docs
(t (list body))))
((body interactive-form) (cond
;; interactive form
((and (listp (car body))
(eq (caar body) 'interactive))
(list (cdr body) (car body)))
;; no interactive form
(t (list body)))))
;; TODO: because inputs to the defun are evaluated only once,
;; -let* need not to create the extra bindings to ensure that.
;; We should find a way to optimize that. Not critical however.
`(defun ,name ,(-map #'cadr inputs)
,@(when docs (list docs))
,@(when interactive-form (list interactive-form))
(-let* ,inputs ,@body))))))

(defalias 'lsp-defun '-defun)



Expand Down