Skip to content

[vscode-graphql-syntax] Add support for GraphQL embedded in Hack files #3872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-rings-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vscode-graphql-syntax': minor
---

Added support for GraphQL embedded in Hack files
1 change: 1 addition & 0 deletions packages/vscode-graphql-syntax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ matching.
- PHP (example: [test.php](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.php))
- Markdown (examples: [test.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.md) & [test-py.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test-py.md))
- Scala (example: [test.scala](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.scala))
- Hack (example: [test.hack](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.hack))

You'll want to install this if you do not use `graphql-config`, or want to use
the highlighting with other extensions than `vscode-graphql`
Expand Down
130 changes: 130 additions & 0 deletions packages/vscode-graphql-syntax/grammars/graphql.hack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"scopeName": "inline.graphql.hack",
"injectionSelector": "L:(meta.embedded.block.hack | source.hack -string -comment)",
"patterns": [
{
"contentName": "meta.embedded.block.graphql",
"begin": "(<<<)\\s*(\"?)(GRAPHQL|GQL)(\\2)(\\s*)$",
"beginCaptures": {
"0": {
"name": "punctuation.section.embedded.begin.php"
},
"1": {
"name": "punctuation.definition.string.php"
},
"3": {
"name": "keyword.operator.heredoc.php"
},
"5": {
"name": "invalid.illegal.trailing-whitespace.php"
}
},
"end": "^\\s*(\\3)(?![A-Za-z0-9_\\x{7f}-\\x{10ffff}])",
"endCaptures": {
"0": {
"name": "punctuation.section.embedded.end.php"
},
"1": {
"name": "keyword.operator.heredoc.php"
}
},
"patterns": [
{
"include": "source.graphql"
}
]
},
{
"begin": "(<<<)\\s*'(GRAPHQL|GQL)'(\\s*)$",
"beginCaptures": {
"0": {
"name": "punctuation.section.embedded.begin.php"
},
"1": {
"name": "punctuation.definition.string.php"
},
"2": {
"name": "keyword.operator.nowdoc.php"
},
"3": {
"name": "invalid.illegal.trailing-whitespace.php"
}
},
"contentName": "source.graphql",
"end": "^\\s*(\\2)(?![A-Za-z0-9_\\x{7f}-\\x{10ffff}])",
"endCaptures": {
"0": {
"name": "punctuation.section.embedded.end.php"
},
"1": {
"name": "keyword.operator.nowdoc.php"
}
},
"name": "meta.embedded.graphql",
"patterns": [
{
"include": "source.graphql"
}
]
},
{
"begin": "(/\\*\\*\\s*(@lang\\s*GraphQL|Graphi?QL|graphql)\\s*\\*/)\\s*(')",
"beginCaptures": {
"1": {
"name": "punctuation.definition.comment.php"
},
"3": {
"name": "punctuation.definition.string.begin.php"
}
},
"contentName": "source.graphql",
"end": "'",
"endCaptures": {
"0": {
"name": "punctuation.definition.string.end.php"
}
},
"name": "meta.embedded.graphql",
"patterns": [
{
"include": "source.graphql"
}
]
},
{
"begin": "((/\\*\\*|//)\\s*(@lang\\s*GraphQL|Graphi?QL|graphql)\\s*(\\*/)?)(\\s*)$",
"beginCaptures": {
"1": {
"name": "punctuation.definition.comment.php"
},
"5": {
"name": "invalid.illegal.trailing-whitespace.php"
}
},
"end": "(?<=')",
"patterns": [
{
"begin": "'",
"beginCaptures": {
"0": {
"name": "punctuation.definition.string.begin.php"
}
},
"contentName": "source.graphql",
"end": "'",
"endCaptures": {
"0": {
"name": "punctuation.definition.string.end.php"
}
},
"name": "meta.embedded.graphql",
"patterns": [
{
"include": "source.graphql"
}
]
}
]
}
]
}
12 changes: 12 additions & 0 deletions packages/vscode-graphql-syntax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
"embeddedLanguages": {
"meta.embedded.block.graphql": "graphql"
}
},
{
"injectTo": [
"source.hack",
"text.html.markdown",
"text.html.derivative"
],
"scopeName": "inline.graphql.hack",
"path": "./grammars/graphql.hack.json",
"embeddedLanguages": {
"meta.embedded.block.graphql": "graphql"
}
}
]
},
Expand Down
17 changes: 17 additions & 0 deletions packages/vscode-graphql-syntax/tests/__fixtures__/test.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?hh

$query = <<<GRAPHQL
query {
site {
name
}
}
GRAPHQL;

$query = /** @lang GraphQL */ '
query {
site {
name
}
}
';
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`inline.graphql.hack grammar > should tokenize a simple hack file 1`] = `
<?hh |
|
$query = |
<<< | punctuation.section.embedded.begin.php punctuation.definition.string.php
GRAPHQL | punctuation.section.embedded.begin.php keyword.operator.heredoc.php
| meta.embedded.block.graphql
query | meta.embedded.block.graphql keyword.operation.graphql
| meta.embedded.block.graphql meta.selectionset.graphql
{ | meta.embedded.block.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.block.graphql meta.selectionset.graphql
site | meta.embedded.block.graphql meta.selectionset.graphql variable.graphql
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
{ | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
name | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql variable.graphql
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
} | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.block.graphql meta.selectionset.graphql
} | meta.embedded.block.graphql meta.selectionset.graphql punctuation.operation.graphql
GRAPHQL | punctuation.section.embedded.end.php keyword.operator.heredoc.php
; |
|
$query = |
/** @lang GraphQL */ | meta.embedded.graphql punctuation.definition.comment.php
| meta.embedded.graphql
' | meta.embedded.graphql punctuation.definition.string.begin.php
| meta.embedded.graphql source.graphql
query | meta.embedded.graphql source.graphql keyword.operation.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql
{ | meta.embedded.graphql source.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql
site | meta.embedded.graphql source.graphql meta.selectionset.graphql variable.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
{ | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
name | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql variable.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
} | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
| meta.embedded.graphql source.graphql meta.selectionset.graphql
} | meta.embedded.graphql source.graphql meta.selectionset.graphql punctuation.operation.graphql
' | meta.embedded.graphql punctuation.definition.string.end.php
; |
|
`;
10 changes: 10 additions & 0 deletions packages/vscode-graphql-syntax/tests/hack-grammar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { tokenizeFile } from './__utilities__/utilities';

describe('inline.graphql.hack grammar', () => {
const scope = 'inline.graphql.hack';

it('should tokenize a simple hack file', async () => {
const result = await tokenizeFile('__fixtures__/test.hack', scope);
expect(result).toMatchSnapshot();
});
});
Loading