rename spacebind -> spacemacs|spacebind

Also fix stuff.
This commit is contained in:
JAremko 2020-03-01 11:42:49 +02:00 committed by Eugene Yaremenko
parent bb0a118344
commit 0753287864
4 changed files with 627 additions and 373 deletions

View File

@ -1,4 +1,4 @@
;;; core-spacebind.el --- Spacemacs Core File
;;; core-spacebind.el --- Spacemacs Core File -*- lexical-binding: t -*-
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
@ -11,12 +11,115 @@
(require 'core-keybindings)
(defvar spacebind--eager-bind nil
"If true bind keys right after `spacmeacs|spacebind' macro-expanse.
Otherwise binding happens at the next event loop.")
;; Binding stacks
(defvar spacebind--bs-add-minor-mode-replacements '()
"Binding stack for `spacemacs/add-key-based-replacements-for-minor-mode'.")
(defvar spacebind--bs-add-major-mode-replacements '()
"Binding stack for `which-key-add-major-mode-key-based-replacements'.")
(defvar spacebind--bs-declare-prefix '()
"Binding stack for `spacemacs/declare-prefix'.")
(defvar spacebind--bs-declare-prefix-for-mode '()
"Binding stack for `spacemacs/declare-prefix-for-mode'.")
(defvar spacebind--bs-set-leader-keys '()
"Binding stack for `spacemacs/set-leader-keys'.")
(defvar spacebind--bs-set-leader-keys-for-major-mode '()
"Binding stack for `spacemacs/set-leader-keys-for-major-mode'.")
(defvar spacebind--bs-set-leader-keys-for-minor-mode '()
"Binding stack for `spacemacs/set-leader-keys-for-minor-mode'.")
(defvar spacebind--bs-add-global-replacements '()
"Binding stack for `which-key-add-key-based-replacements'.")
(defvar spacebind--timer [t]
"`run-with-idle-timer' return value for `spacebind//process-bind-stack'.")
(defun spacebind//process-bind-stack ()
"Drains bind stacks and binds keys and prefixes."
(unwind-protect
(progn
;; `spacemacs/add-key-based-replacements-for-minor-mode'
(dolist (args spacebind--bs-add-minor-mode-replacements)
(let ((mode (car args))
(keys (string-join (append `(,(spacemacs/leader-key))
(cadr args))
" "))
(label (caddr args)))
(spacemacs/add-key-based-replacements-for-minor-mode
mode keys label)))
;; `which-key-add-major-mode-key-based-replacements'
(dolist (args spacebind--bs-add-major-mode-replacements)
(let ((mode (car args))
(keys (string-join (append `(,(spacemacs/leader-key))
`(,(spacemacs/major-mode-prefix))
(cadr args))
" "))
(label (caddr args)))
(which-key-add-major-mode-key-based-replacements mode keys label)))
;; `spacemacs/declare-prefix'
(dolist (args spacebind--bs-declare-prefix)
(let ((prefix (string-join (car args) " "))
(label (cadr args)))
(spacemacs/declare-prefix prefix label)))
;; `spacemacs/declare-prefix-for-mode'
(dolist (args spacebind--bs-declare-prefix-for-mode)
(let ((mode (car args))
(prefix (string-join (cadr args) " "))
(label (caddr args)))
(spacemacs/declare-prefix prefix label)))
;; `spacemacs/set-leader-keys'
(dolist (args spacebind--bs-set-leader-keys)
(let ((keys (string-join (car args) " "))
(fn-sym (cadr args)))
(spacemacs/set-leader-keys keys fn-sym)))
;; `spacemacs/set-leader-keys-for-major-mode'
(dolist (args spacebind--bs-set-leader-keys-for-major-mode)
(let ((mode (car args))
(keys (string-join (cadr args) " "))
(fn-sym (caddr args)))
(spacemacs/set-leader-keys-for-major-mode mode keys fn-sym)))
;; `spacemacs/set-leader-keys-for-minor-mode'
(dolist (args spacebind--bs-set-leader-keys-for-minor-mode)
(let ((mode (car args))
(keys (string-join (cadr args) " "))
(fn-sym (caddr args)))
(spacemacs/set-leader-keys-for-minor-mode mode keys fn-sym)))
;; `which-key-add-key-based-replacements'
(dolist (args spacebind--bs-add-global-replacements)
(let ((keys (string-join (append `(,(spacemacs/leader-key))
(car args))
" "))
(label (cadr args)))
(which-key-add-key-based-replacements keys label))))
;; Reset stacks
(setq spacebind--bs-add-global-replacements nil
spacebind--bs-set-leader-keys-for-minor-mode nil
spacebind--bs-set-leader-keys-for-major-mode nil
spacebind--bs-set-leader-keys nil
spacebind--bs-declare-prefix-for-mode nil
spacebind--bs-declare-prefix nil
spacebind--bs-add-major-mode-replacements nil
spacebind--bs-add-minor-mode-replacements nil
;; Reset timer var
spacebind--timer [t])))
;; TODO: Make this configurable.
(define-inline spacemacs/major-mode-prefix ()
"Get current prefix for major modes."
(defun spacemacs/major-mode-prefix ()
"Get current prefix for major modes.
NOTE: `dotspacemacs-major-mode-leader-key' isn't the same."
"m")
(define-inline spacemacs/leader-key ()
(defun spacemacs/leader-key ()
"Returns `dotspacemacs-leader-key'"
dotspacemacs-leader-key)
@ -26,10 +129,6 @@
(cons (car binding-form) (cddr binding-form))
binding-form))
(defun spacebind//nosp (str)
"Remove all white-spaces from STR."
(replace-regexp-in-string " +" "" str))
(defun spacemacs/add-key-based-replacements-for-minor-mode
(mode key-sequence replacement &rest more)
"Proxy for `which-key-add-key-based-replacements'
@ -92,7 +191,7 @@ delimited by \"|\" character."
(replace-regexp-in-string "[\n[:space:]]+" " " el)
el))
fm)
(let ((full-key-or-prefix (concat path " " key-or-prefix)))
(let ((full-key-or-prefix (append path `(,key-or-prefix))))
(if (symbolp leader-label-or-fn-symbol)
(funcall k-fn
full-key-or-prefix
@ -111,7 +210,7 @@ delimited by \"|\" character."
(spacemacs//spacebind-form-visitor form path k-fn p-fn)
(let* ((is-prefix-form (stringp (cadr form)))
(cur-path (if is-prefix-form
(concat path (car form))
(append path `(,(car form)))
path))
;; Strip key and label from prefix forms.
(bindings (if is-prefix-form
@ -136,7 +235,7 @@ P-FN called for each prefix binding node with 2 arguments:
full_key_prefix_sequence and label_for_leader_menu.
Both K-FN and P-FN should return binding evaluation forms.
The forms will be concatenated and substituted by `spacebind' macro."
(spacemacs//spacebind-form-walker-rec "" k-fn p-fn b-forms))
(spacemacs//spacebind-form-walker-rec nil k-fn p-fn b-forms))
;; Key bindings - keywords handlers
@ -162,12 +261,10 @@ The forms will be concatenated and substituted by `spacebind' macro."
form)
(lambda (key-seq fn-symbol label)
`(progn
(which-key-add-key-based-replacements
(concat (spacemacs/leader-key) " " ,key-seq)
,label)
(spacemacs/set-leader-keys ,(spacebind//nosp key-seq) ',fn-symbol)))
(push (list ',key-seq ,label) spacebind--bs-add-global-replacements)
(push (list ',key-seq ',fn-symbol) spacebind--bs-set-leader-keys)))
(lambda (key-prefix label)
`(spacemacs/declare-prefix ,(spacebind//nosp key-prefix) ,label))))
`(push (list ',key-prefix ,label) spacebind--bs-declare-prefix))))
(cl-defmethod :major ((_ spacemacs--spacebind-state) form)
"Interpreter for major mode binding forms."
@ -177,23 +274,13 @@ The forms will be concatenated and substituted by `spacebind' macro."
form
(lambda (key-seq fn-symbol label)
`(progn
(which-key-add-major-mode-key-based-replacements
',mode
(string-join
(list (spacemacs/leader-key)
(spacemacs/major-mode-prefix)
,key-seq)
" ")
,label)
(spacemacs/set-leader-keys-for-major-mode
',mode
(concat (spacemacs/major-mode-prefix) ,(spacebind//nosp key-seq))
',fn-symbol)))
(push (list ',mode ',key-seq ,label)
spacebind--bs-add-major-mode-replacements)
(push (list ',mode ',key-seq ',fn-symbol)
spacebind--bs-set-leader-keys-for-major-mode)))
(lambda (key-prefix label)
`(spacemacs/declare-prefix-for-mode
',mode
(concat (spacemacs/major-mode-prefix) ,(spacebind//nosp key-prefix))
,label)))))
`(push (list ',mode ',key-prefix ,label)
spacebind--bs-declare-prefix-for-mode)))))
(cl-defmethod :minor ((_ spacemacs--spacebind-state) form)
"Interpreter for minor mode binding forms."
@ -203,21 +290,15 @@ The forms will be concatenated and substituted by `spacebind' macro."
form
(lambda (key-seq fn-symbol label)
`(progn
(spacemacs/add-key-based-replacements-for-minor-mode
',mode
(concat (spacemacs/leader-key) " " ,key-seq)
,label)
(spacemacs/set-leader-keys-for-minor-mode
',mode
,(spacebind//nosp key-seq)
',fn-symbol)))
(push (list ',mode ',key-seq ,label)
spacebind--bs-add-minor-mode-replacements)
(push (list ',mode ',key-seq ',fn-symbol)
spacebind--bs-set-leader-keys-for-minor-mode)))
(lambda (key-prefix label)
`(spacemacs/declare-prefix-for-mode
',mode
,(spacebind//nosp key-prefix)
,label)))))
`(push (list ',mode ',key-prefix ,label)
spacebind--bs-declare-prefix-for-mode)))))
(defmacro spacebind (&rest bindings)
(defmacro spacemacs|spacebind (&rest bindings)
"Bind keys and their prefixes declared via BINDINGS tree like structure.
BINDINGS format:
<DELIMITER_KEYWORD>
@ -266,10 +347,18 @@ See core-spacebind-utest.el for examples.
NOTE: This macro also has `use-package' integration via `:spacebind' key
\(fn <<DELIMITER_KEYWORD> <BINDING_FORMS>...>...)"
(spacemacs--spacebind-state-rsexp
(seq-reduce 'spacemacs//spacebind-dispatch
bindings
(make-spacemacs--spacebind-state
:rsexp `(progn)))))
(append
(spacemacs--spacebind-state-rsexp
(seq-reduce 'spacemacs//spacebind-dispatch
bindings
(make-spacemacs--spacebind-state
:rsexp `(progn))))
`((if (not spacebind--eager-bind)
(when (aref spacebind--timer 0)
(setq spacebind--timer
(run-with-idle-timer 0 nil #'spacebind//process-bind-stack)))
(when (timerp spacebind--timer)
(cancel-timer spacebind--timer))
(spacebind//process-bind-stack)))))
(provide 'core-spacebind)

View File

@ -229,4 +229,4 @@ the scroll transient state.")
body
(use-package-concat
body
`((spacebind ,@args))))))
`((spacemacs|spacebind ,@args))))))

View File

@ -1,4 +1,4 @@
;;; core-spacebind-utest.el --- Spacemacs Unit Test File
;;; core-spacebind-utest.el --- Core Unit Test File -*- lexical-binding: t -*-
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
@ -31,217 +31,283 @@
"Signature of the functions that we will mock for the `spacebind' tests.")
;;;; Helpers:
(defmacro test-spacebind/subst-eval (rep-fun &rest body)
(defmacro test-spacebind|subst-eval (rep-fun &rest body)
"Substitute calls to binding functions with calls to REP-FUN.
REP-FUN applied to the form: (fn-sym (args)).
Binding functions are listed in `test-spacebind-moked-fns-sig'. "
`(cl-labels
((spacemacs/leader-key () "SPC")
(spacemacs/major-mode-prefix () "m")
`(cl-letf
(((symbol-function 'spacemacs/leader-key) (lambda () "SPC"))
((symbol-function 'spacemacs/major-mode-prefix) (lambda () "m"))
,@(mapcar
(lambda (seg)
(let* ((f-s (car seg))
(args (cadr seg))
(pl-args (cl-set-difference args '(&rest &optional))))
`(,f-s ,args (funcall ,rep-fun ',f-s (list ,@pl-args)))))
`((symbol-function ',f-s)
(lambda ,args (,rep-fun ',f-s (list ,@pl-args))))))
test-spacebind-moked-fns-sig))
,@body))
(defmacro test-spacebind/log-calls (&rest body)
(defmacro test-spacebind|log-calls (&rest body)
"Evaluate BODY while mocking and logging calls to the binding functions.
The log is returned.
Binding functions are listed in `test-spacebind-moked-fns-sig'."
`(let ((acc nil))
(test-spacebind/subst-eval
(lambda (fn args) (push (list* fn args) acc))
,@body)
Binding functions are listed in `test-spacebind-moked-fns-sig'.
NOTE: `spacebind--eager-bind' set to true. "
`(let ((spacebind--eager-bind t)
(acc nil))
(test-spacebind|subst-eval
(lambda (fn args)
(push (cons fn args) acc))
,@body)
acc))
;; Example:
(thread-last (spacebind
(thread-last (spacemacs|spacebind
:major
(major-foo-mode
("a" "section a"
("a" foo-fn "execute foo-fn"))))
(test-spacebind/log-calls)
(test-spacebind|log-calls)
(format "%S")
(insert)
;; Prevents execution
(declare))
(defun test-spacebind/plist-diff (plist1 plist2)
"Returns difference of PLIST1 and PLIST2 using `equal'."
(cl-labels ((recur
(acc plist)
(let* ((key (car plist))
(val (cadr plist))
(rest (cddr plist))
(pair (list key val))
(new-acc (append (list pair) acc)))
(if rest
(recur new-acc rest)
new-acc))))
(let ((set-1 (recur '() plist1))
(set-2 (recur '() plist2)))
(cl-set-exclusive-or set-1 set-2 :test 'equal))))
(defmacro test-spacebind|log-stack-eval (&rest body)
"Evaluate BODY while mocking `spacebind//process-bind-stack'.
Stack values after the evaluation are returned and the stacks cleaned.
The return value is a plist with the shape:
(:minor-mode-replacements <STACK>
:major-mode-replacements <STACK>
:declare-prefix <STACK>
:declare-prefix-for-mode <STACK>
:set-leader-keys <STACK>
:set-leader-keys-for-major-mode <STACK>
:set-leader-keys-for-minor-mode <STACK>
:add-global-replacements <STACK>)
<STACK> is a corresponding binding stack.
NOTE: `spacebind--eager-bind' set to true."
`(let ((spacebind--eager-bind t)
(ret-plist nil))
(cl-labels ((spacebind//process-bind-stack
()
(setq ret-plist (list
:minor-mode-replacements
spacebind--bs-add-minor-mode-replacements
:major-mode-replacements
spacebind--bs-add-major-mode-replacements
:declare-prefix
spacebind--bs-declare-prefix
:declare-prefix-for-mode
spacebind--bs-declare-prefix-for-mode
:set-leader-keys
spacebind--bs-set-leader-keys
:set-leader-keys-for-major-mode
spacebind--bs-set-leader-keys-for-major-mode
:set-leader-keys-for-minor-mode
spacebind--bs-set-leader-keys-for-minor-mode
:add-global-replacements
spacebind--bs-add-global-replacements)
spacebind--bs-add-global-replacements nil
spacebind--bs-set-leader-keys-for-minor-mode nil
spacebind--bs-set-leader-keys-for-major-mode nil
spacebind--bs-set-leader-keys nil
spacebind--bs-declare-prefix-for-mode nil
spacebind--bs-declare-prefix nil
spacebind--bs-add-major-mode-replacements nil
spacebind--bs-add-minor-mode-replacements nil
spacebind--timer [t])))
(progn
,@body
ret-plist))))
;; Example:
(thread-last (spacemacs|spacebind
:major
(major-foo-mode
("a" "section a"
("a" foo-fn "execute foo-fn"))))
(test-spacebind|log-stack-eval)
(format "%S")
(insert)
;; Prevents execution
(declare))
(defmacro test-spacebind|validate-keys (&rest body)
"Mocks `spacebind//process-bind-stack' and validate key sequences.
NOTE: `spacebind--eager-bind' set to true. "
`(cl-letf* ((invalid-key-seqs '())
(spacebind--eager-bind t)
(spacebind--bs-add-minor-mode-replacements '())
(spacebind--bs-add-major-mode-replacements '())
(spacebind--bs-declare-prefix '())
(spacebind--bs-declare-prefix-for-mode '())
(spacebind--bs-set-leader-keys '())
(spacebind--bs-set-leader-keys-for-major-mode '())
(spacebind--bs-set-leader-keys-for-minor-mode '())
(spacebind--bs-add-global-replacements '())
(spacebind--timer [t])
(called nil)
((symbol-function 'spacebind//process-bind-stack)
(lambda () (progn))))
,@body
(dolist (el (apply 'append
(append spacebind--bs-add-minor-mode-replacements
spacebind--bs-add-major-mode-replacements
spacebind--bs-declare-prefix
spacebind--bs-declare-prefix-for-mode
spacebind--bs-set-leader-keys
spacebind--bs-set-leader-keys-for-major-mode
spacebind--bs-set-leader-keys-for-minor-mode
spacebind--bs-add-global-replacements)))
(when (listp el) ;; all list arguments are key sequences.
(condition-case err (kbd (string-join el " "))
((error nil) (push err invalid-key-seqs)))))
(cl-remove-duplicates invalid-key-seqs :test 'equal)))
;;;; Tests:
(ert-deftest test-spacebind-major-mode-always-generates-right-calls ()
(should
(eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
:major
(python-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")))))
(ert-deftest test-spacebind-major-mode-always-generates-right-stack ()
(thread-last (spacemacs|spacebind
:major
(py-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file"))))
(test-spacebind|log-stack-eval)
(test-spacebind/plist-diff
'(:major-mode-replacements
((py-mode ("c" "c") "execute file"))
:declare-prefix-for-mode
((py-mode ("c") "compile/execute"))
:set-leader-keys-for-major-mode
((py-mode ("c" "c") spacemacs/python-execute-file))
:minor-mode-replacements nil
:declare-prefix nil
:set-leader-keys nil
:set-leader-keys-for-minor-mode nil
:add-global-replacements nil))
(eq nil)
(should)))
'((spacemacs/set-leader-keys-for-major-mode
python-mode "mcc" spacemacs/python-execute-file nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m c c" "execute file" nil)
(spacemacs/declare-prefix-for-mode
python-mode "mc" "compile/execute" nil))
:test 'equal))))
(ert-deftest test-spacebind-minor-mode-always-generates-right-stack ()
(thread-last (spacemacs|spacebind
:minor
(foo-mode
"With a description"
("a" "section under a key"
("b" baz-fn "call baz-fn"))))
(test-spacebind|log-stack-eval)
(test-spacebind/plist-diff
'(:minor-mode-replacements
((foo-mode ("a" "b") "call baz-fn"))
:set-leader-keys-for-minor-mode
((foo-mode ("a" "b") baz-fn))
:declare-prefix-for-mode
((foo-mode ("a") "section under a key"))
:major-mode-replacements nil
:declare-prefix nil
:set-leader-keys nil
:set-leader-keys-for-major-mode nil
:add-global-replacements nil))
(eq nil)
(should)))
(ert-deftest test-spacebind-minor-mode-always-generates-right-calls ()
(should
(eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
:minor
(foo-mode
"With a description"
("a" "section under a key"
("b" baz-fn "call baz-fn")))))
'((spacemacs/set-leader-keys-for-minor-mode
foo-mode "ab" baz-fn nil)
(spacemacs/add-key-based-replacements-for-minor-mode
foo-mode "SPC a b" "call baz-fn" nil)
(spacemacs/declare-prefix-for-mode
foo-mode "a" "section under a key" nil))
:test 'equal))))
(ert-deftest test-spacebind-global-always-generates-right-calls ()
(should
(eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
"With a description"
:global
(("a" "section under a key"
("b" bar-fn "call bar-fn")))))
'((spacemacs/set-leader-keys "ab" bar-fn nil)
(which-key-add-key-based-replacements "SPC a b" "call bar-fn" nil)
(spacemacs/declare-prefix "a" "section under a key" nil))
:test 'equal))))
(ert-deftest test-spacebind-global-always-generates-right-stack ()
(thread-last (spacemacs|spacebind
"With a description"
:global
(("a" "section under a key"
("b" bar-fn "call bar-fn"))))
(test-spacebind|log-stack-eval)
(test-spacebind/plist-diff
'(:declare-prefix
((("a") "section under a key"))
:set-leader-keys
((("a" "b") bar-fn))
:add-global-replacements
((("a" "b") "call bar-fn"))
:minor-mode-replacements nil
:major-mode-replacements nil
:declare-prefix-for-mode nil
:set-leader-keys-for-major-mode nil
:set-leader-keys-for-minor-mode nil))
(eq nil)
(should)))
(ert-deftest test-spacebind-doc-string-always-ignored ()
(should (equal (test-spacebind/log-calls
(spacebind
(should (equal (test-spacebind|log-stack-eval
(spacemacs|spacebind
:major
(python-mode
(py-mode
"With a doc-string"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")))))
(test-spacebind/log-calls
(spacebind
(test-spacebind|log-stack-eval
(spacemacs|spacebind
:major
(python-mode
(py-mode
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")))))))
(should (equal (test-spacebind/log-calls
(spacebind
(should (equal (test-spacebind|log-stack-eval
(spacemacs|spacebind
:minor
(foo-mode
"With a doc-string"
("a" "section under a key"
("b" baz-fn "call baz-fn")))))
(test-spacebind/log-calls
(spacebind
("b" bar-fn "call bar-fn")))
(baz-mode
"With a doc-string"
("c" "section under a key"
("d" qux-fn "call qux-fn")))))
(test-spacebind|log-stack-eval
(spacemacs|spacebind
:minor
(foo-mode
"With a description"
("a" "section under a key"
("b" baz-fn "call baz-fn")))))))
(should (equal (test-spacebind/log-calls
(spacebind
"With a doc-string"
("b" bar-fn "call bar-fn")))
(baz-mode
("c" "section under a key"
("d" qux-fn "call qux-fn")))))))
(should (equal (test-spacebind|log-stack-eval
(spacemacs|spacebind
:global
(("a" "section under a key"
("With a doc-string"
("a" "section under a key"
("b" bar-fn "call bar-fn")))))
(test-spacebind/log-calls
(spacebind
(test-spacebind|log-stack-eval
(spacemacs|spacebind
:global
(("a" "section under a key"
("b" bar-fn "call bar-fn"))))))))
(ert-deftest test-spacebind-multy-section-always-work ()
(should (eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
:major
(major-foo-mode
("a" "section a"
("a" foo-fn "execute foo-fn")))
:minor
(minor-foo-mode
("b" "section b"
("b" bar-fn "execute bar-fn")))
:major
(major-bar-mode
("c" "section c"
("c" baz-fn "execute baz-fn")))
:minor
(minor-bar-mode
("d" "section d"
("d" qux-fn "execute qux-fn")))
:global
(("e" "section e"
("e" quux-fn "execute quux-fn")))
:global
(("f" "section f"
("f" quuz-fn "execute quuz-fn")))))
'((spacemacs/set-leader-keys "ff" quuz-fn nil)
(which-key-add-key-based-replacements
"SPC f f" "execute quuz-fn" nil)
(spacemacs/declare-prefix
"f" "section f" nil)
(spacemacs/set-leader-keys
"ee" quux-fn nil)
(which-key-add-key-based-replacements
"SPC e e" "execute quux-fn" nil)
(spacemacs/declare-prefix
"e" "section e" nil)
(spacemacs/set-leader-keys-for-minor-mode
minor-bar-mode "dd" qux-fn nil)
(spacemacs/add-key-based-replacements-for-minor-mode
minor-bar-mode "SPC d d" "execute qux-fn" nil)
(spacemacs/declare-prefix-for-mode
minor-bar-mode "d" "section d" nil)
(spacemacs/set-leader-keys-for-major-mode
major-bar-mode "mcc" baz-fn nil)
(which-key-add-major-mode-key-based-replacements
major-bar-mode "SPC m c c" "execute baz-fn" nil)
(spacemacs/declare-prefix-for-mode
major-bar-mode "mc" "section c" nil)
(spacemacs/set-leader-keys-for-minor-mode
minor-foo-mode "bb" bar-fn nil)
(spacemacs/add-key-based-replacements-for-minor-mode
minor-foo-mode "SPC b b" "execute bar-fn" nil)
(spacemacs/declare-prefix-for-mode
minor-foo-mode "b" "section b" nil)
(spacemacs/set-leader-keys-for-major-mode
major-foo-mode "maa" foo-fn nil)
(which-key-add-major-mode-key-based-replacements
major-foo-mode "SPC m a a" "execute foo-fn" nil)
(spacemacs/declare-prefix-for-mode
major-foo-mode "ma" "section a" nil))
:test 'equal))))
(ert-deftest test-spacebind-complex-always-generates-right-calls ()
(should
(eq
'()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
(ert-deftest test-spacebind-always-generates-right-stack ()
(thread-last
(spacemacs|spacebind
:major
(python-mode
(py-mode
"Docstring for documentation"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")
("C-p" "compile/execute"
("TAB" spacemacs/python-execute-file "execute file")
("C" spacemacs/python-execute-file-focus "execute file and focus"))
("d" "debug"
("b" spacemacs/python-toggle-breakpoint "toggle breakpoint"))
@ -261,79 +327,235 @@ Binding functions are listed in `test-spacebind-moked-fns-sig'."
("R" python-shell-send-region "send region to REPL")))
:minor
(some-minor-mode
("a" "section under a key"
("b" foo-fn "call foo-fn")
("c" "sub section under c key"
("d" "sub sub section under d key"
("e" baz-fn "call baz-fn")))))
(some-another-minor-mode
("a" "section under a key"
("b" "sub section under b key"
("c" "sub sub section under c key"
("b" baz-fn "call baz-fn")))))
:global
(("a" "section under a key"
("b" bar-fn "call bar-fn")))))
(("C-v" "section under a key"
("b" bar-fn "call bar-fn"))))
(test-spacebind|log-stack-eval)
(test-spacebind/plist-diff
'(:minor-mode-replacements
((some-another-minor-mode ("a" "b" "c" "b") "call baz-fn")
(some-minor-mode ("a" "c" "d" "e") "call baz-fn")
(some-minor-mode ("a" "b") "call foo-fn"))
:major-mode-replacements
((py-mode ("s" "R") "send region to REPL")
(py-mode ("s" "r") "send region to REPL and focus")
(py-mode ("s" "D") "send function around point to REPL")
(py-mode ("s" "d") "send function around point to REPL and focus")
(py-mode ("s" "S") "send buffer to REPL")
(py-mode ("s" "s") "send buffer to REPL and focus")
(py-mode ("r" "i") "remove unused import")
(py-mode ("d" "b") "toggle breakpoint")
(py-mode ("C-p" "C") "execute file and focus")
(py-mode ("C-p" "TAB") "execute file"))
:declare-prefix
((("C-v") "section under a key"))
:declare-prefix-for-mode
((some-another-minor-mode ("a" "b" "c") "sub sub section under c key")
(some-another-minor-mode ("a" "b") "sub section under b key")
(some-another-minor-mode ("a") "section under a key")
(some-minor-mode ("a" "c" "d") "sub sub section under d key")
(some-minor-mode ("a" "c") "sub section under c key")
(some-minor-mode ("a") "section under a key")
(py-mode ("s") "REPL")
(py-mode ("r") "refactor")
(py-mode ("d") "debug")
(py-mode ("C-p") "compile/execute"))
:set-leader-keys
((("C-v" "b") bar-fn))
:set-leader-keys-for-major-mode
((py-mode ("s" "R") python-shell-send-region)
(py-mode ("s" "r") spacemacs/python-shell-send-region-switch)
(py-mode ("s" "D") python-shell-send-defun)
(py-mode ("s" "d") spacemacs/python-shell-send-defun-switch)
(py-mode ("s" "S") python-shell-send-buffer)
(py-mode ("s" "s") spacemacs/python-shell-send-buffer-switch)
(py-mode ("r" "i") spacemacs/python-remove-unused-imports)
(py-mode ("d" "b") spacemacs/python-toggle-breakpoint)
(py-mode ("C-p" "C") spacemacs/python-execute-file-focus)
(py-mode ("C-p" "TAB") spacemacs/python-execute-file))
:set-leader-keys-for-minor-mode
((some-another-minor-mode ("a" "b" "c" "b") baz-fn)
(some-minor-mode ("a" "c" "d" "e") baz-fn)
(some-minor-mode ("a" "b") foo-fn))
:add-global-replacements
((("C-v" "b") "call bar-fn"))))
(eq nil)
(should)))
'((spacemacs/set-leader-keys
"ab" bar-fn nil)
(which-key-add-key-based-replacements
"SPC a b" "call bar-fn" nil)
(ert-deftest test-spacebind-always-generates-right-calls ()
(thread-first
(spacemacs|spacebind
:major
(py-mode
"Docstring for documentation"
("C-p" "compile/execute"
("TAB" spacemacs/python-execute-file "execute file")
("C" spacemacs/python-execute-file-focus "execute file and focus"))
("d" "debug"
("b" spacemacs/python-toggle-breakpoint "toggle breakpoint"))
("r" "refactor"
("i" spacemacs/python-remove-unused-imports "remove unused import"))
("s" "REPL"
("s" spacemacs/python-shell-send-buffer-switch
"send buffer to REPL and focus")
("S" python-shell-send-buffer
"send buffer to REPL")
("d" spacemacs/python-shell-send-defun-switch
"send function around point to REPL and focus")
("D" python-shell-send-defun
"send function around point to REPL")
("r" spacemacs/python-shell-send-region-switch
"send region to REPL and focus")
("R" python-shell-send-region "send region to REPL")))
:minor
(some-minor-mode
("a" "section under a key"
("b" foo-fn "call foo-fn")
("c" "sub section under c key"
("d" "sub sub section under d key"
("e" baz-fn "call baz-fn")))))
(some-another-minor-mode
("a" "section under a key"
("b" "sub section under b key"
("c" "sub sub section under c key"
("b" baz-fn "call baz-fn")))))
:global
(("C-v" "section under a key"
("b" bar-fn "call bar-fn"))))
(test-spacebind|log-calls)
(cl-set-exclusive-or
'((which-key-add-key-based-replacements
"SPC C-v b" "call bar-fn" nil)
(spacemacs/set-leader-keys-for-minor-mode
some-minor-mode "a b" foo-fn nil)
(spacemacs/set-leader-keys-for-minor-mode
some-minor-mode "a c d e" baz-fn nil)
(spacemacs/set-leader-keys-for-minor-mode
some-another-minor-mode "a b c b" baz-fn nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "C-p TAB" spacemacs/python-execute-file nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "C-p C" spacemacs/python-execute-file-focus nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "d b" spacemacs/python-toggle-breakpoint nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "r i" spacemacs/python-remove-unused-imports nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s s" spacemacs/python-shell-send-buffer-switch nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s S" python-shell-send-buffer nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s d" spacemacs/python-shell-send-defun-switch nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s D" python-shell-send-defun nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s r" spacemacs/python-shell-send-region-switch nil)
(spacemacs/set-leader-keys-for-major-mode
py-mode "s R" python-shell-send-region nil)
(spacemacs/set-leader-keys
"C-v b" bar-fn nil)
(spacemacs/declare-prefix
"C-p" "compile/execute" nil)
(spacemacs/declare-prefix
"d" "debug" nil)
(spacemacs/declare-prefix
"r" "refactor" nil)
(spacemacs/declare-prefix
"s" "REPL" nil)
(spacemacs/declare-prefix
"a" "section under a key" nil)
(spacemacs/set-leader-keys-for-minor-mode
some-minor-mode "abcb" baz-fn nil)
(spacemacs/declare-prefix
"a c" "sub section under c key" nil)
(spacemacs/declare-prefix
"a c d" "sub sub section under d key" nil)
(spacemacs/declare-prefix
"a" "section under a key" nil)
(spacemacs/declare-prefix
"a b" "sub section under b key" nil)
(spacemacs/declare-prefix
"a b c" "sub sub section under c key" nil)
(spacemacs/declare-prefix
"C-v" "section under a key" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m C-p TAB" "execute file" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m C-p C" "execute file and focus" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m d b" "toggle breakpoint" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m r i" "remove unused import" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s s" "send buffer to REPL and focus" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s S" "send buffer to REPL" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s d" "send function around point to REPL and focus" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s D" "send function around point to REPL" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s r" "send region to REPL and focus" nil)
(which-key-add-major-mode-key-based-replacements
py-mode "SPC m s R" "send region to REPL" nil)
(spacemacs/add-key-based-replacements-for-minor-mode
some-minor-mode "SPC abc b" "call baz-fn" nil)
(spacemacs/declare-prefix-for-mode
some-minor-mode "abc" "sub sub section under c key" nil)
(spacemacs/declare-prefix-for-mode
some-minor-mode "ab" "sub section under b key" nil)
(spacemacs/declare-prefix-for-mode
some-minor-mode "a" "section under a key" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "msR" python-shell-send-region nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m s R" "send region to REPL" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "msr" spacemacs/python-shell-send-region-switch nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m s r" "send region to REPL and focus" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "msD" python-shell-send-defun nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m s D" "send function around point to REPL" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "msd" spacemacs/python-shell-send-defun-switch nil)
(which-key-add-major-mode-key-based-replacements
python-mode
"SPC m s d"
"send function around point to REPL and focus"
nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "msS" python-shell-send-buffer nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m s S" "send buffer to REPL" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "mss" spacemacs/python-shell-send-buffer-switch nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m s s" "send buffer to REPL and focus" nil)
(spacemacs/declare-prefix-for-mode
python-mode "ms" "REPL" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "mri" spacemacs/python-remove-unused-imports nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m r i" "remove unused import" nil)
(spacemacs/declare-prefix-for-mode
python-mode "mr" "refactor" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "mdb" spacemacs/python-toggle-breakpoint nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m d b" "toggle breakpoint" nil)
(spacemacs/declare-prefix-for-mode
python-mode "md" "debug" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "mcC" spacemacs/python-execute-file-focus nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m c C" "execute file and focus" nil)
(spacemacs/set-leader-keys-for-major-mode
python-mode "mcc" spacemacs/python-execute-file nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m c c" "execute file" nil)
(spacemacs/declare-prefix-for-mode
python-mode "mc" "compile/execute" nil))
:test 'equal))))
some-minor-mode "SPC a b" "call foo-fn" nil)
(spacemacs/add-key-based-replacements-for-minor-mode
some-minor-mode "SPC a c d e" "call baz-fn" nil)
(spacemacs/add-key-based-replacements-for-minor-mode
some-another-minor-mode "SPC a b c b" "call baz-fn" nil))
:test 'equal)
(eq nil)
(should)))
(ert-deftest test-spacebind-always-generate-valid-key-seqs ()
(thread-first
(spacemacs|spacebind
:major
(py-mode
"Docstring for documentation"
("C-p" "compile/execute"
("TAB" spacemacs/python-execute-file "execute file")
("C" spacemacs/python-execute-file-focus "execute file and focus"))
("d" "debug"
("b" spacemacs/python-toggle-breakpoint "toggle breakpoint"))
("r" "refactor"
("i" spacemacs/python-remove-unused-imports "remove unused import"))
("s" "REPL"
("s" spacemacs/python-shell-send-buffer-switch
"send buffer to REPL and focus")
("S" python-shell-send-buffer
"send buffer to REPL")
("d" spacemacs/python-shell-send-defun-switch
"send function around point to REPL and focus")
("D" python-shell-send-defun
"send function around point to REPL")
("r" spacemacs/python-shell-send-region-switch
"send region to REPL and focus")
("R" python-shell-send-region "send region to REPL")))
:minor
(some-minor-mode
("a" "section under a key"
("b" foo-fn "call foo-fn")
("c" "sub section under c key"
("d" "sub sub section under d key"
("e" baz-fn "call baz-fn")))))
(some-another-minor-mode
("a" "section under a key"
("b" "sub section under b key"
("c" "sub sub section under c key"
("b" baz-fn "call baz-fn")))))
:global
(("C-v" "section under a key"
("b" bar-fn "call bar-fn"))))
(test-spacebind|validate-keys)
(eq '())
(should)))

View File

@ -1,4 +1,4 @@
;;; spacebind-utest.el --- Spacemacs Unit Test File
;;; spacebind-utest.el --- Spacemacs Unit Test File -*- lexical-binding: t -*-
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
@ -11,103 +11,46 @@
(require 'core-spacebind)
(require 'cl-lib)
(defconst test-spacebind-moked-fns-sig
'((spacemacs/add-key-based-replacements-for-minor-mode
(MODE KEY-SEQUENCE REPLACEMENT &rest MORE))
(spacemacs/declare-prefix
(PREFIX NAME &optional LONG-NAME))
(spacemacs/declare-prefix-for-mode
(MODE PREFIX NAME &optional LONG-NAME))
(spacemacs/set-leader-keys
(KEY DEF &rest BINDINGS))
(spacemacs/set-leader-keys-for-major-mode
(MODE KEY DEF &rest BINDINGS))
(spacemacs/set-leader-keys-for-minor-mode
(MODE KEY DEF &rest BINDINGS))
(which-key-add-key-based-replacements
(KEY-SEQUENCE REPLACEMENT &rest MORE))
(which-key-add-major-mode-key-based-replacements
(MODE KEY-SEQUENCE REPLACEMENT &rest MORE)))
"Signature of the functions that we will mock for the `spacebind' tests.")
;;;; Helpers:
(defmacro test-spacebind/subst-eval (rep-fun &rest body)
"Substitute calls to binding functions with calls to REP-FUN.
REP-FUN applied to the form: (fn-sym (args)).
Binding functions are listed in `test-spacebind-moked-fns-sig'. "
`(cl-labels
((spacemacs/leader-key () "SPC")
(spacemacs/major-mode-prefix () "m")
,@(mapcar
(lambda (seg)
(let* ((f-s (car seg))
(args (cadr seg))
(pl-args (cl-set-difference args '(&rest &optional))))
`(,f-s ,args (funcall ,rep-fun ',f-s (list ,@pl-args)))))
test-spacebind-moked-fns-sig))
,@body))
(defmacro test-spacebind/log-calls (&rest body)
"Evaluate BODY while mocking and logging calls to the binding functions.
The log is returned.
Binding functions are listed in `test-spacebind-moked-fns-sig'."
`(let ((acc nil))
(test-spacebind/subst-eval
(lambda (fn args) (push (cons fn args) acc))
,@body)
acc))
;; Example:
(thread-last (spacebind
:major
(major-foo-mode
("a" "section a"
("a" foo-fn "execute foo-fn"))))
(test-spacebind/log-calls)
(format "%S")
(insert)
;; Prevents execution
(declare))
(defmacro test-spacebind|process-bind-stack-called-p (&rest body)
"Mocks `spacebind//process-bind-stack' and returns true if it was called.
NOTE: `spacebind--eager-bind' set to true. "
`(cl-letf* ((spacebind--eager-bind t)
(spacebind--bs-add-minor-mode-replacements '())
(spacebind--bs-add-major-mode-replacements '())
(spacebind--bs-declare-prefix '())
(spacebind--bs-declare-prefix-for-mode '())
(spacebind--bs-set-leader-keys '())
(spacebind--bs-set-leader-keys-for-major-mode '())
(spacebind--bs-set-leader-keys-for-minor-mode '())
(spacebind--bs-add-global-replacements '())
(spacebind--timer [t])
(called nil)
((symbol-function 'spacebind//process-bind-stack)
(lambda () (setq called t))))
,@body
called))
;;;; Tests:
(ert-deftest test-spacebind-sanity-check ()
(should
(eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(spacebind
:major
(python-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")))))
'((spacemacs/set-leader-keys-for-major-mode
python-mode "mcc" spacemacs/python-execute-file nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m c c" "execute file" nil)
(spacemacs/declare-prefix-for-mode
python-mode "mc" "compile/execute" nil))
:test 'equal))))
(thread-last (spacemacs|spacebind
:major
(python-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file"))))
(test-spacebind|process-bind-stack-called-p)
(eq t)
(should)))
(ert-deftest test-spacebind-use-package-integration-works ()
(should
(eq '()
(cl-set-exclusive-or
(test-spacebind/log-calls
(use-package use-package
:spacebind
(:major
(python-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file"))))))
'((spacemacs/set-leader-keys-for-major-mode
python-mode "mcc" spacemacs/python-execute-file nil)
(which-key-add-major-mode-key-based-replacements
python-mode "SPC m c c" "execute file" nil)
(spacemacs/declare-prefix-for-mode
python-mode "mc" "compile/execute" nil))
:test 'equal))))
(thread-last (use-package use-package
:spacebind
(:major
(python-mode
"with a description"
("c" "compile/execute"
("c" spacemacs/python-execute-file "execute file")))))
(test-spacebind|process-bind-stack-called-p)
(eq t)
(should)))