72
72
import com .intellij .util .containers .ImmutableList ;
73
73
import com .intellij .util .messages .MessageBusConnection ;
74
74
import com .intellij .util .ui .UIUtil ;
75
+ import org .apache .commons .httpclient .Header ;
75
76
import org .apache .commons .httpclient .HttpClient ;
76
77
import org .apache .commons .httpclient .methods .PostMethod ;
77
78
import org .apache .commons .httpclient .methods .StringRequestEntity ;
@@ -384,7 +385,9 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
384
385
return ;
385
386
}
386
387
final String requestJson = "{\" query\" :\" " + StringEscapeUtils .escapeJavaScript (context .query ) + "\" , \" variables\" :" + variables + "}" ;
387
- final HttpClient httpClient = new HttpClient (new HttpClientParams ());
388
+ final HttpClientParams params = new HttpClientParams ();
389
+ params .setContentCharset ("UTF-8" ); // set fallback charset to align with JSON spec
390
+ final HttpClient httpClient = new HttpClient (params );
388
391
final String url = endpoint .getUrl ();
389
392
try {
390
393
final PostMethod method = new PostMethod (url );
@@ -399,11 +402,13 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
399
402
httpClient .executeMethod (method );
400
403
final String responseJson = Optional .fromNullable (method .getResponseBodyAsString ()).or ("" );
401
404
sw .stop ();
405
+ final Header responseHeader = method .getResponseHeader ("Content-Type" );
406
+ final boolean reformatJson = responseHeader != null && "application/json" .equals (responseHeader .getValue ());
402
407
final Integer errorCount = getErrorCount (responseJson );
403
408
if (fileEditor instanceof TextEditor ) {
404
409
final TextEditor textEditor = (TextEditor ) fileEditor ;
405
410
UIUtil .invokeLaterIfNeeded (() -> {
406
- updateQueryResultEditor (responseJson , textEditor );
411
+ updateQueryResultEditor (responseJson , textEditor , reformatJson );
407
412
final StringBuilder queryResultText = new StringBuilder (virtualFile .getName ()).
408
413
append (": " ).
409
414
append (sw .getTime ()).
@@ -455,7 +460,7 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
455
460
public void showQueryResult (String jsonResponse ) {
456
461
if (fileEditor instanceof TextEditor ) {
457
462
final TextEditor fileEditor = (TextEditor ) this .fileEditor ;
458
- updateQueryResultEditor (jsonResponse , fileEditor );
463
+ updateQueryResultEditor (jsonResponse , fileEditor , true );
459
464
showQueryResultEditor (fileEditor );
460
465
}
461
466
}
@@ -465,15 +470,17 @@ private void showQueryResultEditor(TextEditor textEditor) {
465
470
textEditor .getEditor ().getScrollingModel ().scrollVertically (0 );
466
471
}
467
472
468
- private void updateQueryResultEditor (String responseJson , TextEditor textEditor ) {
473
+ private void updateQueryResultEditor (String responseJson , TextEditor textEditor , boolean reformatJson ) {
469
474
ApplicationManager .getApplication ().runWriteAction (() -> {
470
475
final Document document = textEditor .getEditor ().getDocument ();
471
- document .setText (responseJson );
472
- if (responseJson .startsWith ("{" )) {
473
- final PsiFile psiFile = PsiDocumentManager .getInstance (myProject ).getPsiFile (document );
474
- if (psiFile != null ) {
475
- new ReformatCodeProcessor (psiFile , false ).run ();
476
- }
476
+ document .setText (responseJson .replace ("\r \n " , "\n " ));
477
+ if (reformatJson ) {
478
+ PsiDocumentManager .getInstance (myProject ).performForCommittedDocument (document , () -> {
479
+ final PsiFile psiFile = PsiDocumentManager .getInstance (myProject ).getPsiFile (document );
480
+ if (psiFile != null ) {
481
+ new ReformatCodeProcessor (psiFile , false ).run ();
482
+ }
483
+ }); // wait for doc to update PSI before reformat
477
484
}
478
485
});
479
486
}
0 commit comments