Skip to content

Commit

Permalink
added dart support to language server and embedded graphql support to…
Browse files Browse the repository at this point in the history
… dart
  • Loading branch information
venkatd committed Jul 10, 2019
1 parent feed206 commit 47d3628
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- Add Dart operation extraction [#1385](https://github.com/apollographql/apollo-tooling/pull/1385)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
- <First `vscode-apollo` related entry goes here>
- Add Dart support for vscode [#1385](https://github.com/apollographql/apollo-tooling/pull/1385)

## `[email protected]`

Expand Down
32 changes: 32 additions & 0 deletions packages/apollo-language-server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(" ");
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-language-server/src/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const fileAssociations: { [extension: string]: string } = {
".jsx": "javascriptreact",
".tsx": "typescriptreact",
".vue": "vue",
".py": "python"
".py": "python",
".dart": "dart"
};

export interface GraphQLProjectConfig {
Expand Down
10 changes: 10 additions & 0 deletions packages/vscode-apollo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
7 changes: 5 additions & 2 deletions packages/vscode-apollo/src/languageServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions packages/vscode-apollo/syntaxes/graphql.dart.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 47d3628

Please sign in to comment.