From 48fb6f8e949a8caf73b0714d647947c069260797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20P=2E=20L=2E=20de=20Carvalho?= Date: Thu, 27 Oct 2022 15:45:56 -0600 Subject: [PATCH] Initial fontification in sh-mode with tree-sitter --- lisp/progmodes/sh-script.el | 144 +++++++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 11 deletions(-) diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 558b62b20a..c3645eb9e9 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -148,6 +148,7 @@ (require 'let-alist) (require 'subr-x)) (require 'executable) +(require 'treesit) (autoload 'comint-completion-at-point "comint") (autoload 'comint-filename-completion "comint") @@ -1534,13 +1535,6 @@ sh-mode ;; we can't look if previous line ended with `\' (setq-local comint-prompt-regexp "^[ \t]*") (setq-local imenu-case-fold-search nil) - (setq font-lock-defaults - `((sh-font-lock-keywords - sh-font-lock-keywords-1 sh-font-lock-keywords-2) - nil nil - ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")) nil - (font-lock-syntactic-face-function - . ,#'sh-font-lock-syntactic-face-function))) (setq-local syntax-propertize-function #'sh-syntax-propertize-function) (add-hook 'syntax-propertize-extend-region-functions #'syntax-propertize-multiline 'append 'local) @@ -1587,7 +1581,26 @@ sh-mode nil nil) (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t) (add-hook 'hack-local-variables-hook - #'sh-after-hack-local-variables nil t)) + #'sh-after-hack-local-variables nil t) + + (cond + ;; Tree-sitter + ((treesit-ready-p 'sh-mode sh-shell) + (setq-local font-lock-keywords-only t) + (setq-local treesit-font-lock-feature-list + '((basic) (moderate) (elaborate))) + (setq-local treesit-font-lock-settings + sh-mode--treesit-settings) + (treesit-major-mode-setup)) + ;; Elisp. + (t + (setq font-lock-defaults + `((sh-font-lock-keywords + sh-font-lock-keywords-1 sh-font-lock-keywords-2) + nil nil + ((?/ . "w") (?~ . "w") (?. . "w") (?- . "w") (?_ . "w")) nil + (font-lock-syntactic-face-function + . ,#'sh-font-lock-syntactic-face-function)))))) ;;;###autoload (defalias 'shell-script-mode 'sh-mode) @@ -3191,6 +3204,115 @@ sh-shellcheck-flymake (process-send-region sh--shellcheck-process (point-min) (point-max)) (process-send-eof sh--shellcheck-process)))) -(provide 'sh-script) - -;;; sh-script.el ends here +;;; Tree-sitter font-lock + +(defface sh-mode--treesit-special-var-name-face + '((t (:inherit font-lock-builtin-face))) + "Face name to use for special `sh-mode' variables (e.g. PATH)") + +(defface sh-mode--treesit-operator-face + '((t (:inherit font-lock-builtin-face))) + "Face name to use for `sh-mode' operators (e.g. <<)") + +(defface sh-mode--treesit-call-face + '((t (:inherit font-lock-function-name-face))) + "Face name to use for `sh-mode' non-builtin command calls") + +(defvar sh-mode--treesit-operators + '("|" "|&" "||" "&&" ">" ">>" "<" "<<" "<<-" "<<<" "==" "!=" ";" + ";;" ";&" ";;&") + "List of `sh-mode' operator to fontify") + +(defvar sh-mode--treesit-keywords + '("case" "do" "done" "elif" "else" "esac" "export" "fi" "for" + "function" "if" "in" "unset" "while" "then") + "Minimal list of keywords that belong to tree-sitter-bash's grammar. + +Some reserved words are not recognize to keep the grammar +simpler. Those are identified with regex-based filtered queries. + +See `sh-mode--treesit-other-keywords' and +`sh-mode--treesit-settings').") + +(defun sh-mode--treesit-other-keywords () + "Returns a list `others' of key/reserved words to be fontified with +regex-based queries as they are not part of tree-sitter-bash's +grammar. + +See `sh-mode--treesit-other-keywords' and +`sh-mode--treesit-settings')." + (let ((minimal sh-mode--treesit-keywords) + (all (append (sh-feature sh-leading-keywords) + (sh-feature sh-other-keywords))) + (others)) + (dolist (keyword all others) + (if (not (member keyword minimal)) + (setq others (cons keyword others)))))) + +(defvar sh-mode--treesit-settings + (treesit-font-lock-rules + :language sh-shell + :feature 'basic + :override t + '(;; function + (function_definition name: (word) @font-lock-function-name-face) + ;; comments + (comment) @font-lock-comment-face + ;; strings and heredoc + [ (string) (raw_string) ] @font-lock-string-face + ;; heredocs + [ (heredoc_start) (heredoc_body) ] @sh-heredoc + ;; variables + (variable_name) @font-lock-variable-name-face) + :language sh-shell + :feature 'moderate + :override t + `(;; keywords + [ ,@sh-mode--treesit-keywords ] @font-lock-keyword-face + ;; reserved words + (command_name + ((word) @font-lock-keyword-face + (:match + ,(rx-to-string + `(seq bol + (or ,@(sh-mode--treesit-other-keywords)) + eol)) + @font-lock-keyword-face))) + ;; function/non-builtin command calls + (command_name (word) @sh-mode--treesit-call-face) + ;; builtin commands + (command_name + ((word) @font-lock-builtin-face + (:match ,(let ((builtins + (sh-feature sh-builtins))) + (rx-to-string + `(seq bol + (or ,@builtins) + eol))) + @font-lock-builtin-face))) + ;; declaration commands + (declaration_command) @font-lock-builtin-face + ;; variables + (variable_name) @font-lock-variable-name-face) + :language sh-shell + :feature 'elaborate + :override t + `(;; everything inside command substitution + (command_substitution _ _ @sh-quoted-exec _) + ;; constants + (case_item value: (word) @font-lock-constant-face) + (file_descriptor) @font-lock-constant-face + ;; operators + [ ,@sh-mode--treesit-operators ] @sh-mode--treesit-operator-face + ;; special variables + ((variable_name) @sh-mode--treesit-special-var-name-face + (:match ,(let ((builtin-vars (sh-feature sh-variables))) + (rx-to-string + `(seq bol + (or ,@builtin-vars) + eol))) + @sh-mode--treesit-special-var-name-face)))) + "Tree-sitter font-lock settings for `sh-mode'.") + +(provide 'sh-mode) +;;; sh-mode.el ends here -- 2.31.1