package com.juliacomputing.jldt.eclipse.ui.console;

import org.eclipse.dltk.console.IScriptConsoleIO;
import org.eclipse.dltk.console.IScriptExecResult;
import org.eclipse.dltk.console.IScriptInterpreter;
import org.eclipse.dltk.console.ScriptExecResult;

import java.io.*;
import java.util.List;

public class JuliaConsoleInterpreter implements IScriptInterpreter {

  private static final String EOM = "\"<eom>\"";
  private static final String ENCODING = "UTF8";

  private final Process process;
  private final BufferedWriter writer;
  private final BufferedReader reader;

  public JuliaConsoleInterpreter(final String path) {

    try {
      ProcessBuilder builder = new ProcessBuilder(path);
      builder.redirectErrorStream(true);
      process = builder.start();
      final OutputStream outputStream = process.getOutputStream();
      writer = new BufferedWriter(new OutputStreamWriter(outputStream, ENCODING));
      final InputStream inputStream = process.getInputStream();
      reader = new BufferedReader(new InputStreamReader(inputStream, "UTF8"));
    }
    catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }

  }

  @Override
  public void addInitialListenerOperation(Runnable runnable) {

  }

  @Override
  public InputStream getInitialOutputStream() {
    return process.getInputStream();
  }

  @Override
  public boolean isValid() {
    return true;
  }

  @Override
  public void consoleConnected(IScriptConsoleIO iScriptConsoleIO) {
  }

  @Override
  public IScriptExecResult exec(String command) throws IOException {
    if (command == null || command.isEmpty())
      return null;
    writer.write(command);
    writer.newLine();
    writer.write(EOM);
    writer.newLine();
    writer.flush();
    final StringBuilder response = new StringBuilder();
    String line = reader.readLine();
    while (line != null && !line.contains(EOM)) {
      response.append(line);
      response.append("\n");
      line = reader.readLine();
    }
    if (line != null)
      response.append(line.replace(EOM, ""));
    return new ScriptExecResult(response.toString());
  }

  @Override
  public int getState() {
    return 0;
  }

  @Override
  public List<?> getCompletions(String s, int i) throws IOException {
    return null;
  }

  @Override
  public String getDescription(String s, int i) throws IOException {
    return null;
  }

  @Override
  public String[] getNames(String s) throws IOException {
    return new String[0];
  }

  @Override
  public void close() throws IOException {
    process.destroy();
  }
}

@@ -1580,24 +1580,56 @@
 Exception %7B%0A
+%09  System.out.println(command);%0A
     if (comm
@@ -1734,53 +1734,8 @@
 ();%0A
-    writer.write(EOM);%0A    writer.newLine();%0A
     
@@ -1814,90 +1814,76 @@
     
-String line = reader.readLine();%0A    while (
+while (reader.ready())%7B%0A      final String 
 line 
-!
 = 
-null && !line.contains(EOM)) %7B
+reader.readLine();
 %0A   
@@ -1945,109 +1945,9 @@
     
-  line = reader.readLine();%0A    %7D%0A    if (line != null)%0A      response.append(line.replace(EOM, %22%22));
+%7D
 %0A   

update repl - code block support