From 7f0984b9c9d0f2b5c05dcb280d2938deabfae396 Mon Sep 17 00:00:00 2001 From: William Oprandi Date: Sun, 26 May 2024 14:34:43 +0200 Subject: [PATCH] Dart port of syntax highlighting --- .../grammars/graphql.dart.json | 50 +++++++++++++++++++ .../vscode-graphql-syntax/grammars/test.dart | 6 +++ packages/vscode-graphql-syntax/package.json | 11 ++++ .../tests/__fixtures__/test.dart | 11 ++++ .../tests/__fixtures__/test.py | 1 + .../__snapshots__/dart-grammar.spec.ts.snap | 32 ++++++++++++ .../tests/dart-grammar.spec.ts | 10 ++++ 7 files changed, 121 insertions(+) create mode 100644 packages/vscode-graphql-syntax/grammars/graphql.dart.json create mode 100644 packages/vscode-graphql-syntax/grammars/test.dart create mode 100644 packages/vscode-graphql-syntax/tests/__fixtures__/test.dart create mode 100644 packages/vscode-graphql-syntax/tests/__snapshots__/dart-grammar.spec.ts.snap create mode 100644 packages/vscode-graphql-syntax/tests/dart-grammar.spec.ts diff --git a/packages/vscode-graphql-syntax/grammars/graphql.dart.json b/packages/vscode-graphql-syntax/grammars/graphql.dart.json new file mode 100644 index 00000000000..e1b3bffb6b9 --- /dev/null +++ b/packages/vscode-graphql-syntax/grammars/graphql.dart.json @@ -0,0 +1,50 @@ +{ + "scopeName": "inline.graphql.dart", + "injectionSelector": "L:(meta.embedded.block.dart | source.dart -string -comment)", + "patterns": [ + { + "begin": "(''')(#graphql)", + "beginCaptures": { + "1": { + "name": "string.interpolated.triple.single.dart" + }, + "2": { + "name": "comment.line.graphql.js" + } + }, + "end": "(''')", + "endCaptures": { + "1": { + "name": "string.interpolated.triple.single.dart" + } + }, + "patterns": [ + { + "include": "source.graphql" + } + ] + }, + { + "begin": "(\"\"\")(#graphql)", + "beginCaptures": { + "1": { + "name": "string.interpolated.triple.double.dart" + }, + "2": { + "name": "comment.line.graphql.js" + } + }, + "end": "(\"\"\")", + "endCaptures": { + "1": { + "name": "string.interpolated.triple.double.dart" + } + }, + "patterns": [ + { + "include": "source.graphql" + } + ] + } + ] +} diff --git a/packages/vscode-graphql-syntax/grammars/test.dart b/packages/vscode-graphql-syntax/grammars/test.dart new file mode 100644 index 00000000000..9effc8a9751 --- /dev/null +++ b/packages/vscode-graphql-syntax/grammars/test.dart @@ -0,0 +1,6 @@ +const test = """#graphql + +"""; + +const tet = '''#graphql +'''; diff --git a/packages/vscode-graphql-syntax/package.json b/packages/vscode-graphql-syntax/package.json index c0c7fa15b8d..bdd3402f753 100644 --- a/packages/vscode-graphql-syntax/package.json +++ b/packages/vscode-graphql-syntax/package.json @@ -143,6 +143,17 @@ "embeddedLanguages": { "meta.embedded.block.graphql": "graphql" } + }, + { + "injectTo": [ + "source.dart", + "text.html.markdown" + ], + "scopeName": "inline.graphql.dart", + "path": "./grammars/graphql.dart.json", + "embeddedLanguages": { + "meta.embedded.block.graphql": "graphql" + } } ] }, diff --git a/packages/vscode-graphql-syntax/tests/__fixtures__/test.dart b/packages/vscode-graphql-syntax/tests/__fixtures__/test.dart new file mode 100644 index 00000000000..bafcb6741d6 --- /dev/null +++ b/packages/vscode-graphql-syntax/tests/__fixtures__/test.dart @@ -0,0 +1,11 @@ +const query1 = '''#graphql + query { + id + } +'''; + +const query2 = """#graphql + query { + id + } +"""; diff --git a/packages/vscode-graphql-syntax/tests/__fixtures__/test.py b/packages/vscode-graphql-syntax/tests/__fixtures__/test.py index f8affc9340e..6ab8581b1fd 100644 --- a/packages/vscode-graphql-syntax/tests/__fixtures__/test.py +++ b/packages/vscode-graphql-syntax/tests/__fixtures__/test.py @@ -61,3 +61,4 @@ name } }""" + diff --git a/packages/vscode-graphql-syntax/tests/__snapshots__/dart-grammar.spec.ts.snap b/packages/vscode-graphql-syntax/tests/__snapshots__/dart-grammar.spec.ts.snap new file mode 100644 index 00000000000..5c926773680 --- /dev/null +++ b/packages/vscode-graphql-syntax/tests/__snapshots__/dart-grammar.spec.ts.snap @@ -0,0 +1,32 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`inline.graphql.dart grammar > should tokenize a simple dart file 1`] = ` +const query1 = | +''' | string.interpolated.triple.single.dart +#graphql | comment.line.graphql.js + | +query | keyword.operation.graphql + | meta.selectionset.graphql +{ | meta.selectionset.graphql punctuation.operation.graphql + | meta.selectionset.graphql +id | meta.selectionset.graphql variable.graphql + | meta.selectionset.graphql +} | meta.selectionset.graphql punctuation.operation.graphql +''' | string.interpolated.triple.single.dart +; | + | +const query2 = | +""" | string.interpolated.triple.double.dart +#graphql | comment.line.graphql.js + | +query | keyword.operation.graphql + | meta.selectionset.graphql +{ | meta.selectionset.graphql punctuation.operation.graphql + | meta.selectionset.graphql +id | meta.selectionset.graphql variable.graphql + | meta.selectionset.graphql +} | meta.selectionset.graphql punctuation.operation.graphql +""" | string.interpolated.triple.double.dart +; | + | +`; diff --git a/packages/vscode-graphql-syntax/tests/dart-grammar.spec.ts b/packages/vscode-graphql-syntax/tests/dart-grammar.spec.ts new file mode 100644 index 00000000000..853f360632a --- /dev/null +++ b/packages/vscode-graphql-syntax/tests/dart-grammar.spec.ts @@ -0,0 +1,10 @@ +import { tokenizeFile } from './__utilities__/utilities'; + +describe('inline.graphql.dart grammar', () => { + const scope = 'inline.graphql.dart'; + + it('should tokenize a simple dart file', async () => { + const result = await tokenizeFile('__fixtures__/test.dart', scope); + expect(result).toMatchSnapshot(); + }); +});