Skip to content

Commit e5dd8ad

Browse files
committed
[vscode-graphql-syntax] Add support for GraphQL embedded in Hack files
Summary: This adds support for GraphQL strings within Hack files, bootstrapped from the existing PHP version. I followed the steps outlined in the README. Test Plan: - `yarn test -u` from package root - `yarn vsce:package` and install resulting VSIX - Load `test.hack` in VS Code - Observe nice syntax highlighting for GraphQL
1 parent 8fbac78 commit e5dd8ad

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

packages/vscode-graphql-syntax/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ matching.
1212
- PHP (example: [test.php](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.php))
1313
- 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))
1414
- Scala (example: [test.scala](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.scala))
15+
- Hack (example: [test.hack](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.hack))
1516

1617
You'll want to install this if you do not use `graphql-config`, or want to use
1718
the highlighting with other extensions than `vscode-graphql`
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"scopeName": "inline.graphql.hack",
3+
"injectionSelector": "L:(meta.embedded.block.hack | source.hack -string -comment)",
4+
"patterns": [
5+
{
6+
"contentName": "meta.embedded.block.graphql",
7+
"begin": "(<<<)\\s*(\"?)(GRAPHQL|GQL)(\\2)(\\s*)$",
8+
"beginCaptures": {
9+
"0": {
10+
"name": "punctuation.section.embedded.begin.php"
11+
},
12+
"1": {
13+
"name": "punctuation.definition.string.php"
14+
},
15+
"3": {
16+
"name": "keyword.operator.heredoc.php"
17+
},
18+
"5": {
19+
"name": "invalid.illegal.trailing-whitespace.php"
20+
}
21+
},
22+
"end": "^\\s*(\\3)(?![A-Za-z0-9_\\x{7f}-\\x{10ffff}])",
23+
"endCaptures": {
24+
"0": {
25+
"name": "punctuation.section.embedded.end.php"
26+
},
27+
"1": {
28+
"name": "keyword.operator.heredoc.php"
29+
}
30+
},
31+
"patterns": [
32+
{
33+
"include": "source.graphql"
34+
}
35+
]
36+
},
37+
{
38+
"begin": "(<<<)\\s*'(GRAPHQL|GQL)'(\\s*)$",
39+
"beginCaptures": {
40+
"0": {
41+
"name": "punctuation.section.embedded.begin.php"
42+
},
43+
"1": {
44+
"name": "punctuation.definition.string.php"
45+
},
46+
"2": {
47+
"name": "keyword.operator.nowdoc.php"
48+
},
49+
"3": {
50+
"name": "invalid.illegal.trailing-whitespace.php"
51+
}
52+
},
53+
"contentName": "source.graphql",
54+
"end": "^\\s*(\\2)(?![A-Za-z0-9_\\x{7f}-\\x{10ffff}])",
55+
"endCaptures": {
56+
"0": {
57+
"name": "punctuation.section.embedded.end.php"
58+
},
59+
"1": {
60+
"name": "keyword.operator.nowdoc.php"
61+
}
62+
},
63+
"name": "meta.embedded.graphql",
64+
"patterns": [
65+
{
66+
"include": "source.graphql"
67+
}
68+
]
69+
},
70+
{
71+
"begin": "(/\\*\\*\\s*(@lang\\s*GraphQL|Graphi?QL|graphql)\\s*\\*/)\\s*(')",
72+
"beginCaptures": {
73+
"1": {
74+
"name": "punctuation.definition.comment.php"
75+
},
76+
"3": {
77+
"name": "punctuation.definition.string.begin.php"
78+
}
79+
},
80+
"contentName": "source.graphql",
81+
"end": "'",
82+
"endCaptures": {
83+
"0": {
84+
"name": "punctuation.definition.string.end.php"
85+
}
86+
},
87+
"name": "meta.embedded.graphql",
88+
"patterns": [
89+
{
90+
"include": "source.graphql"
91+
}
92+
]
93+
},
94+
{
95+
"begin": "((/\\*\\*|//)\\s*(@lang\\s*GraphQL|Graphi?QL|graphql)\\s*(\\*/)?)(\\s*)$",
96+
"beginCaptures": {
97+
"1": {
98+
"name": "punctuation.definition.comment.php"
99+
},
100+
"5": {
101+
"name": "invalid.illegal.trailing-whitespace.php"
102+
}
103+
},
104+
"end": "(?<=')",
105+
"patterns": [
106+
{
107+
"begin": "'",
108+
"beginCaptures": {
109+
"0": {
110+
"name": "punctuation.definition.string.begin.php"
111+
}
112+
},
113+
"contentName": "source.graphql",
114+
"end": "'",
115+
"endCaptures": {
116+
"0": {
117+
"name": "punctuation.definition.string.end.php"
118+
}
119+
},
120+
"name": "meta.embedded.graphql",
121+
"patterns": [
122+
{
123+
"include": "source.graphql"
124+
}
125+
]
126+
}
127+
]
128+
}
129+
]
130+
}

