[gtksourceview: 5/14] typescript.lang: Add highlighting for type-only imports and exports



commit 8677453db64ae6b2608faacdebd2bd024f9958ec
Author: Jeffery To <jeffery to gmail com>
Date:   Wed Jun 3 17:28:30 2020 +0800

    typescript.lang: Add highlighting for type-only imports and exports
    
    This syntax was added in TypeScript 3.8. The supported forms are from
    the feature pull request[1].
    
    [1]: https://github.com/microsoft/TypeScript/pull/35200

 data/language-specs/typescript-js-modules.lang    | 71 ++++++++++++++++++++++-
 data/language-specs/typescript-js-statements.lang | 35 ++++-------
 data/language-specs/typescript.lang               |  5 ++
 tests/syntax-highlighting/file.ts                 |  7 +++
 tests/syntax-highlighting/file.tsx                |  7 +++
 5 files changed, 99 insertions(+), 26 deletions(-)
---
diff --git a/data/language-specs/typescript-js-modules.lang b/data/language-specs/typescript-js-modules.lang
index 63a693dd..e943160c 100644
--- a/data/language-specs/typescript-js-modules.lang
+++ b/data/language-specs/typescript-js-modules.lang
@@ -92,6 +92,42 @@
       </include>
     </context> <!-- /_choice-export-assignment -->
 
+    <!-- ## Type-only export or export type alias declaration
+
+         type-only export:
+         export type { T };
+         export type { T } from './mod';
+
+         export type alias declaration:
+         export type Name = string;
+    -->
+
+    <context id="_choice-export-type-alias-declaration" style-ref="typescript:type-alias-declaration" 
end-parent="true">
+      <start>\%{js:before-next-token}</start>
+      <end>\%{js:statement-end}</end>
+      <include>
+        <context ref="js:comments"/>
+        <context ref="typescript-js-st:_type-alias-declaration-content"/>
+      </include>
+    </context> <!-- /_choice-export-type-alias-declaration -->
+
+    <context id="_choice-export-type-only-or-type-alias-declaration" end-parent="true">
+      <start>\%{typescript:type-keyword}</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
+        <context ref="js:comments"/>
+
+        <context id="_export-type-only-or-type-alias-declaration-content">
+          <include>
+            <context ref="js-mod:_choice-export-named"/>
+            <context ref="_choice-export-type-alias-declaration"/>
+          </include>
+        </context> <!-- /_export-type-only-or-type-alias-declaration-content -->
+
+      </include>
+    </context> <!-- /_choice-export-type-only-or-type-alias-declaration -->
+
     <!-- ## Export declaration -->
 
     <!-- replaces js-mod:_export-declaration-content -->
@@ -105,7 +141,7 @@
         <context ref="_choice-import-default-declaration"/> <!-- for import alias declaration -->
         <context ref="typescript-js-st:choice-interface-declaration"/>
         <context ref="typescript-js-st:choice-namespace-declaration"/>
-        <context ref="typescript-js-st:choice-type-alias-declaration"/>
+        <context ref="_choice-export-type-only-or-type-alias-declaration"/>
         <context ref="js-mod:_export-declaration-content" original="true"/>
       </include>
     </context> <!-- /export-declaration-content -->
@@ -187,6 +223,31 @@
       </include>
     </context> <!-- /_choice-import-alias-or-require -->
 
+    <!-- ## Type-only import
+
+         import type T from './mod';
+         import type { A, B } from './mod';
+         import type * as Types from './mod';
+    -->
+
+    <context id="_choice-import-type-only" end-parent="true">
+      <start>\%{typescript:type-keyword}</start>
+      <end>\%{js:before-next-token}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
+        <context ref="js:comments"/>
+
+        <context id="_import-type-only-content">
+          <include>
+            <context ref="js-mod:_choice-import-named"/>
+            <context ref="js-mod:_choice-all-as-namespace"/>
+            <context ref="js-mod:_choice-import-default"/>
+          </include>
+        </context> <!-- _import-type-only-content -->
+
+      </include>
+    </context> <!-- /_choice-import-type-only -->
+
     <!-- ## Import default -->
 
     <!-- replaces js-mod:_import-default-content -->
@@ -219,5 +280,13 @@
       </include>
     </context> <!-- /_choice-import-default-declaration -->
 
