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

New Feature: Add Python document extractor to the language server #693

Merged
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
34 changes: 31 additions & 3 deletions packages/apollo-language-server/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,22 @@ export function extractGraphQLDocuments(
case "javascriptreact":
case "typescript":
case "typescriptreact":
return extractGraphQLDocumentsFromTemplateLiterals(document);
return extractGraphQLDocumentsFromJSTemplateLiterals(document);
case "python":
return extractGraphQLDocumentsFromPythonStrings(document);
default:
return null;
}
}

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

const documents: GraphQLDocument[] = [];

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

let result;
while ((result = regExp.exec(text)) !== null) {
Expand All @@ -95,6 +97,32 @@ function extractGraphQLDocumentsFromTemplateLiterals(
return documents;
}

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

const documents: GraphQLDocument[] = [];

const regExp = /\b(gql\s*\(\s*[bfru]*("(?:"")?|'(?:'')?))([\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(/\$\{(.+)?\}/g, 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 @@ -37,7 +37,8 @@ const fileAssociations: { [extension: string]: string } = {
".js": "javascript",
".ts": "typescript",
".jsx": "javascriptreact",
".tsx": "typescriptreact"
".tsx": "typescriptreact",
".py": "python"
};

export interface GraphQLProjectConfig {
Expand Down