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

[language server/vscode] Add Ruby support #1304

Merged
merged 2 commits into from
Jul 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
<<<<<<< HEAD
- <First `apollo-language-server` related entry goes here>
=======
- Allow template literal placeholders that span multiple rows[#1299](https://github.com/apollographql/apollo-tooling/pull/1299)
- Add support for extracting GraphQL documents from Ruby source files using `<<-GRAPHQL...GRAPHQL` heredoc. [#1304](https://github.com/apollographql/apollo-tooling/pull/1304)
>>>>>>> [language server] Extract GraphQL documents from Ruby source
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
- <First `vscode-apollo` related entry goes here>
- Add support for Ruby source files using `<<-GRAPHQL...GRAPHQL` heredoc. [#1304](https://github.com/apollographql/apollo-tooling/pull/1304)

## `apollo-graphql@0.3.3`

Expand Down
29 changes: 29 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 "ruby":
return extractGraphQLDocumentsFromRubyStrings(document, tagName);
default:
return null;
}
Expand Down Expand Up @@ -130,6 +132,33 @@ function extractGraphQLDocumentsFromPythonStrings(
return documents;
}

function extractGraphQLDocumentsFromRubyStrings(
document: TextDocument,
tagName: string
): GraphQLDocument[] | null {
const text = document.getText();

const documents: GraphQLDocument[] = [];

const regExp = new RegExp(`(<<-${tagName})([\\s\\S]+?)${tagName}`, "gm");

let result;
while ((result = regExp.exec(text)) !== null) {
const contents = replacePlaceholdersWithWhiteSpace(result[2]);
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",
".rb": "ruby"
};

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.ruby"
],
"scopeName": "inline.graphql.ruby",
"path": "./syntaxes/graphql.rb.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",
"ruby"
],
synchronize: {
fileEvents: [
workspace.createFileSystemWatcher("**/.env"),
workspace.createFileSystemWatcher("**/*.{graphql,js,ts,jsx,tsx,vue,py}")
workspace.createFileSystemWatcher(
"**/*.{graphql,js,ts,jsx,tsx,vue,py,rb}"
)
]
},
outputChannel
Expand Down
23 changes: 23 additions & 0 deletions packages/vscode-apollo/syntaxes/graphql.rb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"fileTypes": ["rb"],
"injectionSelector": "L:source -string -comment",
"patterns": [
{
"contentName": "meta.embedded.block.graphql",
"begin": "(?=(?><<[-~](['\"]?)((?:[_\\w]+_|)GRAPHQL)\\b\\1))",
"beginCaptures": {
"0": {
"name": "punctuation.definition.string.begin.ruby"
}
},
"end": "\\s*\\2$\\n?",
"endCaptures": {
"0": {
"name": "punctuation.definition.string.end.ruby"
}
},
"patterns": [{ "include": "source.graphql" }]
}
],
"scopeName": "inline.graphql.ruby"
}