emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/evil-matchit ae8fddea39 207/244: clean code, tdd for markd


From: ELPA Syncer
Subject: [nongnu] elpa/evil-matchit ae8fddea39 207/244: clean code, tdd for markdown
Date: Thu, 6 Jan 2022 02:59:02 -0500 (EST)

branch: elpa/evil-matchit
commit ae8fddea392441229ed8c3038f7c0ea94dbac9dc
Author: Chen Bin <chenbin.sh@gmail.com>
Commit: Chen Bin <chenbin.sh@gmail.com>

    clean code, tdd for markdown
---
 Makefile                    |  3 ++-
 evil-matchit-markdown.el    | 61 +++++++++++++++++----------------------------
 evil-matchit-sdk.el         |  6 ++++-
 tests/evil-matchit-tests.el | 31 +++++++++++++++++++++++
 4 files changed, 61 insertions(+), 40 deletions(-)

diff --git a/Makefile b/Makefile
index c5e5e0675e..44320d1bcd 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,8 @@ 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;
 
 .PHONY: test
 test: deps clean
-       @$(EMACS) -batch -Q -L . -L deps/evil-1.14.0 -l deps/lua-mode.el -l 
evil-matchit.el -l tests/evil-matchit-tests.el
\ No newline at end of file
+       @$(EMACS) -batch -Q -L . -L deps/evil-1.14.0 -l deps/lua-mode.el -l 
deps/markdown-mode.el -l evil-matchit.el -l tests/evil-matchit-tests.el
\ No newline at end of file
diff --git a/evil-matchit-markdown.el b/evil-matchit-markdown.el
index 550e0d4bb9..f4f54c828b 100644
--- a/evil-matchit-markdown.el
+++ b/evil-matchit-markdown.el
@@ -26,54 +26,39 @@
 
 ;;; Code:
 
-;; OPTIONAL, you don't need SDK to write a plugin for evil-matchit
-;; but SDK don make you write less code, isn't it?
-;; All you need to do is just define the match-tags for SDK algorithm to 
lookup.
 (require 'evil-matchit-sdk)
 
 ;;;###autoload
 (defun evilmi-markdown-get-tag ()
-  "Get current tag.
-Return (list start-position tag)."
-  (let* ((cur-line (evilmi-sdk-curline))
-         rlt
-         forward-direction)
-    (if (string-match "^\\(```\\)" cur-line)
-        (let* ((str (match-string 1 cur-line))
-               (prefix (buffer-substring-no-properties (point-min)
-                                                       
(line-beginning-position)))
-               (forward-direction (eq (% (evilmi-sdk-count-matches str prefix) 
2) 0)))
-          (setq rlt (list (if forward-direction (line-beginning-position)
-                            (line-end-position))
-                          forward-direction
-                          str))))
-    rlt))
+  "Get current tag.  Return (list start-position tag)."
+  (when (string-match "^\\(```\\)" (evilmi-sdk-curline))
+    (let* ((str "```")
+           (text (evilmi-sdk-text-before-current-line))
+           (forward-p (eq (% (evilmi-sdk-count-matches str text) 2) 0)))
+      (list (if forward-p (line-beginning-position) (line-end-position))
+            forward-p
+            str))))
 
 ;;;###autoload
 (defun evilmi-markdown-jump (info num)
-  "Jump to the next tag."
-  (let* ((forward-direction (nth 1 info))
+  "Jump to the next tag using INFO and NUM."
+  (ignore num)
+  (let* ((forward-p (nth 1 info))
          (str (nth 2 info))
          (pos (point))
          (rlt pos))
-    (cond
-     ((string= str "```")
-      (let* ((prefix (buffer-substring-no-properties (point-min)
-                                                     
(line-beginning-position))))
-        (cond
-         (forward-direction
-          ;; jump forward
-          (goto-char (+ pos (length str)))
-          (search-forward str)
-          (setq rlt (line-end-position)))
-         (t
-          ;; jump backward
-          (goto-char (- pos (length str)))
-          (search-backward str)
-          (setq rlt (line-beginning-position))))))
-     (t
-      ;; do nothing
-      ))
+    (when (string= str "```")
+      (cond
+       (forward-p
+        ;; jump forward
+        (goto-char (+ pos (length str)))
+        (search-forward str)
+        (setq rlt (line-end-position)))
+       (t
+        ;; jump backward
+        (goto-char (- pos (length str)))
+        (search-backward str)
+        (setq rlt (line-beginning-position)))))
     rlt))
 
 (provide 'evil-matchit-markdown)
diff --git a/evil-matchit-sdk.el b/evil-matchit-sdk.el
index 973c63b672..1ca71c2bdc 100644
--- a/evil-matchit-sdk.el
+++ b/evil-matchit-sdk.el
@@ -198,6 +198,10 @@ If IS-FORWARD is t, jump forward; or else jump backward."
     (buffer-substring-no-properties (line-beginning-position)
                                     (line-end-position))))
 
+(defun evilmi-sdk-text-before-current-line ()
+  "Text before current line."
+  (buffer-substring-no-properties (point-min) (line-beginning-position)))
+
 ;;;###autoload
 (defun evilmi-sdk-member (keyword keyword-list)
   "Check if KEYWORD exist in KEYWORD-LIST."
@@ -450,7 +454,7 @@ after calling this function."
     rlt))
 
 (defun evilmi-sdk-count-matches (regexp str)
-  "Count matches of regexp in STR."
+  "Count match of REGEXP in STR."
   (let* ((count 0)
          (start 0))
     (while (string-match regexp str start)
diff --git a/tests/evil-matchit-tests.el b/tests/evil-matchit-tests.el
index 12247b6761..feebc6cede 100644
--- a/tests/evil-matchit-tests.el
+++ b/tests/evil-matchit-tests.el
@@ -377,5 +377,36 @@
 
     (should (eq major-mode 'verilog-mode))))
 
+(ert-deftest evilmi-test-markdown ()
+  (with-temp-buffer
+    (insert "### test1\n"
+            "```java\n"
+            "Example 1\n"
+            "```\n\n"
+            "### test2\n"
+            "```c\n"
+            "Example 2\n"
+            "```\n")
+    (markdown-mode)
+
+    (goto-char (point-min))
+    (forward-line 1)
+    (evilmi-jump-items)
+    (should (string= "```" (evilmi-sdk-curline)))
+    (forward-line -1)
+    (should (string= "Example 1" (evilmi-sdk-curline)))
+    (forward-line 1)
+    (evilmi-jump-items)
+    (should (string= "```java" (evilmi-sdk-curline)))
+
+    (goto-char (point-min))
+    (search-forward "```c")
+    (evilmi-jump-items)
+    (should (string= "```" (evilmi-sdk-curline)))
+    (evilmi-jump-items)
+    (should (string= "```c" (evilmi-sdk-curline)))
+
+    (should (eq major-mode 'markdown-mode))))
+
 (ert-run-tests-batch-and-exit)
 ;;; evil-matchit-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]