branch: externals/matlab-mode
commit e71bb01c921efb4d14d2a319eb17d5332631ce4b
Author: John Ciolfi <[email protected]>
Commit: John Ciolfi <[email protected]>

    t-utils: added t-utils-view-parse-tree and tuned up parse tree testing
---
 tests/t-utils.el                                   | 65 +++++++++++++++-------
 ...binary_operator_continued_issue107_expected.txt |  4 +-
 .../parser_cell_expected.txt                       |  4 +-
 .../parser_comments_expected.txt                   |  4 +-
 .../parser_simple_expected.txt                     |  4 +-
 .../parser_simple_with_error_expected.txt          |  4 +-
 .../parser_variable_ids_expected.txt               |  4 +-
 7 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/tests/t-utils.el b/tests/t-utils.el
index edab15ecfa..6077915193 100644
--- a/tests/t-utils.el
+++ b/tests/t-utils.el
@@ -137,6 +137,11 @@
 ;;      example, `t-utils-sweep-test-indent' will run indent-region on all 
matched files under a
 ;;      directory tree looking for indent issues.
 ;;
+;;   6. Debugging the parse tree produced by tree-sitter
+;;
+;;      - Visit the file
+;;      - M-x t-utils-view-parse-tree
+;;
 ;; ----------------------------
 ;; | Example: font-lock tests |
 ;; ----------------------------
@@ -2665,8 +2670,8 @@ otherwise the result is displayed on stdout."
 
     (t-utils--log log-file (format "FINISHED: %s %s\n" test-name 
(t-utils--took start-time)))))
 
-(defun t-utils--syntax-tree-draw-node (node)
-  "Draw the syntax tree of NODE in the current buffer.
+(defun t-utils--parse-tree-draw-node (node)
+  "Draw the parse tree of NODE in the current buffer.
 
 When this function is called, point should be at the position where the
 node should start.  When this function returns, it leaves point at the
@@ -2694,7 +2699,7 @@ Similar `treesit--explorer-draw-node' but designed for 
test baselines."
   ;;
   ;; Debian 12                                        Windows
   ;; ----                                             -------
-  ;; t-utils--syntax-tree-draw-node                   
t-utils--syntax-tree-draw-node
+  ;; t-utils--parse-tree-draw-node                    
t-utils--parse-tree-draw-node
   ;; node: #<treesit-node source_file in 1-10>        node: #<treesit-node 
source_file in 1-10>
   ;; field-name: nil                                  field-name: nil
   ;; named: t                                         named: t
@@ -2815,46 +2820,46 @@ Similar `treesit--explorer-draw-node' but designed for 
test baselines."
             ;; Draw children on the same line.
             (progn
               (insert " ")
-              (t-utils--syntax-tree-draw-node child))
+              (t-utils--parse-tree-draw-node child))
           ;; Draw children on the new line.
           (insert "\n")
           (indent-to-column children-indent)
-          (t-utils--syntax-tree-draw-node child))
+          (t-utils--parse-tree-draw-node child))
         (setq can-inline draw-inline)))
 
     ;; Done drawing children, draw the ending paren.
     (when named (insert ")"))))
 
-(defun t-utils--get-syntax-tree ()
+(defun t-utils--get-parse-tree ()
   "Return the syntax tree for the current buffer."
   (let ((root (or (treesit-buffer-root-node)
                   (error "No tree-sitter root node"))))
     (with-temp-buffer
-      (insert "# -*- t-utils-ts-syntax-tree -*-\n")
+      (insert "# -*- t-utils-ts-parse-tree -*-\n")
       (insert "# tree-sitter parse tree annotated with 
[NODE_START,NODE_END]@{NODE_TEXT}@\n")
-      (insert "# where node is of length NODE-END - NODE_START\n")
-      (t-utils--syntax-tree-draw-node root)
+      (insert "# where NODE_TEXT is of length NODE-END - NODE_START\n")
+      (t-utils--parse-tree-draw-node root)
       (goto-char (point-max))
       (insert "\n")
       (buffer-string))))
 
-(defvar t-utils--ts-syntax-tree-mode-syntax-table
+(defvar t-utils--ts-parse-tree-mode-syntax-table
   (let ((table (make-syntax-table)))
     (modify-syntax-entry ?\# "<" table)
     (modify-syntax-entry ?\n ">" table)
     (modify-syntax-entry ?'  "_" table)
     (modify-syntax-entry ?\" "_" table)
     table)
-  "Syntax table for `t-utils-ts-syntax-tree-mode'.")
+  "Syntax table for `t-utils-ts-parse-tree-mode'.")
 