+    <!-- replaces js-mod:_import-declaration-content -->
+    <context id="import-declaration-content">
+      <include>
+        <context ref="_choice-import-type-only"/>
+        <context ref="js-mod:_import-declaration-content" original="true"/>
+      </include>
+    </context> <!-- /import-declaration-content -->
+
   </definitions>
 </language>
diff --git a/data/language-specs/typescript-js-statements.lang 
b/data/language-specs/typescript-js-statements.lang
index fe21bcec..1559be19 100644
--- a/data/language-specs/typescript-js-statements.lang
+++ b/data/language-specs/typescript-js-statements.lang
@@ -471,10 +471,6 @@
          type Container<T> = { value: T };
     -->
 
-    <define-regex id="_type-alias-declaration-keyword" extended="true">
-      \%{js:keyword-start} type \%{js:keyword-end}
-    </define-regex> <!-- /_type-alias-declaration-keyword -->
-
     <context id="_type-alias-value" once-only="true">
       <start>=</start>
       <end>\%{js:before-next-token}</end>
@@ -498,35 +494,24 @@
       </include>
     </context> <!-- /_ordered-type-alias-value -->
 
-    <context id="_type-alias-declaration-content">
-      <include>
-        <context ref="js:ordered-identifier"/>
-        <context ref="typescript-type-gen:ordered-type-parameters-list"/>
-        <context ref="_ordered-type-alias-value"/>
-      </include>
-    </context> <!-- /_type-alias-declaration-content -->
-
     <!-- <TypeAliasDeclaration> -->
     <context id="_type-alias-declarations" style-ref="typescript:type-alias-declaration">
-      <start>\%{_type-alias-declaration-keyword}</start>
+      <start>\%{typescript:type-keyword}</start>
       <end>\%{js:statement-end}</end>
       <include>
         <context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
         <context ref="js:comments"/>
-        <context ref="_type-alias-declaration-content"/>
-      </include>
-    </context> <!-- /_type-alias-declarations -->
 
-    <!-- <TypeAliasDeclaration> -->
-    <context id="choice-type-alias-declaration" style-ref="typescript:type-alias-declaration" 
end-parent="true">
-      <start>\%{_type-alias-declaration-keyword}</start>
-      <end>\%{js:statement-end}</end>
-      <include>
-        <context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
-        <context ref="js:comments"/>
-        <context ref="_type-alias-declaration-content"/>
+        <context id="_type-alias-declaration-content">
+          <include>
+            <context ref="js:ordered-identifier"/>
+            <context ref="typescript-type-gen:ordered-type-parameters-list"/>
+            <context ref="_ordered-type-alias-value"/>
+          </include>
+        </context> <!-- /_type-alias-declaration-content -->
+
       </include>
-    </context> <!-- /choice-type-alias-declaration -->
+    </context> <!-- /_type-alias-declarations -->
 
 
     <!-- # Statements -->
diff --git a/data/language-specs/typescript.lang b/data/language-specs/typescript.lang
index 0adfa707..1c6c3471 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -122,6 +122,10 @@
       \%{js:keyword-start} class \%{js:keyword-end}
     </define-regex> <!-- /class-expression-keyword -->
 
+    <define-regex id="type-keyword" extended="true">
+      \%{js:keyword-start} type \%{js:keyword-end}
+    </define-regex> <!-- /type-keyword -->
+
     <context id="js-ordered-string" once-only="true">
       <start>\%{js:before-next-token}</start>
       <end>\%{js:before-next-token}</end>
@@ -798,6 +802,7 @@
     <!-- ### Import declaration -->
 
     <replace id="js-mod:_import-default-content" ref="typescript-js-mod:import-default-content"/>
+    <replace id="js-mod:_import-declaration-content" ref="typescript-js-mod:import-declaration-content"/>
 
 
     <!-- # Main context -->
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index 868e9362..8f30877b 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -569,6 +569,13 @@ import shortname = Long.Namespace.Name;
 // Import require
 import mod = require("module");
 
+// Type-only imports and exports
+import type T from './mod';
+import type { A, B } from './mod';
+import type * as Types from './mod';
+export type { T };
+export type { T } from './mod';
+
 
 /* Variable declaration */
 
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index 353c177c..fd13e4dc 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -677,6 +677,13 @@ import shortname = Long.Namespace.Name;
 // Import require
 import mod = require("module");
 
+// Type-only imports and exports
+import type T from './mod';
+import type { A, B } from './mod';
+import type * as Types from './mod';
+export type { T };
+export type { T } from './mod';
+
 
 /* Variable declaration */
 


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