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

[vscode] Use tagName from config to extract documents. #769

Merged
merged 2 commits into from
Nov 30, 2018
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
10 changes: 9 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/packages/vscode-apollo/lib/**/*.js"]
}
},
{
"name": "Attach to TS Server",
"type": "node",
"request": "attach",
"protocol": "inspector",
"port": 6009,
"sourceMaps": true,
}
]
}
20 changes: 13 additions & 7 deletions packages/apollo-language-server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class GraphQLDocument {
}

export function extractGraphQLDocuments(
document: TextDocument
document: TextDocument,
tagName: string = "gql"
): GraphQLDocument[] | null {
switch (document.languageId) {
case "graphql":
Expand All @@ -63,22 +64,23 @@ export function extractGraphQLDocuments(
case "javascriptreact":
case "typescript":
case "typescriptreact":
return extractGraphQLDocumentsFromJSTemplateLiterals(document);
return extractGraphQLDocumentsFromJSTemplateLiterals(document, tagName);
case "python":
return extractGraphQLDocumentsFromPythonStrings(document);
return extractGraphQLDocumentsFromPythonStrings(document, tagName);
default:
return null;
}
}

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

const documents: GraphQLDocument[] = [];

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

let result;
while ((result = regExp.exec(text)) !== null) {
Expand All @@ -98,13 +100,17 @@ function extractGraphQLDocumentsFromJSTemplateLiterals(
}

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

const documents: GraphQLDocument[] = [];

const regExp = /\b(gql\s*\(\s*[bfru]*("(?:"")?|'(?:'')?))([\s\S]+?)\2\s*\)/gm;
const regExp = new RegExp(
`\\b(${tagName}\\s*\\(\\s*[bfru]*("(?:"")?|'(?:'')?))([\\s\\S]+?)\\2\\s*\\)`,
"gm"
);

let result;
while ((result = regExp.exec(text)) !== null) {
Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-language-server/src/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export abstract class GraphQLProject implements GraphQLSchemaProvider {
}

documentDidChange(document: TextDocument) {
const documents = extractGraphQLDocuments(document);
const documents = extractGraphQLDocuments(
document,
this.config.client && this.config.client.tagName
);
if (documents) {
this.documentsByFile.set(document.uri, documents);
this.invalidate();
Expand Down