branch: elpa/evil-matchit commit b314e816bacfc01bb7df9b19a06b18638af5cdbe Author: Chen Bin <chenbin...@gmail.com> Commit: Chen Bin <chenbin...@gmail.com>
improved yaml indent algorithm --- Makefile | 9 ++--- evil-matchit-indent.el | 88 +++++++++++++++++++++++---------------------- evil-matchit-yaml.el | 5 ++- evil-matchit.el | 33 +++++++++++------ pkg.sh | 2 +- tests/evil-matchit-tests.el | 21 +++++++++++ tests/hello.yml | 6 ++++ 7 files changed, 103 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index 55fa2760cc..83a03c168f 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,12 @@ clean: deps: @mkdir -p deps; @if [ ! -f deps/evil-1.14.0/evil.el ]; then curl -L https://stable.melpa.org/packages/evil-1.14.0.tar | tar x -C deps/; fi; - @if [ ! -f deps/lua-mode.el ]; then curl -L https://raw.githubusercontent.com/immerrr/lua-mode/master/lua-mode.el > deps/lua-mode.el; fi; - @if [ ! -f deps/markdown-mode.el ]; then curl -L https://raw.githubusercontent.com/jrblevin/markdown-mode/master/markdown-mode.el > deps/markdown-mode.el; fi; - @if [ ! -f deps/tuareg-2.2.0/tuareg.el ]; then curl -L https://stable.melpa.org/packages/tuareg-2.2.0.tar | tar x -C deps/; fi + @if [ ! -f deps/lua-mode.el ]; then curl -L https://stable.melpa.org/packages/lua-mode-20210802.el > deps/lua-mode.el; fi; + @if [ ! -f deps/markdown-mode.el ]; then curl -L https://stable.melpa.org/packages/markdown-mode-2.5.el > deps/markdown-mode.el; fi; + @if [ ! -f deps/tuareg-2.2.0/tuareg.el ]; then curl -L https://stable.melpa.org/packages/tuareg-2.2.0.tar | tar x -C deps/; fi; + @if [ ! -f deps/yaml-mode.el ]; then curl -L https://stable.melpa.org/packages/yaml-mode-0.0.15.el > deps/yaml-mode.el; fi; .PHONY: test test: deps clean - @$(EMACS) -batch -Q -L . -L deps/evil-1.14.0 -l deps/lua-mode.el -l deps/markdown-mode.el -L deps/tuareg-2.2.0 -l evil-matchit.el -l tests/evil-matchit-tests.el + @$(EMACS) -batch -Q -L . -L deps/evil-1.14.0 -l deps/yaml-mode.el -l deps/lua-mode.el -l deps/markdown-mode.el -L deps/tuareg-2.2.0 -l evil-matchit.el -l tests/evil-matchit-tests.el diff --git a/evil-matchit-indent.el b/evil-matchit-indent.el index af7d8df75f..e08eb4f612 100644 --- a/evil-matchit-indent.el +++ b/evil-matchit-indent.el @@ -113,44 +113,55 @@ SPACES-PER-TAB defines the number of spaces of one tab character." (setq rlt (list (line-end-position) 1 ""))) (t + (message "next-line=%s" next-line) + (message "(evilmi-indent-tab-count next-line)=%s" (evilmi-indent-tab-count next-line)) + (message "(evilmi-indent-tab-count cur-line)=%s" (evilmi-indent-tab-count cur-line)) (setq rlt nil))) rlt)) +;;;###autoload +(defun evilmi-indent-extract-keyword (line) + "Extract keyword from LINE." + (let* (keyword) + (when (string-match evilmi-indent-open-tag-regexp line) + (setq keyword (match-string 1 line))) + keyword)) + (defun evilmi-indent-back-to-first-tag (cur-indent) - "Jump to the open tag based on CUR-INDENT. + "Jump to the open tag based on CUR-INDENT. For example, jump from the tag \"finally\" to \"try\"." - (let (out-of-loop - where-to-go - (cur-line (evilmi-sdk-curline)) - (keyword (evilmi-indent-extract-keyword cur-line)) - regexp) - - (setq regexp - (and evilmi-indent-first-tag-function - (funcall evilmi-indent-first-tag-function keyword))) - - (when evilmi-debug - (message "evilmi-indent-back-to-first-tag called. keyword=%s regexp=%s cur-line=%s" - keyword regexp cur-line)) - - (when regexp - (save-excursion - (while (not out-of-loop) - (forward-line -1) - (setq cur-line (evilmi-sdk-curline)) - - (when (and (= cur-indent (evilmi-indent-tab-count cur-line)) - (string-match regexp cur-line)) - (setq where-to-go (line-beginning-position)) - (setq out-of-loop t)) - - ;; if it's first line, we need get out of loop - (if (= (point-min) (line-beginning-position)) - (setq out-of-loop t)))) - (when where-to-go - (goto-char where-to-go) - (skip-chars-forward " \t"))))) + (let* (out-of-loop + where-to-go + (cur-line (evilmi-sdk-curline)) + (keyword (evilmi-indent-extract-keyword cur-line)) + regexp) + + (setq regexp + (and evilmi-indent-first-tag-function + (funcall evilmi-indent-first-tag-function keyword))) + + (when evilmi-debug + (message "evilmi-indent-back-to-first-tag called. keyword=%s regexp=%s cur-line=%s" + keyword regexp cur-line)) + + (when regexp + (save-excursion + (while (not out-of-loop) + (forward-line -1) + (setq cur-line (evilmi-sdk-curline)) + + (when (and (= cur-indent (evilmi-indent-tab-count cur-line)) + (string-match regexp cur-line)) + (setq where-to-go (line-beginning-position)) + (setq out-of-loop t)) + + ;; if it's first line, we need get out of loop + (if (= (point-min) (line-beginning-position)) + (setq out-of-loop t)))) + (when where-to-go + (goto-char where-to-go) + (skip-chars-forward " \t"))))) (defun evilmi-indent-goto-next-tag (keyword cur-indent) "Move to next open tag using KEYWORD and CUR-INDENT." @@ -185,7 +196,6 @@ For example, jump from the tag \"finally\" to \"try\"." ;;;###autoload (defun evilmi-indent-jump (info) "Use INFO from `evilmi-indent-get-tag' to jump to matched tag." - (ignore num) (let* ((p (nth 0 info)) (tag-type (nth 1 info)) (keyword (nth 2 info)) @@ -238,7 +248,7 @@ For example, jump from the tag \"finally\" to \"try\"." ;; record the previous line which indents more than original line (setq rlt (line-end-position))))) - ;; stop the loop at the end of hte buffer + ;; stop the loop at the end of the buffer (when (= (point-max) (line-end-position)) (setq dendent t))) @@ -248,13 +258,5 @@ For example, jump from the tag \"finally\" to \"try\"." rlt)) -;;;###autoload -(defun evilmi-indent-extract-keyword (line) - "Extract keyword from LINE." - (let (keyword) - (when (string-match evilmi-indent-open-tag-regexp line) - (setq keyword (match-string 1 cur-line))) - keyword)) - (provide 'evil-matchit-indent) -;;; evil-matchit-indent ends here +;;; evil-matchit-indent.el ends here diff --git a/evil-matchit-yaml.el b/evil-matchit-yaml.el index 681c84d89c..5e14c9e829 100644 --- a/evil-matchit-yaml.el +++ b/evil-matchit-yaml.el @@ -35,9 +35,8 @@ ;;;###autoload (defun evilmi-yaml-get-tag () "Return '(start-position tag-type keyword)." - (if evilmi-debug (message "evilmi-yaml-get-tag called")) - - (let ((rlt (evilmi-indent-get-tag))) + (let* ((evilmi-spaces-per-tab 2) + (rlt (evilmi-indent-get-tag))) (when (and evilmi-debug rlt) (message "evilmi-yaml-get-tag called. rlt=%s" rlt)) rlt)) diff --git a/evil-matchit.el b/evil-matchit.el index ff210ab427..3c877c36bb 100644 --- a/evil-matchit.el +++ b/evil-matchit.el @@ -4,7 +4,7 @@ ;; Author: Chen Bin <chenbin...@gmail.com> ;; URL: http://github.com/redguardtoo/evil-matchit -;; Version: 2.4.3 +;; Version: 2.4.4 ;; Keywords: matchit vim evil ;; Package-Requires: ((evil "1.14.0") (emacs "25.1")) ;; @@ -48,21 +48,33 @@ ;;; Code: +(defgroup evil-matchit nil + "Matchit.vim for Emacs." + :group 'evil + :prefix "evil-matchit") + (eval-when-compile (require 'evil-macros)) (require 'evil-matchit-sdk) -(defvar evilmi-plugins '(emacs-lisp-mode ((evilmi-simple-get-tag evilmi-simple-jump))) - "The Matrix to of algorithms.") +(defcustom evilmi-plugins + '(emacs-lisp-mode ((evilmi-simple-get-tag evilmi-simple-jump))) + "The Matrix of algorithms." + :group 'evil-matchit + :type '(repeat sexp)) -(defvar evilmi-may-jump-by-percentage t +(defcustom evilmi-may-jump-by-percentage t "Simulate `evil-jump-item'. For example, `50%' jumps to 50 percentage of buffer. -If nil, `50%' jumps 50 times.") +If nil, `50%' jumps 50 times." + :group 'evil-matchit + :type 'boolean) -(defvar evilmi-shortcut "%" +(defcustom evilmi-shortcut "%" "The keybinding of `evilmi-jump-items' and then text object shortcut. -Some people prefer using \"m\" instead.") +Some people prefer using \"m\" instead." + :group 'evil-matchit + :type 'string) ;; {{ make linter happy (defvar evil-visual-char) @@ -273,13 +285,13 @@ If IS-INNER is t, the region is inner text object." (list b e))) (evil-define-text-object evilmi-inner-text-object (&optional num begin end type) - "Inner text object describing the region selected when you press % from evil-matchit" + "Inner text object describing the region selected when pressing %." :type line (let* ((selected-region (evilmi--region-to-select-or-delete num t))) (evil-range (car selected-region) (cadr selected-region) 'line))) (evil-define-text-object evilmi-outer-text-object (&optional num begin end type) - "Outer text object describing the region selected when you press % from evil-matchit" + "Outer text object describing the region selected when pressing %." :type line (let ((selected-region (evilmi--region-to-select-or-delete num))) (evil-range (car selected-region) (cadr selected-region) 'line))) @@ -339,7 +351,7 @@ If IS-INNER is t, the region is inner text object." (defun evilmi-version() "Print version." (interactive) - (message "2.4.3")) + (message "2.4.4")) (defvar evil-matchit-mode-map (make-sparse-keymap) "Keymap used by the minor mode.") @@ -348,6 +360,7 @@ If IS-INNER is t, the region is inner text object." (define-minor-mode evil-matchit-mode "Buffer-local minor mode to emulate matchit.vim." :keymap (make-sparse-keymap) + :group 'evil-matchit ;; get correct value of `(point)` in visual-line mode ;; @see https://bitbucket.org/lyro/evil/issues/540/get-the-char-under-cusor-in-visual-line (evil-set-command-property 'evilmi-jump-items :keep-visual t) diff --git a/pkg.sh b/pkg.sh index 1f5f53fb6a..a02e9972fb 100755 --- a/pkg.sh +++ b/pkg.sh @@ -1,6 +1,6 @@ #!/bin/bash name=evil-matchit -version=2.4.3 +version=2.4.4 pkg=$name-$version mkdir $pkg cp README.org $pkg diff --git a/tests/evil-matchit-tests.el b/tests/evil-matchit-tests.el index 891f1112bd..d28408df9c 100644 --- a/tests/evil-matchit-tests.el +++ b/tests/evil-matchit-tests.el @@ -609,5 +609,26 @@ (should (eq major-mode 'python-mode)))) +(ert-deftest evilmi-test-yaml () + (with-temp-buffer + (evilmi-test-read-file "hello.yml") + (yaml-mode) + + (goto-char (point-min)) + (should (string= "mysql-database" (thing-at-point 'symbol))) + (evilmi-jump-items) + (should (string= "hello" (thing-at-point 'symbol))) + (evilmi-jump-items) + (should (string= "mysql-database" (thing-at-point 'symbol))) + + (search-forward "hostname:") + (should (string= "hostname:" (string-trim (evilmi-sdk-curline)))) + (evilmi-jump-items) + (should (string= "localhost" (thing-at-point 'symbol))) + (evilmi-jump-items) + (should (string= "hostname:" (string-trim (evilmi-sdk-curline)))) + + (should (eq major-mode 'yaml-mode)))) + (ert-run-tests-batch-and-exit) ;;; evil-matchit-tests.el ends here diff --git a/tests/hello.yml b/tests/hello.yml new file mode 100644 index 0000000000..e990173ad4 --- /dev/null +++ b/tests/hello.yml @@ -0,0 +1,6 @@ +mysql-database: + hostname: + test: localhost + port: 3012 + username: root + password: hello