packages/vscode-graphql-syntax/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@
143143
"embeddedLanguages": {
144144
"meta.embedded.block.graphql": "graphql"
145145
}
146+
},
147+
{
148+
"injectTo": [
149+
"source.hack",
150+
"text.html.markdown",
151+
"text.html.derivative"
152+
],
153+
"scopeName": "inline.graphql.hack",
154+
"path": "./grammars/graphql.hack.json",
155+
"embeddedLanguages": {
156+
"meta.embedded.block.graphql": "graphql"
157+
}
146158
}
147159
]
148160
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?hh
2+
3+
$query = <<<GRAPHQL
4+
query {
5+
site {
6+
name
7+
}
8+
}
9+
GRAPHQL;
10+
11+
$query = /** @lang GraphQL */ '
12+
query {
13+
site {
14+
name
15+
}
16+
}
17+
';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`inline.graphql.hack grammar > should tokenize a simple hack file 1`] = `
4+
<?hh |
5+
|
6+
$query = |
7+
<<< | punctuation.section.embedded.begin.php punctuation.definition.string.php
8+
GRAPHQL | punctuation.section.embedded.begin.php keyword.operator.heredoc.php
9+
| meta.embedded.block.graphql
10+
query | meta.embedded.block.graphql keyword.operation.graphql
11+
| meta.embedded.block.graphql meta.selectionset.graphql
12+
{ | meta.embedded.block.graphql meta.selectionset.graphql punctuation.operation.graphql
13+
| meta.embedded.block.graphql meta.selectionset.graphql
14+
site | meta.embedded.block.graphql meta.selectionset.graphql variable.graphql
15+
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
16+
{ | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
17+
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
18+
name | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql variable.graphql
19+
| meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql
20+
} | meta.embedded.block.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
21+
| meta.embedded.block.graphql meta.selectionset.graphql
22+
} | meta.embedded.block.graphql meta.selectionset.graphql punctuation.operation.graphql
23+
GRAPHQL | punctuation.section.embedded.end.php keyword.operator.heredoc.php
24+
; |
25+
|
26+
$query = |
27+
/** @lang GraphQL */ | meta.embedded.graphql punctuation.definition.comment.php
28+
| meta.embedded.graphql
29+
' | meta.embedded.graphql punctuation.definition.string.begin.php
30+
| meta.embedded.graphql source.graphql
31+
query | meta.embedded.graphql source.graphql keyword.operation.graphql
32+
| meta.embedded.graphql source.graphql meta.selectionset.graphql
33+
{ | meta.embedded.graphql source.graphql meta.selectionset.graphql punctuation.operation.graphql
34+
| meta.embedded.graphql source.graphql meta.selectionset.graphql
35+
site | meta.embedded.graphql source.graphql meta.selectionset.graphql variable.graphql
36+
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
37+
{ | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
38+
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
39+
name | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql variable.graphql
40+
| meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql
41+
} | meta.embedded.graphql source.graphql meta.selectionset.graphql meta.selectionset.graphql punctuation.operation.graphql
42+
| meta.embedded.graphql source.graphql meta.selectionset.graphql
43+
} | meta.embedded.graphql source.graphql meta.selectionset.graphql punctuation.operation.graphql
44+
' | meta.embedded.graphql punctuation.definition.string.end.php
45+
; |
46+
|
47+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { tokenizeFile } from './__utilities__/utilities';
2+
3+
describe('inline.graphql.hack grammar', () => {
4+
const scope = 'inline.graphql.hack';
5+
6+
it('should tokenize a simple hack file', async () => {
7+
const result = await tokenizeFile('__fixtures__/test.hack', scope);
8+
expect(result).toMatchSnapshot();
9+
});
10+
});

0 commit comments

Comments
 (0)