Skip to content

Commit

Permalink
add support for elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
rkolpakov authored and venkatd committed May 28, 2020
1 parent 4139557 commit 125d4b5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- Add Elixir support for vscode [#1969](https://github.com/apollographql/apollo-tooling/pull/1969)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
- <First `vscode-apollo` related entry goes here>
- Add Elixir support for vscode [#1969](https://github.com/apollographql/apollo-tooling/pull/1969)

## `apollo@2.27.3`

Expand Down
31 changes: 31 additions & 0 deletions packages/apollo-language-server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export function extractGraphQLDocuments(
return extractGraphQLDocumentsFromDartStrings(document, tagName);
case "reason":
return extractGraphQLDocumentsFromReasonStrings(document, tagName);
case "elixir":
return extractGraphQLDocumentsFromElixirStrings(document, tagName);
default:
return null;
}
Expand Down Expand Up @@ -228,6 +230,35 @@ function extractGraphQLDocumentsFromReasonStrings(
return documents;
}

function extractGraphQLDocumentsFromElixirStrings(
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
4 changes: 3 additions & 1 deletion packages/apollo-language-server/src/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const fileAssociations: { [extension: string]: string } = {
".py": "python",
".rb": "ruby",
".dart": "dart",
".re": "reason"
".re": "reason",
".ex": "elixir",
".exs": "elixir"
};

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 @@ -133,6 +133,16 @@
"embeddedLanguages": {
"meta.embedded.block.graphql": "graphql"
}
},
{
"injectTo": [
"source.elixir"
],
"scopeName": "inline.graphql.elixir",
"path": "./syntaxes/graphql.ex.json",
"embeddedLanguages": {
"meta.embedded.block.graphql": "graphql"
}
}
],
"commands": [
Expand Down
5 changes: 3 additions & 2 deletions packages/vscode-apollo/src/languageServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ export function getLanguageServerClient(
"python",
"ruby",
"dart",
"reason"
"reason",
"elixir"
],
synchronize: {
fileEvents: [
workspace.createFileSystemWatcher("**/.env?(.local)"),
workspace.createFileSystemWatcher(
"**/*.{graphql,js,ts,jsx,tsx,vue,py,rb,dart,re}"
"**/*.{graphql,js,ts,jsx,tsx,vue,py,rb,dart,re,ex,exs}"
)
]
},
Expand Down
40 changes: 40 additions & 0 deletions packages/vscode-apollo/syntaxes/graphql.ex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"fileTypes": [
"exs"
],
"injectionSelector": "L:source -string -comment",
"patterns": [
{
"name": "meta.function-call.elixir",
"begin": "\\b(gql)(\\()",
"beginCaptures": {
"1": {
"name": "entity.name.function.elixir"
},
"2": {
"name": "punctuation.definition.arguments.begin.elixir"
}
},
"end": "(\\))",
"endCaptures": {
"1": {
"name": "punctuation.definition.arguments.end.elixir"
}
},
"patterns": [
{
"name": "taggedTemplates",
"contentName": "meta.embedded.block.graphql",
"begin": "r?(\"\"\")",
"end": "((\\1))",
"patterns": [
{
"include": "source.graphql"
}
]
}
]
}
],
"scopeName": "inline.graphql.elixir"
}

0 comments on commit 125d4b5

Please sign in to comment.