Posted By

guai on 08/26/11


Tagged

groovy jedit


Versions (?)

jEdit macro that makes it just like GroovyConsole


 / Published in: Java
 

bind it to ctrl+enter and everything that GroovyConsole do jEdit will do too. Uses groovy installation.

  1. buffer.setMode("groovy");
  2. buffers = jEdit.getBuffers();
  3. for(i = 0; i < buffers.length; i++) {
  4. gsh = buffers[i].getProperty("GroovyShell");
  5. if(gsh != null) { break; }
  6. }
  7. if(gsh == null) {
  8. libfiles = new File(System.getenv("GROOVY_HOME") + "/lib/").listFiles();
  9. cl = this.getClass().getClassLoader();
  10. for(i = 0; i < libfiles.length; i++)
  11. cl.addURL(new URL("jar:" + libfiles[i].toURL().toString() + "!/"));
  12. gsh = new groovy.lang.GroovyShell(cl);
  13. }
  14. for(i = 0; i < buffers.length; i++)
  15. buffers[i].setProperty("GroovyShell", gsh);
  16. outbuffer = buffer.getProperty("outbuffer");
  17. if((outbuffer == null) || outbuffer.isClosed()) {
  18. outbuffer = jEdit.newFile(view);
  19. buffer.setProperty("outbuffer", outbuffer);
  20. }
  21. out = new java.io.StringWriter();
  22. gsh.setProperty("out", out);
  23. String text() {
  24. view.showBuffer(buffer);
  25. res = textArea.getSelectedText();
  26. if(res == null || res.length() == 0) { res = buffer.getText(); }
  27. return res;
  28. }
  29. String cwd = System.getProperty("user.dir");
  30. System.setProperty("user.dir", buffer.getDirectory());
  31. result = gsh.evaluate(text());
  32. System.setProperty("user.dir", cwd);
  33. outbuffer.remove(0, outbuffer.getLength());
  34. outbuffer.insert(0, out.toString());
  35. outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
  36. if(result != null) {
  37. view.showBuffer(outbuffer);
  38. }

Report this snippet  

You need to login to post a comment.