@@ -8,6 +8,7 @@ import schema from './schema';
8
8
import pypyjs_vm from 'pypyjs' ;
9
9
10
10
import 'codemirror/mode/python/python' ;
11
+ import 'codemirror/addon/lint/lint' ;
11
12
import '../css/playground.styl' ;
12
13
13
14
if ( typeof PUBLIC_PATH === "undefined" ) {
@@ -32,6 +33,23 @@ class Query(graphene.ObjectType):
32
33
schema = graphene.Schema(query=Query)
33
34
` ;
34
35
36
+ CodeMirror . registerHelper ( 'lint' , 'python' , function ( text , options , editor ) {
37
+ return ( options . errors || [ ] ) . map ( ( error ) => {
38
+ var tokens = editor . getLineTokens ( error . line - 1 ) ;
39
+ tokens = tokens . filter ( ( token , pos ) => {
40
+ return ! ! token . type || token . string . trim ( ) . length > 0 ;
41
+ } ) ;
42
+ if ( ! tokens ) return [ ] ;
43
+ return {
44
+ message : `${ error . name } : ${ error . message } ` ,
45
+ severity : 'error' ,
46
+ type : 'syntax' ,
47
+ from : CodeMirror . Pos ( error . line - 1 , tokens [ 0 ] . start ) ,
48
+ to : CodeMirror . Pos ( error . line - 1 , tokens [ tokens . length - 1 ] . end ) ,
49
+ } ;
50
+ } ) ;
51
+ } ) ;
52
+
35
53
// function graphQLFetcher(graphQLParams) {
36
54
// return fetch('http://swapi.graphene-python.org/graphql', {
37
55
// method: 'post',
@@ -113,10 +131,13 @@ __graphene_executor = Executor([TrackResolver()], map_type=OrderedDict)
113
131
value : baseCode ,
114
132
mode : "python" ,
115
133
theme : "graphene" ,
116
- // lineNumbers: true,
134
+ lineNumbers : true ,
117
135
tabSize : 4 ,
118
136
indentUnit : 4 ,
119
- gutters : [ "CodeMirror-linenumbers" , "breakpoints" ]
137
+ gutters : [ "CodeMirror-linenumbers" , "breakpoints" ] ,
138
+ lint : {
139
+ errors : [ ] ,
140
+ } ,
120
141
} ) ;
121
142
this . editor . on ( "change" , this . onEditorChange . bind ( this ) ) ;
122
143
}
@@ -132,6 +153,7 @@ __graphene_executor = Executor([TrackResolver()], map_type=OrderedDict)
132
153
this . createSchema ( this . editor . getValue ( ) ) ;
133
154
}
134
155
createSchema ( code ) {
156
+ if ( this . previousCode == code ) return ;
135
157
console . log ( 'createSchema' ) ;
136
158
this . validSchema = null ;
137
159
this . pypyjs . then ( ( ) => {
@@ -142,12 +164,16 @@ assert schema, 'You have to define a schema'
142
164
` )
143
165
} ) . then ( ( ) => {
144
166
console . log ( 'NO ERRORS' ) ;
167
+ this . removeErrors ( ) ;
145
168
this . validSchema = true ;
146
169
} , ( err ) => {
147
- console . log ( 'ERROR' , err ) ;
170
+ this . editor . options . lint . errors = [ ] ;
171
+ console . log ( 'ERRORS' , err ) ;
172
+ this . logError ( err ) ;
148
173
this . validSchema = false ;
149
174
// this.editor.setGutterMarker(5, "breakpoints", syntaxError());
150
175
} ) . then ( this . updateGraphiQL . bind ( this ) ) ;
176
+ this . previousCode = code ;
151
177
}
152
178
updateGraphiQL ( ) {
153
179
if ( this . validSchema ) {
@@ -157,6 +183,24 @@ assert schema, 'You have to define a schema'
157
183
this . refs . graphiql . refs . docExplorer . forceUpdate ( ) ;
158
184
}
159
185
}
186
+ logError ( error ) {
187
+ var lines = error . trace . split ( '\n' ) ;
188
+ var file_errors = lines . map ( ( errorLine ) => {
189
+ return errorLine . match ( / F i l e " < s t r i n g > " , l i n e ( \d + ) / ) ;
190
+ } ) . filter ( ( x ) => ! ! x ) ;
191
+ if ( ! file_errors . length ) return ;
192
+ var line = parseInt ( file_errors [ file_errors . length - 1 ] [ 1 ] ) ;
193
+ error . line = line - 2 ;
194
+ if ( error . name == "ImportError" && error . message == "No module named django" ) {
195
+ error . message = "Django is not supported yet in Playground editor" ;
196
+ }
197
+ this . editor . options . lint . errors . push ( error ) ;
198
+ CodeMirror . signal ( this . editor , 'change' , this . editor ) ;
199
+ }
200
+ removeErrors ( ) {
201
+ this . editor . options . lint . errors = [ ] ;
202
+ CodeMirror . signal ( this . editor , 'change' , this . editor ) ;
203
+ }
160
204
fetcher ( graphQLParams ) {
161
205
if ( ! this . validSchema ) {
162
206
return graphQLFetcher ( arguments ) ;
0 commit comments