7272import com .intellij .util .containers .ImmutableList ;
7373import com .intellij .util .messages .MessageBusConnection ;
7474import com .intellij .util .ui .UIUtil ;
75+ import org .apache .commons .httpclient .Header ;
7576import org .apache .commons .httpclient .HttpClient ;
7677import org .apache .commons .httpclient .methods .PostMethod ;
7778import org .apache .commons .httpclient .methods .StringRequestEntity ;
@@ -384,7 +385,9 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
384385 return ;
385386 }
386387 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 );
388391 final String url = endpoint .getUrl ();
389392 try {
390393 final PostMethod method = new PostMethod (url );
@@ -399,11 +402,13 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
399402 httpClient .executeMethod (method );
400403 final String responseJson = Optional .fromNullable (method .getResponseBodyAsString ()).or ("" );
401404 sw .stop ();
405+ final Header responseHeader = method .getResponseHeader ("Content-Type" );
406+ final boolean reformatJson = responseHeader != null && "application/json" .equals (responseHeader .getValue ());
402407 final Integer errorCount = getErrorCount (responseJson );
403408 if (fileEditor instanceof TextEditor ) {
404409 final TextEditor textEditor = (TextEditor ) fileEditor ;
405410 UIUtil .invokeLaterIfNeeded (() -> {
406- updateQueryResultEditor (responseJson , textEditor );
411+ updateQueryResultEditor (responseJson , textEditor , reformatJson );
407412 final StringBuilder queryResultText = new StringBuilder (virtualFile .getName ()).
408413 append (": " ).
409414 append (sw .getTime ()).
@@ -455,7 +460,7 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
455460 public void showQueryResult (String jsonResponse ) {
456461 if (fileEditor instanceof TextEditor ) {
457462 final TextEditor fileEditor = (TextEditor ) this .fileEditor ;
458- updateQueryResultEditor (jsonResponse , fileEditor );
463+ updateQueryResultEditor (jsonResponse , fileEditor , true );
459464 showQueryResultEditor (fileEditor );
460465 }
461466 }
@@ -465,15 +470,17 @@ private void showQueryResultEditor(TextEditor textEditor) {
465470 textEditor .getEditor ().getScrollingModel ().scrollVertically (0 );
466471 }
467472
468- private void updateQueryResultEditor (String responseJson , TextEditor textEditor ) {
473+ private void updateQueryResultEditor (String responseJson , TextEditor textEditor , boolean reformatJson ) {
469474 ApplicationManager .getApplication ().runWriteAction (() -> {
470475 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
477484 }
478485 });
479486 }
0 commit comments