@@ -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 = / g q l \ 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 ( g q l \s * \( \s * [ b f r u ] * ( " (?: " " ) ? | ' (?: ' ' ) ? ) ) ( [ \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+
98132function replacePlaceholdersWithWhiteSpace ( content : string ) {
99133 return content . replace ( / \$ \{ ( .+ ) ? \} / g, match => {
100134 return Array ( match . length ) . join ( " " ) ;
0 commit comments