[gtksourceview: 2/8] typescript.lang: Add support for nullish coalescing



commit 0acf2092e6c378bb9d14951b168b32bafc41cfd4
Author: Jeffery To <jeffery to gmail com>
Date:   Fri Nov 15 03:44:59 2019 +0800

    typescript.lang: Add support for nullish coalescing
    
    Added in TypeScript 3.7.
    
    Based on the stage 3 proposal:
    https://github.com/tc39/proposal-nullish-coalescing/
    
    Reordering the binary and ternary operator contexts in
    javascript-expressions.lang should have no functional impact on
    JavaScript highlighting (they are mutually exclusive), but makes this
    change much easier.

 data/language-specs/javascript-expressions.lang    |  4 +-
 data/language-specs/typescript-js-expressions.lang | 44 ++++++++++++++++++++++
 data/language-specs/typescript.lang                |  5 +++
 tests/syntax-highlighting/file.ts                  |  4 ++
 tests/syntax-highlighting/file.tsx                 |  4 ++
 5 files changed, 59 insertions(+), 2 deletions(-)
---
diff --git a/data/language-specs/javascript-expressions.lang b/data/language-specs/javascript-expressions.lang
index cf579b9e..46a276c8 100644
--- a/data/language-specs/javascript-expressions.lang
+++ b/data/language-specs/javascript-expressions.lang
@@ -636,9 +636,9 @@
         <context ref="js:embedded-lang-hooks"/>
         <context ref="_increment-decrement-operators"/>
         <context ref="js-fn:arrow-functions"/>
-        <context ref="_ternary-operators-without-comma"/>
         <context ref="_keyword-binary-operators-without-comma"/>
         <context ref="_binary-operators-without-comma"/>
+        <context ref="_ternary-operators-without-comma"/>
       </include>
     </context> <!-- /_post-lhs-expression-without-comma -->
 
@@ -657,9 +657,9 @@
         <context ref="js:embedded-lang-hooks"/>
         <context ref="_increment-decrement-operators"/>
         <context ref="js-fn:arrow-functions"/>
-        <context ref="_ternary-operators-with-comma"/>
         <context ref="_keyword-binary-operators-with-comma"/>
         <context ref="_binary-operators-with-comma"/>
+        <context ref="_ternary-operators-with-comma"/>
       </include>
     </context> <!-- /_post-lhs-expression-with-comma -->
 
diff --git a/data/language-specs/typescript-js-expressions.lang 
b/data/language-specs/typescript-js-expressions.lang
index c4a629bd..bc1b4eb3 100644
--- a/data/language-specs/typescript-js-expressions.lang
+++ b/data/language-specs/typescript-js-expressions.lang
@@ -182,6 +182,50 @@
     </context> <!-- /pre-lhs-expression -->
 
 
+    <!-- # Binary operators -->
+
+    <!-- excluding comma operator -->
+    <define-regex id="_binary-operator" extended="true">
+      (?:
+        \*\* =? |                            # exponentiation (assignment) (ES2016)
+        [+/*%-] =? |                         # arithmetic (assignment)
+        [!=]==? |                            # equality
+        &amp;&amp; | \|\| |                  # logical
+        \?\? |                               # nullish coalescing (stage 3 proposal)
+        [&amp;|^] =? |                       # bitwise logical (assignment)
+        (?: &lt;&lt; | &gt;&gt;&gt;? ) =? |  # bitwise shift (assignment)
+        [&lt;&gt;]=? |                       # relational
+        =                                    # assignment
+      )
+    </define-regex> <!-- /_binary-operator -->
+
+    <!-- ## Without comma -->
+
+    <!-- replaces js-expr:_binary-operators-without-comma -->
+    <context id="binary-operators-without-comma">
+      <start>\%{_binary-operator}</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="js:binary-operator"/>
+        <context ref="js:comments"/>
+        <context ref="js-expr:_binary-operator-without-comma-content"/>
+      </include>
+    </context> <!-- /binary-operators-without-comma -->
+
+    <!-- ## With comma -->
+
+    <!-- replaces js-expr:_binary-operators-with-comma -->
+    <context id="binary-operators-with-comma">
+      <start>(\%{_binary-operator}|,)</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="js:binary-operator"/>
+        <context ref="js:comments"/>
+        <context ref="js-expr:_binary-operator-with-comma-content"/>
+      </include>
+    </context> <!-- /binary-operators-with-comma -->
+
+
     <!-- # as operator (type assertion / cast)
 
          a = obj as string;
diff --git a/data/language-specs/typescript.lang b/data/language-specs/typescript.lang
index 37434333..d453995e 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -752,6 +752,11 @@
 
     <replace id="js-expr:_pre-lhs-expression" ref="typescript-js-expr:pre-lhs-expression"/>
 
+    <!-- ### Binary operators -->
+
+    <replace id="js-expr:_binary-operators-without-comma" 
ref="typescript-js-expr:binary-operators-without-comma"/>
+    <replace id="js-expr:_binary-operators-with-comma" ref="typescript-js-expr:binary-operators-with-comma"/>
+
     <!-- ### Post-LHS expression -->
 
     <replace id="js-expr:_post-lhs-expression-without-comma" 
ref="typescript-js-expr:post-lhs-expression-without-comma"/>
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index 1fbe09cd..c797c191 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -477,6 +477,10 @@ obj?.[expr];
 func?.(...args);
 foo?.3:0; // correctly highlighted as the ternary operator, not optional chaining
 
+// Nullish coalescing (stage 3 proposal)
+( obj ?? 'default value' );
+a = foo ?? 1, bar ?? 2;
+
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index caabf38e..0450515e 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -580,6 +580,10 @@ obj?.[expr];
 func?.(...args);
 foo?.3:0; // correctly highlighted as the ternary operator, not optional chaining
 
+// Nullish coalescing (stage 3 proposal)
+( obj ?? 'default value' );
+a = foo ?? 1, bar ?? 2;
+
 // Type arguments for function calls
 fn<string>();
 fn<string, number>();


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