Skip to content

2.1.2. Auto completion and static features

chaschev edited this page Jan 13, 2014 · 5 revisions

One of the benefits of the Bear is that it should the smallest possible amount to learn. You learn Bear by using Bear in your IDE which should guide you through the API. If something is unclear in Bear, feel free to create an issue or ask a question at Stackoverflow.com.

In this mini-tutorial we will show you how to easily type this cryptic method definition:

@Method
def ls(){
    run([named("ls task", { _,  task ->
        println _.sys.lsQuick(".")
    } as TaskCallable)])
}
  1. Start typing a method name. To get a list of method suggestions for your project, press Ctrl+Space and find the method run() which should be at the top of a list:

  1. A tooltip enlisting run() parameters will pop up. In case it does not or it disappears, press Ctrl+P to bring it back. run() accepts a list of TaskCallable, so continue typing to declare it with Groovy syntax [element1, element2]. Type new Name to create a new instance of a named callable and press Ctrl+Space to auto-complete:

  1. Press Ctrl+P to display the parameters. First is the name for the callable. Second is a closure containing code to run. In Java 6 closures are normally implemented as anonymous class instances, i.e. instances of Runnable or Callable, and Groovy has syntax sugar for this which we will use. Now we will create an anonymous inner class with help of IDE and then convert it to a closure. If you are familiar with Groovy closure syntax, you can use it right away.

Enter the task name and start typing new , hit Ctrl+Shift+Space. Then choose TaskCallable and it will create an anonymous class stub:

  1. Place cursor at TaskCallable declaration, hit Alt-Enter and choose 'Change to dynamic':

My version of Idea has a glitch and does not add as TaskCallable at the end of the closure, so you will need to add it manually.

  1. Dynamic Groovy does not require specifying a type in a variable declaration and Idea is able to infer the dynamic types so you can types from the declaration:

  1. Use _ (similar to $ in jQuery, Groovy just lacks this symbol) to access sys object which is used to run remote commands:

  1. Change the constructor to a template method named(...). For the majority of the constructors there are static equivalents - it gives a concise notation and allows to customize object creation when it is required in future.

After you do this the too pieces of code should look identical:

Clone this wiki locally