From d52f449bfa5cc7265170b1ebebe4581aa31ac539 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 | 153 +++++++++++++++++++++++++++++++++--- 1 file changed, 142 insertions(+), 11 deletions(-) diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 558b62b20a..17f938e95a 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,28 @@ 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 + '((comments functions strings heredocs) + (variables keywords commands decl-commands) + (constants operators builtin-variables))) + (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 +3206,122 @@ 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 + +(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)))))) + +(defun sh-mode--treesit-fontify-decl-command (node override _start _end) + "Fontifies only the name of declaration_command nodes. + +This is used instead of `font-lock-builtion-face' directly because +otherwise the whole command, including the variable assignment part, +is fontified with with `font-lock-builtin-face'. An alternative to +this would be to declaration_command nodes to have a `name:' field." + (let* ((maybe-decl-cmd (treesit-node-parent node)) + (node-type (treesit-node-type maybe-decl-cmd))) + (when (string= node-type "declaration_command") + (let* ((name-node (car (treesit-node-children maybe-decl-cmd))) + (name-beg (treesit-node-start name-node)) + (name-end (treesit-node-end name-node))) + (put-text-property name-beg + name-end + 'face + font-lock-builtin-face))))) + +(defvar sh-mode--treesit-settings + (treesit-font-lock-rules + :feature 'comments + :language sh-shell + '((comment) @font-lock-comment-face) + :feature 'functions + :language sh-shell + '((function_definition name: (word) @font-lock-function-name-face)) + :feature 'strings + :language sh-shell + '([(string) (raw_string)] @font-lock-string-face) + :feature 'heredocs + :language sh-shell + '([(heredoc_start) (heredoc_body)] @sh-heredoc) + :feature 'variables + :language sh-shell + '((variable_name) @font-lock-variable-name-face) + :feature 'keywords + :language sh-shell + `(;; 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)))) + :feature 'commands + :language sh-shell + `(;; function/non-builtin command calls + (command_name (word) @font-lock-function-name-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)))) + :feature 'decl-commands + :language sh-shell + '(;; declaration commands + (declaration_command _ @sh-mode--treesit-fontify-decl-command)) + :feature 'constants + :language sh-shell + '((case_item value: (word) @font-lock-constant-face) + (file_descriptor) @font-lock-constant-face) + :feature 'operators + :language sh-shell + `([ ,@sh-mode--treesit-operators ] @font-lock-builtin-face) + :feature 'builtin-variables + :language sh-shell + `(((special_variable_name) @font-lock-builtin-face + (:match ,(let ((builtin-vars (sh-feature sh-variables))) + (rx-to-string + `(seq bol + (or ,@builtin-vars) + eol))) + @font-lock-builtin-face)))) + "Tree-sitter font-lock settings for `sh-mode'.") + +(provide 'sh-mode) +;;; sh-mode.el ends here -- 2.31.1