Skip to content

Commit 42d32e0

Browse files
venkatdessaji
authored andcommitted
Dart support for vscode-apollo and language-server (apollographql#1385)
1 parent abfae8a commit 42d32e0

6 files changed

Lines changed: 89 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
- `apollo-graphql`
2020
- <First `apollo-graphql` related entry goes here>
2121
- `apollo-language-server`
22-
- <First `apollo-language-server` related entry goes here>
22+
- Add Dart operation extraction [#1385](https://github.com/apollographql/apollo-tooling/pull/1385)
2323
- `apollo-tools`
2424
- <First `apollo-tools` related entry goes here>
2525
- `vscode-apollo`
26-
- <First `vscode-apollo` related entry goes here>
26+
- Add Dart support for vscode [#1385](https://github.com/apollographql/apollo-tooling/pull/1385)
2727

2828
## `apollo@2.16.1`, `apollo-language-server@1.13.1`, `vscode-apollo@1.8.1`
2929

packages/apollo-language-server/src/document.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function extractGraphQLDocuments(
7070
return extractGraphQLDocumentsFromPythonStrings(document, tagName);
7171
case "ruby":
7272
return extractGraphQLDocumentsFromRubyStrings(document, tagName);
73+
case "dart":
74+
return extractGraphQLDocumentsFromDartStrings(document, tagName);
7375
default:
7476
return null;
7577
}
@@ -159,6 +161,36 @@ function extractGraphQLDocumentsFromRubyStrings(
159161
return documents;
160162
}
161163

164+
function extractGraphQLDocumentsFromDartStrings(
165+
document: TextDocument,
166+
tagName: string
167+
): GraphQLDocument[] | null {
168+
const text = document.getText();
169+
170+
const documents: GraphQLDocument[] = [];
171+
172+
const regExp = new RegExp(
173+
`\\b(${tagName}\\(\\s*r?("""|'''))([\\s\\S]+?)\\2\\s*\\)`,
174+
"gm"
175+
);
176+
177+
let result;
178+
while ((result = regExp.exec(text)) !== null) {
179+
const contents = replacePlaceholdersWithWhiteSpace(result[3]);
180+
const position = document.positionAt(result.index + result[1].length);
181+
const locationOffset: SourceLocation = {
182+
line: position.line + 1,
183+
column: position.character + 1
184+
};
185+
const source = new Source(contents, document.uri, locationOffset);
186+
documents.push(new GraphQLDocument(source));
187+
}
188+
189+
if (documents.length < 1) return null;
190+
191+
return documents;
192+
}
193+
162194
function replacePlaceholdersWithWhiteSpace(content: string) {
163195
return content.replace(/\$\{([\s\S]+?)\}/gm, match => {
164196
return Array(match.length).join(" ");

packages/apollo-language-server/src/project/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const fileAssociations: { [extension: string]: string } = {
4141
".tsx": "typescriptreact",
4242
".vue": "vue",
4343
".py": "python",
44-
".rb": "ruby"
44+
".rb": "ruby",
45+
".dart": "dart"
4546
};
4647

4748
export interface GraphQLProjectConfig {

packages/vscode-apollo/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@
110110
"embeddedLanguages": {
111111
"meta.embedded.block.graphql": "graphql"
112112
}
113+
},
114+
{
115+
"injectTo": [
116+
"source.dart"
117+
],
118+
"scopeName": "inline.graphql.dart",
119+
"path": "./syntaxes/graphql.dart.json",
120+
"embeddedLanguages": {
121+
"meta.embedded.block.graphql": "graphql"
122+
}
113123
}
114124
],
115125
"commands": [

packages/vscode-apollo/src/languageServerClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ export function getLanguageServerClient(
4848
"typescriptreact",
4949
"vue",
5050
"python",
51-
"ruby"
51+
"ruby",
52+
"dart"
5253
],
5354
synchronize: {
5455
fileEvents: [
5556
workspace.createFileSystemWatcher("**/.env"),
5657
workspace.createFileSystemWatcher(
57-
"**/*.{graphql,js,ts,jsx,tsx,vue,py,rb}"
58+
"**/*.{graphql,js,ts,jsx,tsx,vue,py,rb,dart}"
5859
)
5960
]
6061
},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"fileTypes": [
3+
"dart"
4+
],
5+
"injectionSelector": "L:source -string -comment",
6+
"patterns": [
7+
{
8+
"name": "meta.function-call.dart",
9+
"begin": "\\b(gql)(\\()",
10+
"beginCaptures": {
11+
"1": {
12+
"name": "entity.name.function.dart"
13+
},
14+
"2": {
15+
"name": "punctuation.definition.arguments.begin.dart"
16+
}
17+
},
18+
"end": "(\\))",
19+
"endCaptures": {
20+
"1": {
21+
"name": "punctuation.definition.arguments.end.dart"
22+
}
23+
},
24+
"patterns": [
25+
{
26+
"name": "taggedTemplates",
27+
"contentName": "meta.embedded.block.graphql",
28+
"begin": "r?(\"\"\"|''')",
29+
"end": "((\\1))",
30+
"patterns": [
31+
{
32+
"include": "source.graphql"
33+
}
34+
]
35+
}
36+
]
37+
}
38+
],
39+
"scopeName": "inline.graphql.dart"
40+
}

0 commit comments

Comments
 (0)