Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vscode support for Elixir #1971

Merged
merged 1 commit into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
## Upcoming

- `apollo`
- Fix CLI crashing when trying to open a directory that is named like a file [#1948](https://github.com/apollographql/apollo-tooling/pull/1948).
- <First `apollo` related entry goes here>
- `apollo-codegen-flow`
- <First `apollo-codegen-flow` related entry goes here>
- `apollo-codegen-scala`
- <First `apollo-codegen-scala` related entry goes here>
- `apollo-codegen-swift`
- Change selection sets to computed vars to remove unnecessary memory use [#1950](https://github.com/apollographql/apollo-tooling/pull/1950)
- <First `apollo-codegen-swift` related entry goes here>
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- `apollo-codegen-core`
Expand All @@ -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 [#1971](https://github.com/apollographql/apollo-tooling/pull/1971)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
- <First `vscode-apollo` related entry goes here>
- Add Elixir support for vscode [#1971](https://github.com/apollographql/apollo-tooling/pull/1971)

## `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
41 changes: 41 additions & 0 deletions packages/vscode-apollo/syntaxes/graphql.ex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"fileTypes": [
"ex",
"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"
}