|
| 1 | +package org.nixos.idea.format; |
| 2 | + |
| 3 | +import com.intellij.execution.ExecutionException; |
| 4 | +import com.intellij.execution.configurations.GeneralCommandLine; |
| 5 | +import com.intellij.execution.process.CapturingProcessAdapter; |
| 6 | +import com.intellij.execution.process.OSProcessHandler; |
| 7 | +import com.intellij.execution.process.ProcessEvent; |
| 8 | +import com.intellij.formatting.service.AsyncDocumentFormattingService; |
| 9 | +import com.intellij.formatting.service.AsyncFormattingRequest; |
| 10 | +import com.intellij.openapi.util.NlsSafe; |
| 11 | +import com.intellij.psi.PsiFile; |
| 12 | +import com.intellij.util.execution.ParametersListUtil; |
| 13 | +import org.jetbrains.annotations.NonNls; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | +import org.nixos.idea.file.NixFile; |
| 17 | +import org.nixos.idea.lang.NixLanguage; |
| 18 | +import org.nixos.idea.settings.NixExternalFormatterSettings; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.util.EnumSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +public final class NixExternalFormatter extends AsyncDocumentFormattingService { |
| 29 | + |
| 30 | + @Override |
| 31 | + protected @NotNull String getNotificationGroupId() { |
| 32 | + return NixLanguage.NOTIFICATION_GROUP_ID; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected @NotNull @NlsSafe String getName() { |
| 37 | + return "NixIDEA"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public @NotNull Set<Feature> getFeatures() { |
| 42 | + return EnumSet.noneOf(Feature.class); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean canFormat(@NotNull PsiFile psiFile) { |
| 47 | + return psiFile instanceof NixFile; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @Override |
| 52 | + protected @Nullable FormattingTask createFormattingTask(@NotNull AsyncFormattingRequest request) { |
| 53 | + NixExternalFormatterSettings nixSettings = NixExternalFormatterSettings.getInstance(); |
| 54 | + if (!nixSettings.isFormatEnabled()) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + var ioFile = request.getIOFile(); |
| 59 | + if (ioFile == null) return null; |
| 60 | + |
| 61 | + @NonNls |
| 62 | + var command = nixSettings.getFormatCommand(); |
| 63 | + List<String> argv = ParametersListUtil.parse(command, false, true); |
| 64 | + |
| 65 | + var commandLine = new GeneralCommandLine(argv); |
| 66 | + |
| 67 | + try { |
| 68 | + var handler = new OSProcessHandler(commandLine.withCharset(StandardCharsets.UTF_8)); |
| 69 | + OutputStream processInput = handler.getProcessInput(); |
| 70 | + return new FormattingTask() { |
| 71 | + @Override |
| 72 | + public void run() { |
| 73 | + handler.addProcessListener(new CapturingProcessAdapter() { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void processTerminated(@NotNull ProcessEvent event) { |
| 77 | + int exitCode = event.getExitCode(); |
| 78 | + if (exitCode == 0) { |
| 79 | + request.onTextReady(getOutput().getStdout()); |
| 80 | + } else { |
| 81 | + request.onError("NixIDEA", getOutput().getStderr()); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + handler.startNotify(); |
| 86 | + try { |
| 87 | + Files.copy(ioFile.toPath(), processInput); |
| 88 | + processInput.flush(); |
| 89 | + processInput.close(); |
| 90 | + } catch (IOException e) { |
| 91 | + handler.destroyProcess(); |
| 92 | + request.onError("NixIDEA", e.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean cancel() { |
| 98 | + handler.destroyProcess(); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean isRunUnderProgress() { |
| 104 | + return true; |
| 105 | + } |
| 106 | + }; |
| 107 | + } catch (ExecutionException e) { |
| 108 | + request.onError("NixIDEA", e.getMessage()); |
| 109 | + return null; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments