Skip to content

Commit 9d75522

Browse files
committed
Add Python document extractor to language server
Python does not have tagged templates but it's safe to assume that the user will use a function named `gql()` to wrap document literals in code.
1 parent f5c5769 commit 9d75522

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

packages/apollo-language-server/src/document.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,22 @@ export function extractGraphQLDocuments(
6363
case "javascriptreact":
6464
case "typescript":
6565
case "typescriptreact":
66-
return extractGraphQLDocumentsFromTemplateLiterals(document);
66+
return extractGraphQLDocumentsFromJSTemplateLiterals(document);
67+
case "python":
68+
return extractGraphQLDocumentsFromPythonStrings(document);
6769
default:
6870
return null;
6971
}
7072
}
7173

72-
function extractGraphQLDocumentsFromTemplateLiterals(
74+
function extractGraphQLDocumentsFromJSTemplateLiterals(
7375
document: TextDocument
7476
): GraphQLDocument[] | null {
7577
const text = document.getText();
7678

7779
const documents: GraphQLDocument[] = [];
7880

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

8183
let result;
8284
while ((result = regExp.exec(text)) !== null) {
@@ -95,6 +97,38 @@ function extractGraphQLDocumentsFromTemplateLiterals(
9597
return documents;
9698
}
9799

100+
function extractGraphQLDocumentsFromPythonStrings(
101+
document: TextDocument
102+
): GraphQLDocument[] | null {
103+
const text = document.getText();
104+
105+
const documents: GraphQLDocument[] = [];
106+
107+
const regExp = /\b(gql\s*\(\s*[bfru]*("(?:"")?|'(?:'')?))([\s\S]+?)\2\s*\)/gm;
108+
109+
let result;
110+
while ((result = regExp.exec(text)) !== null) {
111+
const contents = replacePlaceholdersWithWhiteSpace(result[3]);
112+
const position = document.positionAt(result.index + result[1].length);
113+
const locationOffset: SourceLocation = {
114+
line: position.line + 1,
115+
column: position.character + 1
116+
};
117+
console.log(
118+
result.index,
119+
result.index + result[1].length,
120+
position,
121+
locationOffset
122+
);
123+
const source = new Source(contents, document.uri, locationOffset);
124+
documents.push(new GraphQLDocument(source));
125+
}
126+
127+
if (documents.length < 1) return null;
128+
129+
return documents;
130+
}
131+
98132
function replacePlaceholdersWithWhiteSpace(content: string) {
99133
return content.replace(/\$\{(.+)?\}/g, match => {
100134
return Array(match.length).join(" ");

packages/apollo-language-server/src/project/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const fileAssociations: { [extension: string]: string } = {
3737
".js": "javascript",
3838
".ts": "typescript",
3939
".jsx": "javascriptreact",
40-
".tsx": "typescriptreact"
40+
".tsx": "typescriptreact",
41+
".py": "python"
4142
};
4243

4344
export interface GraphQLProjectConfig {

0 commit comments

Comments
 (0)