Subject: bug#26217: bug#2910: 23.0.60; Shell-script coloring bug

From: Mauro Aranda <[email-masked]>
Date: Sat, 14 Oct 2023 09:44:37 -0300

tags 26217 patch
quit

On 13/10/23 13:06, Stefan Monnier wrote:
 >> I don't know what's the decision (if there is one) about situations like
 >> these.  If the bugs in lang-mode are fixed by lang-ts-mode, will these
 >> bug reports be treated as: wontfix? fixed? open in case someone wants to
 >> spend time in lang-mode?
 >
 > AFAIK we don't consider the `foo-ts-mode` to obsolete the other modes.
 > Maybe we will, but we don't yet.  IMO I think we'd first need to have
 > a good long-term strategy about what we'll do when tree-sitter becomes
 > unmaintained/obsolete.  IOW I think we need to develop our own layer of
 > abstraction above tree-sitter so that we can accommodate other
 > parser backends.
 >
 > FWIW, it's not clear at all what such a layer would look like, so we're
 > pretty far from it.  I'd welcome people start thinking about it, maybe
 > by looking at existing alternatives like our own `wisi` (in GNU ELPA),
 > SMIE, maybe LSP (assuming there are servers out there which can provide
 > that kind of functionality), etc...

Thank you for your response.  I don't know if this has been raised in
emacs-devel, but IMO it should be.

 >> - Wrong indentation for lines after:
 >> for i do echo 1; done
 >>
 >> I took a look at this, and ISTM that giving "do" a special treatment
 >> like sh-smie--sh-keyword-p gives to "in" might fix this, perhaps by
 >> reusing sh-smie--sh-keyword-in-p.
 >
 > Sounds about right.

I attach a patch that should also handle the fontification issue. It
also comes with some tests, and I've did some manual testing on my own.
I haven't found problems, but I'm suspicious because it seems too easy.
From 420fbfc1b2651839f214edbd809a663f42dcf080 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Sat, 14 Oct 2023 09:05:35 -0300
Subject: [PATCH] Fix indentation and fontification in shell-script (Bug#26217)

* lisp/progmodes/sh-script.el (sh-smie--sh-keyword-p): Treat "do" as
special, like we treat "in".
(sh-smie--sh-keyword-in-p): Change signature.  Take the token to
decide correctly if it's a keyword.
(sh-font-lock-keywords-var-1): Add do.

* test/lisp/progmodes/sh-script-resources/sh-indents.erts: New test.
* test/lisp/progmodes/sh-script-tests.el
(sh-script-test-do-fontification): New test.
---
 lisp/progmodes/sh-script.el                         | 13 ++++++++-----
 .../progmodes/sh-script-resources/sh-indents.erts   |  7 +++++++
 test/lisp/progmodes/sh-script-tests.el              | 11 +++++++++++
 3 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index cc521cb0591..de76e175a10 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -869,7 +869,7 @@ sh-font-lock-keywords-var
   "Default expressions to highlight in Shell Script modes.  See `sh-feature'.")
 
 (defvar sh-font-lock-keywords-var-1
-  '((sh "[ \t]in\\>"))
+  '((sh "[ \t]\\(in\\|do\\)\\>"))
   "Subdued level highlighting for Shell Script modes.")
 
 (defvar sh-font-lock-keywords-var-2 ()
@@ -1809,8 +1809,8 @@ sh-smie--sh-operators-back-re
   (concat "\\(?:^\\|[^\\]\\)\\(?:\\\\\\\\\\)*"
           "\\(" sh-smie--sh-operators-re "\\)"))
 
-(defun sh-smie--sh-keyword-in-p ()
-  "Assuming we're looking at \"in\", return non-nil if it's a keyword.
+(defun sh-smie--sh-keyword-in/do-p (tok)
+  "When looking at TOK (either \"in\" or \"do\"), non-nil if TOK is a keyword.
 Does not preserve point."
   (let ((forward-sexp-function nil)
         (words nil)                     ;We've seen words.
@@ -1832,7 +1832,10 @@ sh-smie--sh-keyword-in-p
        ((equal prev ";")
         (if words (setq newline t)
           (setq res 'keyword)))
-       ((member prev '("case" "for" "select")) (setq res 'keyword))
+       ((member prev (if (string= tok "in")
+                         '("case" "for" "select")
+                       '("for" "select")))
+        (setq res 'keyword))
        ((assoc prev smie-grammar) (setq res 'word))
        (t
         (if newline
@@ -1844,7 +1847,7 @@ sh-smie--sh-keyword-p
   "Non-nil if TOK (at which we're looking) really is a keyword."
   (cond
    ((looking-at "[[:alnum:]_]+=") nil)
-   ((equal tok "in") (sh-smie--sh-keyword-in-p))
+   ((member tok '("in" "do")) (sh-smie--sh-keyword-in/do-p tok))
    (t (sh-smie--keyword-p))))
 
 (defun sh-smie--default-forward-token ()
diff --git a/test/lisp/progmodes/sh-script-resources/sh-indents.erts b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
index 1f92610b3aa..36f4e4c22ab 100644
--- a/test/lisp/progmodes/sh-script-resources/sh-indents.erts
+++ b/test/lisp/progmodes/sh-script-resources/sh-indents.erts
@@ -38,3 +38,10 @@ if test ;then
 fi
 other
 =-=-=
+
+Name: sh-indents5
+
+=-=
+for i do echo 1; done
+for i; do echo 1; done
+=-=-=
diff --git a/test/lisp/progmodes/sh-script-tests.el b/test/lisp/progmodes/sh-script-tests.el
index 52c1303c414..135d7afe3fe 100644
--- a/test/lisp/progmodes/sh-script-tests.el
+++ b/test/lisp/progmodes/sh-script-tests.el
@@ -87,4 +87,15 @@ test-backward-token
   (should-not (test-sh-back "foo;bar"))
   (should (test-sh-back "foo#zot")))
 
+(ert-deftest sh-script-test-do-fontification ()
+  "Test that \"do\" gets fontified correctly, even with no \";\"."
+  (with-temp-buffer
+    (shell-script-mode)
+    (insert "for i do echo 1; done")
+    (font-lock-ensure)
+    (goto-char (point-min))
+    (search-forward "do")
+    (forward-char -1)
+    (should (equal (get-text-property (point) 'face) 'font-lock-keyword-face))))
+
 ;;; sh-script-tests.el ends here
-- 
2.34.1