Skip to content

Commit

Permalink
- Query result viewer doesn't always reformat the response JSON (#209)
Browse files Browse the repository at this point in the history
- Make the Query result viewer work with windows line endings in JSON responses (#191)
  • Loading branch information
jimkyndemeyer committed Jan 26, 2019
1 parent f0a60e2 commit 5611a9b
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.intellij.util.containers.ImmutableList;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.UIUtil;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
Expand Down Expand Up @@ -464,18 +465,22 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
httpClient.executeMethod(method);
final String responseJson = Optional.fromNullable(method.getResponseBodyAsString()).or("");
sw.stop();
final Header responseHeader = method.getResponseHeader("Content-Type");
final boolean reformatJson = responseHeader != null && "application/json".equals(responseHeader.getValue());
final Integer errorCount = getErrorCount(responseJson);
if (fileEditor instanceof TextEditor) {
final TextEditor textEditor = (TextEditor) fileEditor;
UIUtil.invokeLaterIfNeeded(() -> {
ApplicationManager.getApplication().runWriteAction(() -> {
final Document document = textEditor.getEditor().getDocument();
document.setText(responseJson);
if(requestJson.startsWith("{")) {
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
if (psiFile != null) {
new ReformatCodeProcessor(psiFile, false).run();
}
document.setText(responseJson.replace("\r\n", "\n"));
if(reformatJson) {
PsiDocumentManager.getInstance(myProject).performForCommittedDocument(document, () -> {
final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
if (psiFile != null) {
new ReformatCodeProcessor(psiFile, false).run();
}
}); // wait for doc to update PSI before reformat
}
});
final StringBuilder queryResultText = new StringBuilder(virtualFile.getName()).
Expand Down

0 comments on commit 5611a9b

Please sign in to comment.