Remove tree-sitter-indent's integration with cc-mode

Also minor improvement of tree-sitter-indent error messaging.

* lisp/progmodes/js.el (js--treesit-indent-rules): Fix it so it works
with the new presets (which expects regexps).
(js--treesit-cc-indent-rules): Remove variable.
(js-mode): Go back to js--treesit-indent-rules.
* lisp/treesit.el (treesit-indent): Don't report error message when
ANCHOR or OFFSET is nil: let treesit-simple-indent display the
message.
(treesit-simple-indent): Remove cc-mode integration.  Display error
message when no rules matches.
This commit is contained in:
Yuan Fu 2022-10-27 16:44:33 -07:00
parent bb0a427e9d
commit 3a44069753
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 9 additions and 127 deletions

View file

@ -3421,7 +3421,7 @@ This function is intended for use in `after-change-functions'."
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((node-is ">") parent-bol 0)
((node-is ".") parent-bol ,js-indent-level)
((node-is "\\.") parent-bol ,js-indent-level)
((parent-is "named_imports") parent-bol ,js-indent-level)
((parent-is "statement_block") parent-bol ,js-indent-level)
((parent-is "variable_declarator") parent-bol ,js-indent-level)
@ -3442,122 +3442,6 @@ This function is intended for use in `after-change-functions'."
((node-is "/") parent 0)
((parent-is "jsx_self_closing_element") parent ,js-indent-level))))
(defvar js--treesit-cc-indent-rules
(let ((function-re (rx (or "function_declaration"
"method_definition"
"function")))
(if-statement-like (rx (or "if" "try" "while" "do")
"_statement"))
(else-clause-like (rx (or "else" "catch" "finally")
"_clause"))
(switch-case-re (rx "switch_" (or "case" "default"))))
`((javascript
;; Function declaration.
;; "{"
((match "statement_block" ,function-re) parent defun-open)
((n-p-gp "}" "statement_block" ,function-re) grand-parent defun-close)
((and (node-is ,function-re) (parent-is "program"))
parent topmost-intro)
((parent-is ,function-re) parent topmost-intro-cont)
((and (n-p-gp nil "statement_block" ,function-re)
(match nil nil nil 0 0))
parent-bol defun-block-intro)
;; Class
((node-is "class_declaration") parent topmost-intro)
((parent-is "class_declaration") parent topmost-intro-cont)
((match "{" "class_declaration") parent class-open)
((match "}" "class_declaration") parent class-close)
((node-is "class_heritage") parent inher-intro)
((parent-is "class_heritage") parent inher-cont)
;; Javascript doesn't have class access keywords
;; (`member-init-intro', `member-init-cont') I think?
((match "{" "class_declaration") parent class-open)
((match "}" "class_body") parent class-close)
((parent-is "class_body") parent inclass)
((parent-is "member_definition") parent topmost-intro-cont)
((match "statement_block" "member_definition") parent defun-open)
((n-p-gp "}" "statement_block" "member_definition")
grand-parent defun-close)
;; Javascript doesn't have class parameters
;; (`template-args-cont').
;; Conditional.
((match "statement_block" ,if-statement-like)
parent substatement-open)
((match "statement_block" ,else-clause-like)
grand-parent substatement-open)
;; Javascript doesn't have `substatement-label'.
((node-is ,else-clause-like) parent else-clause)
((parent-is ,else-clause-like) grand-parent substatement)
((match "while" "do_statement") parent do-while-closure)
((field-is "consequence") parent substatement)
((field-is "condition") parent statement-cont)
;; Switch.
((node-is ,switch-case-re) grand-parent case-label)
((match "statement_block" ,switch-case-re) parent statement-case-open)
((match nil ,switch-case-re "body") parent statement-case-intro)
((parent-is ,switch-case-re) first-sibling statement-case-cont)
((node-is "switch_body") parent substatement-open)
((match "}" "switch_body") grand-parent substatement-close)
;; Brace list.
((node-is "object") parent-bol brace-list-open)
((match "}" "object") parent brace-list-close)
;; ((match nil "object" nil 0 0) parent brace-list-intro)
((match nil "object" nil 0 0) parent ,js-indent-level)
((match nil "object" nil 1) first-sibling brace-list-entry)
;; Pair.
((match nil "pair" "value") parent ,js-indent-level)
;; Javascript doesn't have extern.
;; Parenthesis. These syntax symbols uses line-up functions,
;; which don't work with tree-sitter, so we roll our own:
;; `arglist-close', `arglist-intro', `arglist-cont',
;; `arglist-cont-nonempty'.
((parent-is "formal_parameters") first-sibling 1)
((node-is "formal_parameters") parent statement-cont)
;; Misc.
((parent-is "function_declaration") parent func-decl-cont)
((parent-is "string-fragment") grand-parent string)
((parent-is "comment") grand-parent c)
;; Fallback for top-level statements.
((parent-is "program") parent 0)
;; Fallback for blocks & statements.
((parent-is "expression_statement") parent statement-cont)
((match "}" "statement_block") parent-bol block-close)
((match "statement_block" "statement_block") parent block-open)
((match nil "statement_block" nil 0 0) parent-bol statement-block-intro)
((parent-is "statement_block") prev-sibling statement)
;; Template substitution.
((match "}" "template_substitution") parent-bol block-close)
((match nil "template_substitution" nil 0 0) parent-bol statement-block-intro)
((parent-is "template_substitution") prev-sibling statement)
;; JSX, copied from `js--treesit-indent-rules' (TODO).
((parent-is "jsx_opening_element") parent ,js-indent-level)
((node-is "jsx_closing_element") parent 0)
((node-is "jsx_text") parent ,js-indent-level)
((parent-is "jsx_element") parent ,js-indent-level)
((node-is "/") parent 0)
((parent-is "jsx_self_closing_element") parent ,js-indent-level)
(catch-all parent-bol statement-cont)))))
(defvar js--treesit-keywords
'("as" "async" "await" "break" "case" "catch" "class" "const" "continue"
"debugger" "default" "delete" "do" "else" "export" "extends" "finally"
@ -3878,7 +3762,7 @@ definition*\"."
((treesit-ready-p 'js-mode 'javascript)
(treesit-parser-create 'javascript)
;; Indent.
(setq-local treesit-simple-indent-rules js--treesit-cc-indent-rules)
(setq-local treesit-simple-indent-rules js--treesit-indent-rules)
;; Navigation.
(setq-local treesit-defun-type-regexp
(rx (or "class_declaration"

View file

@ -893,9 +893,8 @@ of the current line.")
(t nil)))
(`(,anchor . ,offset)
(funcall treesit-indent-function node parent bol)))
(if (null anchor)
(when treesit--indent-verbose
(message "Failed to find the anchor"))
(if (or (null anchor) (null offset))
nil
(let ((col (+ (save-excursion
(goto-char anchor)
(current-column))
@ -905,8 +904,6 @@ of the current line.")
(indent-line-to col))
(indent-line-to col)))))))
(declare-function c-calc-offset "cc-engine")
(defun treesit-simple-indent (node parent bol)
"Calculate indentation according to `treesit-simple-indent-rules'.
@ -936,10 +933,11 @@ OFFSET."
(let ((anchor-pos
(treesit--simple-indent-eval
(list anchor node parent bol))))
(cons anchor-pos
(if (integerp offset)
offset
(c-calc-offset (list offset anchor-pos)))))))))
(cons anchor-pos offset))
finally return
(progn (when treesit--indent-verbose
(message "No matched rule"))
(cons nil nil))))))
(defun treesit-check-indent (mode)
"Check current buffer's indentation against a major mode MODE.