Sha256: bd2888f8bf8fceea67507f344ca286b2681492370ffe51f212322f2d928fe5c6

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

require 'java'

module Redcar
  class Groovy
    class ReplMirror < Redcar::REPL::ReplMirror
      def title
        "Groovy REPL"
      end

      def grammar_name
        "Groovy REPL"
      end

      def prompt
        "groovy>"
      end

      def evaluator
        @instance ||= ReplMirror::Evaluator.new
      end

      def format_error(e)
        backtrace = e.backtrace
        "ERROR #{e.class}:\n #{e.message}\n        #{backtrace.join("\n        ")}"
      end

      def help
        h = super
        h << """
Note on Groovy Script Scoping:
Classes, undefined variables, and undefined closures are saved in the script
binding between statements.
Defined methods, closures, and variables are not added to the binding,
because they are considered local variables and thus are not available after
the defining statement.

Example:

def foo = 'hello! I am a local variable'
foo = 'hi! I am a binding variable'

See 'http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22'
for more information.
"""
      end

      class Evaluator
        def self.load_dependencies
          unless @loaded
            Groovy.load_dependencies
            import 'groovy.lang.GroovyShell'
            import 'java.io.PrintWriter'
            import 'java.io.StringWriter'
            @loaded = true
          end
        end

        def initialize
          Evaluator.load_dependencies
          @out = StringWriter.new
          @shell = GroovyShell.new
          @shell.setProperty('out',@out)
        end

        def inspect
          "groovyREPL main"
        end

        def execute(cmd)
          output = @shell.evaluate(cmd,"GroovyREPL").to_s
          output = "null" unless output and not output.empty?
          if @out and not @out.toString().empty?
            console = @out.toString() + "\n"
          else
            console = ""
          end
          buf = @out.getBuffer()
          buf.delete(0,buf.length()) if buf.length() > 0
          console + "===> #{output}"
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
redcar-groovy-0.2 lib/groovy/repl_mirror.rb
redcar-groovy-0.1-java lib/groovy/repl_mirror.rb
redcar-dev-0.12.9dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.8dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.7dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.6dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.4dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.3dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.1dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-dev-0.12.0dev-java plugins/groovy/lib/groovy/repl_mirror.rb
redcar-0.11 plugins/groovy/lib/groovy/repl_mirror.rb
redcar-0.11.0dev plugins/groovy/lib/groovy/repl_mirror.rb
redcar-0.10 plugins/groovy/lib/groovy/repl_mirror.rb