Revise the toggle scheme of tree-sitter (again)

Now instead of a toggle function (major-mode-backend-function), we let
major mode set local variables like treesit-font-lock-settings,
treesit-imenu-function, then treesit-mode takes care of activating
those things (clearing font-lock-keywords, setting
imenu-create-index-function to treesit-imenu-function, etc).

js.el and python.el: I've returned js-mode and python-mode to exactly
what they were before tree-sitter change, plus lines at the end
setting up tree-sitter variables.  Sorry about all these fuss :-D

* lisp/treesit.el (treesit-mode-inhibit-message): Remove option.
(major-mode-backend-function)
(treesit-remapped-major-mode-alist): Remove variables.
(treesit-mode): Move down to the end of buffer.  Do more things.
(global-treesit-mode): Move down to the end of buffer.  Don't handle
major-mode-remap-alist anymore.
(global-treesit-mode--turn-on): Move down to the end of buffer.
(treesit-ready-p): Move down to the end of buffer.  Changed signature.
(treesit-font-lock-enable): Remove function.
(treesit-defun-type-regexp): New variable.
(treesit-beginning-of-defun)
(treesit-end-of-defun): New function.
(treesit-imenu-function): New variable.
(treesit-mode-supported)
(treesit-required-languages)
(treesit--local-variable-backup): New variables.
(treesit--backup-local-variable): New function

* lisp/progmodes/js.el (js-use-tree-sitter): Remove option.
(js--treesit-defun-type-regexp): Remove variable.  (Now set inline in
js-mode.)
(js--treesit-beginning-of-defun)
(js--treesit-end-of-defun): Remove functions.  (Now use
treesit-beginning/end-of-defun.)
(js--backend-toggle)
(js--json-backend-toggle): Remove function.
(js-mode)
(js-json-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/python.el (python--backend-toggle): Remove function.
(python-mode): Restore back to before tree-sitter changes.  Add
tree-sitter setup at the end.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings): Use
js--fontify-template-string.
(ts-mode--fontify-template-string): Remove function (because we can
just use js--fontify-template-string).
(ts-mode--defun-type-regexp): Remove variable (now set inline in
ts-mode).
(ts-mode--beginning-of-defun)
(ts-mode--end-of-defun): Remove functions (now using
treesit-beginning/end-of-defun).
(ts-mode): Setup tree-sitter variables and then turn on treesit-mode
or move to js-mode.
This commit is contained in:
Yuan Fu 2022-10-19 16:44:04 -07:00
parent 4c328daf01
commit 5159789e55
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
4 changed files with 253 additions and 327 deletions

View file

@ -6375,35 +6375,6 @@ Add import for undefined name `%s' (empty to skip): "
(defvar electric-indent-inhibit)
(defvar prettify-symbols-alist)
(defun python--backend-toggle (backend warn)
"Toggle backend for `python-mode'.
BACKEND and WARN are explained in `treesit-mode-function'."
(cond
;; Tree-sitter.
((and (eq backend 'treesit) (treesit-ready-p warn 'python))
(setq-local font-lock-keywords-only t)
(setq-local treesit-font-lock-feature-list
'((basic) (moderate) (elaborate)))
(setq-local treesit-font-lock-settings
python--treesit-settings)
(treesit-font-lock-enable)
(setq-local imenu-create-index-function
#'python-imenu-treesit-create-index)
(add-hook 'which-func-functions
#'python-info-treesit-current-defun nil t))
;; Elisp.
((eq backend 'elisp)
(setq-local font-lock-defaults
`(,python-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function
. python-font-lock-syntactic-face-function)
(font-lock-extend-after-change-region-function
. python-font-lock-extend-region)))
(setq-local imenu-create-index-function
#'python-imenu-create-index)
(add-hook 'which-func-functions #'python-info-current-defun nil t))))
;;;###autoload
(define-derived-mode python-mode prog-mode "Python"
"Major mode for editing Python files.
@ -6420,9 +6391,11 @@ BACKEND and WARN are explained in `treesit-mode-function'."
(setq-local forward-sexp-function python-forward-sexp-function)
(python--backend-toggle 'elisp nil)
(setq-local major-mode-backend-function #'python--backend-toggle)
(setq-local font-lock-defaults
`(,python-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function
. python-font-lock-syntactic-face-function)))
(setq-local syntax-propertize-function
python-syntax-propertize-function)
@ -6451,9 +6424,14 @@ BACKEND and WARN are explained in `treesit-mode-function'."
(add-hook 'post-self-insert-hook
#'python-indent-post-self-insert-function 'append 'local)
(setq-local imenu-create-index-function
#'python-imenu-create-index)
(setq-local add-log-current-defun-function
#'python-info-current-defun)
(add-hook 'which-func-functions #'python-info-current-defun nil t)
(setq-local skeleton-further-elements
'((abbrev-mode nil)
(< '(backward-delete-char-untabify (min python-indent-offset
@ -6462,13 +6440,13 @@ BACKEND and WARN are explained in `treesit-mode-function'."
(with-no-warnings
;; suppress warnings about eldoc-documentation-function being obsolete
(if (null eldoc-documentation-function)
;; Emacs<25
(setq-local eldoc-documentation-function #'python-eldoc-function)
(if (boundp 'eldoc-documentation-functions)
(add-hook 'eldoc-documentation-functions #'python-eldoc-function nil t)
(add-function :before-until (local 'eldoc-documentation-function)
#'python-eldoc-function))))
(if (null eldoc-documentation-function)
;; Emacs<25
(setq-local eldoc-documentation-function #'python-eldoc-function)
(if (boundp 'eldoc-documentation-functions)
(add-hook 'eldoc-documentation-functions #'python-eldoc-function nil t)
(add-function :before-until (local 'eldoc-documentation-function)
#'python-eldoc-function))))
(add-to-list
'hs-special-modes-alist
@ -6499,7 +6477,15 @@ BACKEND and WARN are explained in `treesit-mode-function'."
(when python-indent-guess-indent-offset
(python-indent-guess-indent-offset))
(add-hook 'flymake-diagnostic-functions #'python-flymake nil t))
(add-hook 'flymake-diagnostic-functions #'python-flymake nil t)
(setq-local treesit-mode-supported t)
(setq-local treesit-required-languages '(python))
(setq-local treesit-font-lock-feature-list
'((basic) (moderate) (elaborate)))
(setq-local treesit-font-lock-settings python--treesit-settings)
(setq-local treesit-imenu-function
#'python-imenu-treesit-create-index))
;;; Completion predicates for M-x
;; Commands that only make sense when editing Python code