From 47d36287ec0608fdd06b96d6bab4e7073258d76f Mon Sep 17 00:00:00 2001 From: Venkat Dinavahi Date: Wed, 10 Jul 2019 14:50:35 +0200 Subject: [PATCH] added dart support to language server and embedded graphql support to dart --- CHANGELOG.md | 4 +- .../apollo-language-server/src/document.ts | 32 +++++++++++++++ .../src/project/base.ts | 3 +- packages/vscode-apollo/package.json | 10 +++++ .../vscode-apollo/src/languageServerClient.ts | 7 +++- .../vscode-apollo/syntaxes/graphql.dart.json | 40 +++++++++++++++++++ 6 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 packages/vscode-apollo/syntaxes/graphql.dart.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ffe097d35..9833cc378b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,11 +21,11 @@ - `apollo-graphql` - - `apollo-language-server` - - + - Add Dart operation extraction [#1385](https://github.com/apollographql/apollo-tooling/pull/1385) - `apollo-tools` - - `vscode-apollo` - - + - Add Dart support for vscode [#1385](https://github.com/apollographql/apollo-tooling/pull/1385) ## `apollo-graphql@0.3.3` diff --git a/packages/apollo-language-server/src/document.ts b/packages/apollo-language-server/src/document.ts index c0e44a881c..e1f9de4f21 100644 --- a/packages/apollo-language-server/src/document.ts +++ b/packages/apollo-language-server/src/document.ts @@ -68,6 +68,8 @@ export function extractGraphQLDocuments( return extractGraphQLDocumentsFromJSTemplateLiterals(document, tagName); case "python": return extractGraphQLDocumentsFromPythonStrings(document, tagName); + case "dart": + return extractGraphQLDocumentsFromDartStrings(document, tagName); default: return null; } @@ -130,6 +132,36 @@ function extractGraphQLDocumentsFromPythonStrings( return documents; } +function extractGraphQLDocumentsFromDartStrings( + document: TextDocument, + tagName: string +): GraphQLDocument[] | null { + const text = document.getText(); + + const documents: GraphQLDocument[] = []; + + const regExp = new RegExp( + `\\b(${tagName}\\(\\s*r?("""|'''))([\\s\\S]+?)\\2\\s*\\)`, + "gm" + ); + + let result; + while ((result = regExp.exec(text)) !== null) { + const contents = replacePlaceholdersWithWhiteSpace(result[3]); + const position = document.positionAt(result.index + result[1].length); + const locationOffset: SourceLocation = { + line: position.line + 1, + column: position.character + 1 + }; + const source = new Source(contents, document.uri, locationOffset); + documents.push(new GraphQLDocument(source)); + } + + if (documents.length < 1) return null; + + return documents; +} + function replacePlaceholdersWithWhiteSpace(content: string) { return content.replace(/\$\{([\s\S]+?)\}/gm, match => { return Array(match.length).join(" "); diff --git a/packages/apollo-language-server/src/project/base.ts b/packages/apollo-language-server/src/project/base.ts index ef96b74970..179b8aeca9 100644 --- a/packages/apollo-language-server/src/project/base.ts +++ b/packages/apollo-language-server/src/project/base.ts @@ -40,7 +40,8 @@ const fileAssociations: { [extension: string]: string } = { ".jsx": "javascriptreact", ".tsx": "typescriptreact", ".vue": "vue", - ".py": "python" + ".py": "python", + ".dart": "dart" }; export interface GraphQLProjectConfig { diff --git a/packages/vscode-apollo/package.json b/packages/vscode-apollo/package.json index 129c95f125..cf35b421be 100644 --- a/packages/vscode-apollo/package.json +++ b/packages/vscode-apollo/package.json @@ -100,6 +100,16 @@ "embeddedLanguages": { "meta.embedded.block.graphql": "graphql" } + }, + { + "injectTo": [ + "source.dart" + ], + "scopeName": "inline.dart.python", + "path": "./syntaxes/graphql.dart.json", + "embeddedLanguages": { + "meta.embedded.block.graphql": "graphql" + } } ], "commands": [ diff --git a/packages/vscode-apollo/src/languageServerClient.ts b/packages/vscode-apollo/src/languageServerClient.ts index fc6a83cabd..e0809affe6 100644 --- a/packages/vscode-apollo/src/languageServerClient.ts +++ b/packages/vscode-apollo/src/languageServerClient.ts @@ -47,12 +47,15 @@ export function getLanguageServerClient( "javascriptreact", "typescriptreact", "vue", - "python" + "python", + "dart" ], synchronize: { fileEvents: [ workspace.createFileSystemWatcher("**/.env"), - workspace.createFileSystemWatcher("**/*.{graphql,js,ts,jsx,tsx,vue,py}") + workspace.createFileSystemWatcher( + "**/*.{graphql,js,ts,jsx,tsx,vue,py,dart}" + ) ] }, outputChannel diff --git a/packages/vscode-apollo/syntaxes/graphql.dart.json b/packages/vscode-apollo/syntaxes/graphql.dart.json new file mode 100644 index 0000000000..21633853a7 --- /dev/null +++ b/packages/vscode-apollo/syntaxes/graphql.dart.json @@ -0,0 +1,40 @@ +{ + "fileTypes": [ + "dart" + ], + "injectionSelector": "L:source -string -comment", + "patterns": [ + { + "name": "meta.function-call.dart", + "begin": "\\b(gql)\\s*(\\()", + "beginCaptures": { + "1": { + "name": "entity.name.function.dart" + }, + "2": { + "name": "punctuation.definition.arguments.begin.dart" + } + }, + "end": "(\\))", + "endCaptures": { + "1": { + "name": "punctuation.definition.arguments.end.dart" + } + }, + "patterns": [ + { + "name": "taggedTemplates", + "contentName": "meta.embedded.block.graphql", + "begin": "r?(\"\"\"|''')", + "end": "((\\1))", + "patterns": [ + { + "include": "source.graphql" + } + ] + } + ] + } + ], + "scopeName": "inline.graphql.dart" +}