aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-10-19 16:44:04 -0700
committerYuan Fu2022-10-19 16:44:04 -0700
commit5159789e55d64c7482dff3dc1a621d01f530f83c (patch)
tree37226a815710fd704c02b8ad08a498cfe4887c34 /lisp/progmodes/python.el
parent4c328daf0130fc139e827a8a8d6689e2b15c73ea (diff)
downloademacs-5159789e55d64c7482dff3dc1a621d01f530f83c.tar.gz
emacs-5159789e55d64c7482dff3dc1a621d01f530f83c.zip
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.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el66
1 files changed, 26 insertions, 40 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 834e8036a1b..4ac5f2e3b0a 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -6375,35 +6375,6 @@ Add import for undefined name `%s' (empty to skip): "
6375(defvar electric-indent-inhibit) 6375(defvar electric-indent-inhibit)
6376(defvar prettify-symbols-alist) 6376(defvar prettify-symbols-alist)
6377 6377
6378(defun python--backend-toggle (backend warn)
6379 "Toggle backend for `python-mode'.
6380BACKEND and WARN are explained in `treesit-mode-function'."
6381 (cond
6382 ;; Tree-sitter.
6383 ((and (eq backend 'treesit) (treesit-ready-p warn 'python))
6384 (setq-local font-lock-keywords-only t)
6385 (setq-local treesit-font-lock-feature-list
6386 '((basic) (moderate) (elaborate)))
6387 (setq-local treesit-font-lock-settings
6388 python--treesit-settings)
6389 (treesit-font-lock-enable)
6390 (setq-local imenu-create-index-function
6391 #'python-imenu-treesit-create-index)
6392 (add-hook 'which-func-functions
6393 #'python-info-treesit-current-defun nil t))
6394 ;; Elisp.
6395 ((eq backend 'elisp)
6396 (setq-local font-lock-defaults
6397 `(,python-font-lock-keywords
6398 nil nil nil nil
6399 (font-lock-syntactic-face-function
6400 . python-font-lock-syntactic-face-function)
6401 (font-lock-extend-after-change-region-function
6402 . python-font-lock-extend-region)))
6403 (setq-local imenu-create-index-function
6404 #'python-imenu-create-index)
6405 (add-hook 'which-func-functions #'python-info-current-defun nil t))))
6406
6407;;;###autoload 6378;;;###autoload
6408(define-derived-mode python-mode prog-mode "Python" 6379(define-derived-mode python-mode prog-mode "Python"
6409 "Major mode for editing Python files. 6380 "Major mode for editing Python files.
@@ -6420,9 +6391,11 @@ BACKEND and WARN are explained in `treesit-mode-function'."
6420 6391
6421 (setq-local forward-sexp-function python-forward-sexp-function) 6392 (setq-local forward-sexp-function python-forward-sexp-function)
6422 6393
6423 (python--backend-toggle 'elisp nil) 6394 (setq-local font-lock-defaults
6424 6395 `(,python-font-lock-keywords
6425 (setq-local major-mode-backend-function #'python--backend-toggle) 6396 nil nil nil nil
6397 (font-lock-syntactic-face-function
6398 . python-font-lock-syntactic-face-function)))
6426 6399
6427 (setq-local syntax-propertize-function 6400 (setq-local syntax-propertize-function
6428 python-syntax-propertize-function) 6401 python-syntax-propertize-function)
@@ -6451,9 +6424,14 @@ BACKEND and WARN are explained in `treesit-mode-function'."
6451 (add-hook 'post-self-insert-hook 6424 (add-hook 'post-self-insert-hook
6452 #'python-indent-post-self-insert-function 'append 'local) 6425 #'python-indent-post-self-insert-function 'append 'local)
6453 6426
6427 (setq-local imenu-create-index-function
6428 #'python-imenu-create-index)
6429
6454 (setq-local add-log-current-defun-function 6430 (setq-local add-log-current-defun-function
6455 #'python-info-current-defun) 6431 #'python-info-current-defun)
6456 6432
6433 (add-hook 'which-func-functions #'python-info-current-defun nil t)
6434
6457 (setq-local skeleton-further-elements 6435 (setq-local skeleton-further-elements
6458 '((abbrev-mode nil) 6436 '((abbrev-mode nil)
6459 (< '(backward-delete-char-untabify (min python-indent-offset 6437 (< '(backward-delete-char-untabify (min python-indent-offset
@@ -6462,13 +6440,13 @@ BACKEND and WARN are explained in `treesit-mode-function'."
6462 6440
6463 (with-no-warnings 6441 (with-no-warnings
6464 ;; suppress warnings about eldoc-documentation-function being obsolete 6442 ;; suppress warnings about eldoc-documentation-function being obsolete
6465 (if (null eldoc-documentation-function) 6443 (if (null eldoc-documentation-function)
6466 ;; Emacs<25 6444 ;; Emacs<25
6467 (setq-local eldoc-documentation-function #'python-eldoc-function) 6445 (setq-local eldoc-documentation-function #'python-eldoc-function)
6468 (if (boundp 'eldoc-documentation-functions) 6446 (if (boundp 'eldoc-documentation-functions)
6469 (add-hook 'eldoc-documentation-functions #'python-eldoc-function nil t) 6447 (add-hook 'eldoc-documentation-functions #'python-eldoc-function nil t)
6470 (add-function :before-until (local 'eldoc-documentation-function) 6448 (add-function :before-until (local 'eldoc-documentation-function)
6471 #'python-eldoc-function)))) 6449 #'python-eldoc-function))))
6472 6450
6473 (add-to-list 6451 (add-to-list
6474 'hs-special-modes-alist 6452 'hs-special-modes-alist
@@ -6499,7 +6477,15 @@ BACKEND and WARN are explained in `treesit-mode-function'."
6499 (when python-indent-guess-indent-offset 6477 (when python-indent-guess-indent-offset
6500 (python-indent-guess-indent-offset)) 6478 (python-indent-guess-indent-offset))
6501 6479
6502 (add-hook 'flymake-diagnostic-functions #'python-flymake nil t)) 6480 (add-hook 'flymake-diagnostic-functions #'python-flymake nil t)
6481
6482 (setq-local treesit-mode-supported t)
6483 (setq-local treesit-required-languages '(python))
6484 (setq-local treesit-font-lock-feature-list
6485 '((basic) (moderate) (elaborate)))
6486 (setq-local treesit-font-lock-settings python--treesit-settings)
6487 (setq-local treesit-imenu-function
6488 #'python-imenu-treesit-create-index))
6503 6489
6504;;; Completion predicates for M-x 6490;;; Completion predicates for M-x
6505;; Commands that only make sense when editing Python code 6491;; Commands that only make sense when editing Python code