]> git.eshelyaron.com Git - emacs.git/commitdiff
Split python-mode into native and tree-sitter variant
authorYuan Fu <casouri@gmail.com>
Sun, 20 Nov 2022 00:09:08 +0000 (16:09 -0800)
committerYuan Fu <casouri@gmail.com>
Sun, 20 Nov 2022 00:09:08 +0000 (16:09 -0800)
* lisp/progmodes/python.el (python-base-mode): New virtual mode that
contains most of the setup.
(python-mode): Change to inherit from python-base-mode.
(python-ts-mode): New mode that sets up tree-sitter.

lisp/progmodes/python.el

index b9b71a57d7dac175408f3e6acbb9c0632cca2b1b..01a6887bb6e85d51446cb0dd7b9549a9e40d5d12 100644 (file)
@@ -6482,10 +6482,12 @@ Add import for undefined name `%s' (empty to skip): "
 (defvar prettify-symbols-alist)
 
 ;;;###autoload
-(define-derived-mode python-mode prog-mode "Python"
-  "Major mode for editing Python files.
+(define-derived-mode python-base-mode prog-mode "Python"
+  "Generic major mode for editing Python files.
 
-\\{python-mode-map}"
+This is a generic major mode intended to be inherited by a
+concrete implementations.  Currently there two concrete
+implementations: `python-mode' and `python-ts-mode'."
   (setq-local tab-width 8)
   (setq-local indent-tabs-mode nil)
 
@@ -6569,11 +6571,30 @@ Add import for undefined name `%s' (empty to skip): "
   (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))
+
+;;;###autoload
+(define-derived-mode python-mode python-base-mode "Python"
+  "Major mode for editing Python files.
 
-  (cond
-   ;; Tree-sitter.
-   ((treesit-ready-p 'python-mode 'python)
+\\{python-mode-map}"
+  (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)
+  (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-ts-mode python-base-mode "Python"
+  "Major mode for editing Python files, using tree-sitter library.
+
+\\{python-mode-map}"
+  (when (treesit-ready-p 'python-mode 'python)
     (treesit-parser-create 'python)
     (setq-local treesit-font-lock-feature-list
                 '(( comment string function-name class-name)
@@ -6587,19 +6608,7 @@ Add import for undefined name `%s' (empty to skip): "
     (setq-local beginning-of-defun-function
                 #'python-treesit-beginning-of-defun)
     (setq-local end-of-defun-function #'python-treesit-end-of-defun)
-    (treesit-major-mode-setup))
-   ;; Elisp.
-   (t
-    (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)
-    (setq-local imenu-create-index-function
-                #'python-imenu-create-index)
-    (add-hook 'which-func-functions #'python-info-current-defun nil t))))
+    (treesit-major-mode-setup)))
 
 ;;; Completion predicates for M-x
 ;; Commands that only make sense when editing Python code