Skip to content

Commit 5611a9b

Browse files
committed
- Query result viewer doesn't always reformat the response JSON (#209)
- Make the Query result viewer work with windows line endings in JSON responses (#191)
1 parent f0a60e2 commit 5611a9b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/main/com/intellij/lang/jsgraphql/ide/project/JSGraphQLLanguageUIProjectService.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.intellij.util.containers.ImmutableList;
8686
import com.intellij.util.messages.MessageBusConnection;
8787
import com.intellij.util.ui.UIUtil;
88+
import org.apache.commons.httpclient.Header;
8889
import org.apache.commons.httpclient.HttpClient;
8990
import org.apache.commons.httpclient.methods.PostMethod;
9091
import org.apache.commons.httpclient.methods.StringRequestEntity;
@@ -464,18 +465,22 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
464465
httpClient.executeMethod(method);
465466
final String responseJson = Optional.fromNullable(method.getResponseBodyAsString()).or("");
466467
sw.stop();
468+
final Header responseHeader = method.getResponseHeader("Content-Type");
469+
final boolean reformatJson = responseHeader != null && "application/json".equals(responseHeader.getValue());
467470
final Integer errorCount = getErrorCount(responseJson);
468471
if (fileEditor instanceof TextEditor) {
469472
final TextEditor textEditor = (TextEditor) fileEditor;
470473
UIUtil.invokeLaterIfNeeded(() -> {
471474
ApplicationManager.getApplication().runWriteAction(() -> {
472475
final Document document = textEditor.getEditor().getDocument();
473-
document.setText(responseJson);
474-
if(requestJson.startsWith("{")) {
475-
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
476-
if (psiFile != null) {
477-
new ReformatCodeProcessor(psiFile, false).run();
478-
}
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
479484
}
480485
});
481486
final StringBuilder queryResultText = new StringBuilder(virtualFile.getName()).

0 commit comments

Comments
 (0)