Search in sources :

Example 1 with BaseProcessHandler

use of com.intellij.execution.process.BaseProcessHandler in project Perl5-IDEA by Camelcade.

the class PerlFormatWithPerlTidyAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    if (isEnabled(event)) {
        final PsiFile file = PerlActionUtil.getPsiFileFromEvent(event);
        if (file == null) {
            return;
        }
        final Project project = file.getProject();
        final Document document = file.getViewProvider().getDocument();
        if (document == null) {
            return;
        }
        final VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile == null) {
            return;
        }
        FileDocumentManager.getInstance().saveDocument(document);
        new Task.Backgroundable(project, PerlBundle.message("perl.tidy.formatting"), false) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                indicator.setIndeterminate(true);
                try {
                    PerlCommandLine perlTidyCommandLine = getPerlTidyCommandLine(project);
                    if (perlTidyCommandLine == null) {
                        return;
                    }
                    BaseProcessHandler<?> processHandler = PerlHostData.createProcessHandler(perlTidyCommandLine.withCharset(virtualFile.getCharset()));
                    final OutputStream outputStream = Objects.requireNonNull(processHandler.getProcessInput());
                    try {
                        final byte[] sourceBytes = virtualFile.contentsToByteArray();
                        outputStream.write(sourceBytes);
                        outputStream.close();
                    } catch (IOException e) {
                        LOG.error(e);
                        Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), e.getMessage(), NotificationType.ERROR));
                        return;
                    }
                    ProcessOutput processOutput = PerlHostData.getOutput(processHandler);
                    final List<String> stdoutLines = processOutput.getStdoutLines(false);
                    List<String> stderrLines = processOutput.getStderrLines();
                    if (stderrLines.isEmpty()) {
                        WriteCommandAction.runWriteCommandAction(project, () -> {
                            document.setText(StringUtil.join(stdoutLines, "\n"));
                            PsiDocumentManager.getInstance(project).commitDocument(document);
                        });
                    } else {
                        LOG.warn("Non-empty stderr: " + processOutput.getStderr());
                        Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.formatting.error.title"), StringUtil.join(stderrLines, "<br>"), NotificationType.ERROR));
                    }
                } catch (ExecutionException e) {
                    LOG.error(e);
                    Notifications.Bus.notify(new Notification(getGroup(), PerlBundle.message("perl.action.perl.tidy.running.error.title"), e.getMessage(), NotificationType.ERROR));
                }
            }
        }.queue();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) Notification(com.intellij.notification.Notification) Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessOutput(com.intellij.execution.process.ProcessOutput) PerlCommandLine(com.perl5.lang.perl.idea.execution.PerlCommandLine) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) ExecutionException(com.intellij.execution.ExecutionException) BaseProcessHandler(com.intellij.execution.process.BaseProcessHandler)

Example 2 with BaseProcessHandler

use of com.intellij.execution.process.BaseProcessHandler in project consulo by consulo.

the class EnvironmentAwareHost method getProcessOutput.

/**
 * @param commandLine commandLine to execute on this host
 * @return output of the corresponding process
 */
@Nonnull
public ProcessOutput getProcessOutput(@Nonnull GeneralCommandLine commandLine) throws ExecutionException {
    BaseProcessHandler handler = getProcessHandler(commandLine);
    CapturingProcessRunner runner = new CapturingProcessRunner(handler);
    return runner.runProcess();
}
Also used : BaseProcessHandler(com.intellij.execution.process.BaseProcessHandler) CapturingProcessRunner(com.intellij.execution.process.CapturingProcessRunner) Nonnull(javax.annotation.Nonnull)

Aggregations

BaseProcessHandler (com.intellij.execution.process.BaseProcessHandler)2 ExecutionException (com.intellij.execution.ExecutionException)1 CapturingProcessRunner (com.intellij.execution.process.CapturingProcessRunner)1 ProcessOutput (com.intellij.execution.process.ProcessOutput)1 Notification (com.intellij.notification.Notification)1 Document (com.intellij.openapi.editor.Document)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 PerlCommandLine (com.perl5.lang.perl.idea.execution.PerlCommandLine)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1