-(defface t-utils-ts-syntax-tree-code-face
+(defface t-utils-ts-parse-tree-code-face
   '((t
      :inherit default
      :box t
      :bold t))
   "The face used for code.")
 
-(defvar t-utils--ts-syntax-tree-font-lock-keywords
+(defvar t-utils--ts-parse-tree-font-lock-keywords
   (list
    ;; (NODE_TYPE[NODE_START,NODE_END)@{NODE_TEXT}@
    (list (concat "\\((\\)"                      ;; 1 (
@@ -2871,7 +2876,8 @@ Similar `treesit--explorer-draw-node' but designed for 
test baselines."
          '(2 'font-lock-function-name-face)
          '(3 'font-lock-constant-face)
          '(4 'shadow)
-         '(5 't-utils-ts-syntax-tree-code-face prepend)
+         ;; prepend to cover when code text has a # which are header comments, 
but not when in code.
+         '(5 't-utils-ts-parse-tree-code-face prepend)
          '(6 'shadow prepend))
    ;; FIELD:
    (list "\\([a-zA-Z]+:\\)"
@@ -2883,7 +2889,7 @@ Similar `treesit--explorer-draw-node' but designed for 
test baselines."
                  ","                 ;;   ,
                  "[0-9]+"            ;;   END
                  "\\]\\)")           ;;   ]
-         '(1 't-utils-ts-syntax-tree-code-face)
+         '(1 't-utils-ts-parse-tree-code-face)
          '(2 ''font-lock-constant-face))
    ;; (NODE
    (list (concat "\\((\\)"              ;; 1 (
@@ -2894,14 +2900,30 @@ Similar `treesit--explorer-draw-node' but designed for 
test baselines."
    (list "\\()\\)"
          '(1 'shadow))
    )
-  "Keywords to fontify in `sbmgr-mode'.")
+  "Keywords to fontify in `t-utils-ts-parse-tree-mode'.")
 
-(define-derived-mode t-utils-ts-syntax-tree-mode fundamental-mode 
"ts-syntax-tree" ()
-  "Major mode for treesit syntax trees created by `t-utils--get-syntax-tree'."
-  (set-syntax-table t-utils--ts-syntax-tree-mode-syntax-table)
-  (setq-local font-lock-defaults 
'((t-utils--ts-syntax-tree-font-lock-keywords) nil nil nil))
+(define-derived-mode t-utils-ts-parse-tree-mode fundamental-mode 
"ts-parse-tree" ()
+  "Major mode for treesit parse trees created by `t-utils--get-parse-tree'."
+  (set-syntax-table t-utils--ts-parse-tree-mode-syntax-table)
+  (setq-local font-lock-defaults '((t-utils--ts-parse-tree-font-lock-keywords) 
nil nil nil))
   (read-only-mode 1))
 
+(defun t-utils-view-parse-tree ()
+  "View the tree-sitter parse/syntax tree for the current buffer."
+  (interactive)
+  (let* ((parse-tree (t-utils--get-parse-tree))
+         (view-buf-name (concat "*" (buffer-name) "-parse-tree*"))
+         (view-buf (get-buffer-create view-buf-name)))
+    (with-current-buffer view-buf
+      (read-only-mode -1)
+      (buffer-disable-undo)
+      (erase-buffer)
+      (insert parse-tree)
+      (goto-char (point-min))
+      (t-utils-ts-parse-tree-mode))
+    (pop-to-buffer view-buf)
+    view-buf))
+
 (defun t-utils--test-parser-error-node-checker (lang-file _got _got-file 
_expected _expected-file)
   "Check ERROR node status for `t-utils-test-parser'.
 
@@ -3009,7 +3031,7 @@ To debug a specific -parser test file
 
             (t-utils--insert-file-for-test lang-file)
 
-            (setq got (t-utils--get-syntax-tree))
+            (setq got (t-utils--get-parse-tree))
 
             (let ((error-msg (t-utils--baseline-check
                               test-name start-time
@@ -3030,3 +3052,4 @@ To debug a specific -parser test file
 ;; LocalWords:  consp listp cdr CRLF impl tmp xr boundp SPC kbd prin progn 
defmacro sexp stdlib locs
 ;; LocalWords:  showall repeat:nil kkk fff Dkkkk kkkkkk mapcar eobp trim'd bol 
NPS prev puthash
 ;; LocalWords:  maphash lessp gethash nbutlast mapconcat ppss imenu pcase eow 
NAME's darwin libtree
+;; LocalWords:  defface fontify
diff --git 
a/tests/test-matlab-ts-mode-parser-files/parser_binary_operator_continued_issue107_expected.txt
 
b/tests/test-matlab-ts-mode-parser-files/parser_binary_operator_continued_issue107_expected.txt
index 2990228786..b430c01db6 100644
--- 
a/tests/test-matlab-ts-mode-parser-files/parser_binary_operator_continued_issue107_expected.txt
+++ 
b/tests/test-matlab-ts-mode-parser-files/parser_binary_operator_continued_issue107_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@) (comment[22,89]@{% See 
https://github.com/acristoffers/tree-sitter-...}@)
  (assignment left: (identifier[91,93]@{v1}@) =[94,95]
   right: 
diff --git a/tests/test-matlab-ts-mode-parser-files/parser_cell_expected.txt 
b/tests/test-matlab-ts-mode-parser-files/parser_cell_expected.txt
index 6bd68e79e1..a93c88c866 100644
--- a/tests/test-matlab-ts-mode-parser-files/parser_cell_expected.txt
+++ b/tests/test-matlab-ts-mode-parser-files/parser_cell_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@)
  (assignment left: (identifier[22,23]@{a}@) =[24,25]
   right: 
diff --git 
a/tests/test-matlab-ts-mode-parser-files/parser_comments_expected.txt 
b/tests/test-matlab-ts-mode-parser-files/parser_comments_expected.txt
index 4ca94f4f40..fc4474d988 100644
--- a/tests/test-matlab-ts-mode-parser-files/parser_comments_expected.txt
+++ b/tests/test-matlab-ts-mode-parser-files/parser_comments_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@)
  (function_definition function[22,30]
   (function_output (identifier[31,34]@{out}@) =[35,36])
diff --git a/tests/test-matlab-ts-mode-parser-files/parser_simple_expected.txt 
b/tests/test-matlab-ts-mode-parser-files/parser_simple_expected.txt
index 650b9f568f..61c6ee39ef 100644
--- a/tests/test-matlab-ts-mode-parser-files/parser_simple_expected.txt
+++ b/tests/test-matlab-ts-mode-parser-files/parser_simple_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@)
  (assignment left: (identifier[22,23]@{a}@) =[24,25]
   right: (binary_operator left: (number[26,27]@{1}@) +[28,29] right: 
(number[30,31]@{2}@)))
diff --git 
a/tests/test-matlab-ts-mode-parser-files/parser_simple_with_error_expected.txt 
b/tests/test-matlab-ts-mode-parser-files/parser_simple_with_error_expected.txt
index fafae6d54a..d43843869e 100644
--- 
a/tests/test-matlab-ts-mode-parser-files/parser_simple_with_error_expected.txt
+++ 
b/tests/test-matlab-ts-mode-parser-files/parser_simple_with_error_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@)
  (ERROR (identifier[22,23]@{a}@) =[24,25]
   (binary_operator left: (number[26,27]@{1}@) +[28,29] right: 
(number[30,31]@{2}@))
diff --git 
a/tests/test-matlab-ts-mode-parser-files/parser_variable_ids_expected.txt 
b/tests/test-matlab-ts-mode-parser-files/parser_variable_ids_expected.txt
index 0c966b03c0..53572d8799 100644
--- a/tests/test-matlab-ts-mode-parser-files/parser_variable_ids_expected.txt
+++ b/tests/test-matlab-ts-mode-parser-files/parser_variable_ids_expected.txt
@@ -1,6 +1,6 @@
-# -*- t-utils-ts-syntax-tree -*-
+# -*- t-utils-ts-parse-tree -*-
 # tree-sitter parse tree annotated with [NODE_START,NODE_END]@{NODE_TEXT}@
-# where node is of length NODE-END - NODE_START
+# where NODE_TEXT is of length NODE-END - NODE_START
 (source_file (comment[1,20]@{% -*- matlab-ts -*-}@)
  (function_definition function[22,30]
   (function_output (identifier[31,35]@{out1}@) =[36,37])

Reply via email to