[gtksourceview] sh.lang: Introduce "command-substitution" context



commit b20035bf9b6ca433aae51e096f13a9d7ddd9a73b
Author: neyfag <11970-neyfag users noreply gitlab gnome org>
Date:   Wed Nov 13 20:27:01 2019 +0100

    sh.lang: Introduce "command-substitution" context

 data/language-specs/sh.lang       | 51 ++++++++++++++++++++++++++++-----------
 tests/syntax-highlighting/file.sh | 11 +++++++--
 2 files changed, 46 insertions(+), 16 deletions(-)
---
diff --git a/data/language-specs/sh.lang b/data/language-specs/sh.lang
index 1d073038..5611ddca 100644
--- a/data/language-specs/sh.lang
+++ b/data/language-specs/sh.lang
@@ -52,7 +52,7 @@
     <!-- we cannot use def:shell-like-comment, because
          m4.lang needs to replace this context -->
     <context id="line-comment" style-ref="comment" end-at-line-end="true" class="comment" 
class-disabled="no-spell-check">
-      <start>(?&lt;=\s)#</start>
+      <start>(?&lt;=\s|^)#</start>
       <include>
         <context ref="def:in-comment"/>
       </include>
@@ -66,13 +66,10 @@
         <context style-ref="def:special-char">
             <match>\\[$`"\\]</match>
         </context>
-        <!-- skip backslash where it's not an escaping char -->
-        <context>
-            <match>\\</match>
-        </context>
         <context ref="def:line-continue"/>
         <context ref="variable"/>
         <context ref="backtick-subshell"/>
+        <context ref="command-substitution"/>
       </include>
     </context>
 
@@ -81,8 +78,18 @@
       <end>'</end>
     </context>
 
-    <context id="subshell">
-      <start>\(</start>
+    <context id="subshell" style-ref="subshell">
+      <start>(?&lt;=\%{unescaped-space}|[`&amp;|;]|^)\((?!\()</start>
+      <end>\)(?=\s|[`&amp;|;]|$)</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="keyword"/>
+        <context sub-pattern="0" where="end" style-ref="keyword"/>
+        <context ref="sh"/>
+      </include>
+    </context>
+
+    <context id="command-substitution" style-ref="subshell">
+      <start>\$\((?!\()</start>
       <end>\)</end>
       <include>
         <context sub-pattern="0" where="start" style-ref="keyword"/>
@@ -91,17 +98,27 @@
       </include>
     </context>
 
+    <!-- Treated separately, not including itself nor command-like contexts
+      (see https://gitlab.gnome.org/GNOME/gtksourceview/issues/94) -->
     <context id="backtick-subshell" style-ref="subshell">
       <start>`</start>
       <end>`</end>
       <include>
-        <context ref="def:escape"/>
+        <context ref="line-comment"/>
         <context ref="def:line-continue"/>
-        <context ref="variable"/>
-        <context ref="built-in-command"/>
-        <context ref="common-command"/>
+        <context ref="def:escape"/>
         <context ref="single-quoted-string"/>
         <context ref="double-quoted-string"/>
+        <context ref="subshell"/>
+        <context ref="command-substitution"/>
+        <context ref="punctuator"/>
+        <context ref="function"/>
+        <context ref="here-doc"/>
+        <context ref="redirection"/>
+        <context ref="operator"/>
+        <context ref="variable"/>
+        <context ref="stand-alone-variable-definition"/>
+        <context ref="built-in-command"/>
       </include>
     </context>
 
@@ -188,16 +205,19 @@
         \$\{ (
           [-@?*\$0-9]
           | [!#]? ( \%{identifier} (\[ ([@*]|-?[0-9]+) \])? )?
-        )</start>
+        )
+      </start>
       <end>\}</end>
       <include>
         <context sub-pattern="0" where="start" style-ref="variable"/>
         <context sub-pattern="0" where="end" style-ref="variable"/>
         <context ref="def:escape"/>
+        <context ref="def:line-continue"/>
         <context ref="single-quoted-string"/>
         <context ref="double-quoted-string"/>
         <context ref="variable"/>
         <context ref="backtick-subshell"/>
+        <context ref="command-substitution"/>
       </include>
     </context>
 
@@ -215,7 +235,7 @@
       </include>
     </context>
 
-    <context id="variable-definition-command" end-at-line-end="true" extend-parent="false">
+    <context id="variable-definition-command" end-at-line-end="true">
       <start extended="true">
         (?&lt;=\%{unescaped-space}|\%[)
         (declare|local|typeset|readonly|export)
@@ -230,9 +250,10 @@
         <context ref="line-comment"/>
         <context ref="single-quoted-string"/>
         <context ref="double-quoted-string"/>
+        <context ref="here-doc"/>
         <context ref="redirection"/>
         <context ref="backtick-subshell"/>
-        <context ref="subshell"/>
+        <context ref="command-substitution"/>
         <context ref="variable"/>
         <context style-ref="variable-definition">
           <match>(?&lt;=\%{unescaped-space})\%{identifier}</match>
@@ -525,11 +546,13 @@
       <include>
         <context ref="def:shebang"/>
         <context ref="line-comment"/>
+        <context ref="def:line-continue"/>
         <context ref="def:escape"/>
         <context ref="single-quoted-string"/>
         <context ref="double-quoted-string"/>
         <context ref="subshell"/>
         <context ref="backtick-subshell"/>
+        <context ref="command-substitution"/>
         <context ref="punctuator"/>
         <context ref="function"/>
         <context ref="here-doc"/>
diff --git a/tests/syntax-highlighting/file.sh b/tests/syntax-highlighting/file.sh
index fe52c1e4..c2e06ba7 100644
--- a/tests/syntax-highlighting/file.sh
+++ b/tests/syntax-highlighting/file.sh
@@ -5,6 +5,8 @@ echo "Hi there!"
 
 xxx${xxx}xxx # Parameter in braces
 xxx${var/\"}xxx xxx${var/\}}xxx # Escaped characters
+xxx${var\
+/x/y} # Line continue
 
 xxx$0000 # One digit parameter
 xxx$-xxx xxx$$xxx xxx$@xxx # Special parameters
@@ -23,7 +25,7 @@ xxx${parameter%%word}xxx # Remove Largest Suffix Pattern
 xxx${parameter#word}xxx # Remove Smallest Prefix Pattern
 xxx${parameter##word}xxx # Remove Largest Prefix Pattern
 
-xxx${x:-$(ls)}xxx
+xxx${x:-$(ls ~/*)}xxx
 xxx${posix:?}xxx
 xxx${3:+posix}xxx
 xxx${#HOME}xxx
@@ -63,4 +65,9 @@ echo next line
 # Redirections
1>&2 &> 3>&-
 < 0<&3 3<&-
-3<> 1>|
\ No newline at end of file
+3<> 1>|
+
+# Quoting
+'no special characters'
+"$var, ${var/x/y}, $(cmd), `cmd`, \
+\$, \`, \", \\, \ "


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]