Posted By

guai on 09/22/11


Tagged

jruby jedit


Versions (?)

jEdit macro integrating jRuby


 / Published in: Java
 

Almost the same as my snippet for groovy, but for jruby

  1. buffer.setMode("ruby");
  2. buffers = jEdit.getBuffers();
  3. for(i = 0; i < buffers.length; i++) {
  4. jrc = buffers[i].getProperty("JRubyContainer");
  5. if(jrc != null) { break; }
  6. }
  7. if(jrc == null) {
  8. jarfilenamefilter = new java.io.FilenameFilter() {
  9. boolean accept(File dir, String name) {
  10. return name.endsWith(".jar");
  11. }
  12. };
  13. libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
  14. cl = this.getClass().getClassLoader();
  15. for(i = 0; i < libfiles.length; i++)
  16. cl.addURL(new URL("jar:" + libfiles[i].toURL().toString() + "!/"));
  17. jrc = new org.jruby.embed.ScriptingContainer();
  18. jrc.setClassLoader(cl);
  19. }
  20. for(i = 0; i < buffers.length; i++) {
  21. buffers[i].setProperty("JRubyContainer", jrc);
  22. }
  23. outbuffer = buffer.getProperty("outbuffer");
  24. if((outbuffer == null) || outbuffer.isClosed()) {
  25. outbuffer = jEdit.newFile(view);
  26. buffer.setProperty("outbuffer", outbuffer);
  27. }
  28. out = new java.io.StringWriter();
  29. jrc.setWriter(out);
  30. String text() {
  31. view.showBuffer(buffer);
  32. res = textArea.getSelectedText();
  33. if(res == null || res.length() == 0) { res = buffer.getText(); }
  34. return res;
  35. }
  36. text = text();
  37. jrc.remove("DATA");
  38. ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
  39. if(ma.matches()) {
  40. text = ma.group(1);
  41. jrc.put("DATA", ma.group(2));
  42. }
  43. result = jrc.runScriptlet(new java.io.ByteArrayInputStream(("# -*- encoding : utf-8 -*-\n\n" + text).getBytes("UTF-8")), "(eval)");
  44. outbuffer.remove(0, outbuffer.getLength());
  45. outbuffer.insert(0, out.toString());
  46. if(result != null) {
  47. outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
  48. view.showBuffer(outbuffer);
  49. }

Report this snippet  

You need to login to post a comment.