Showing posts with label ide. Show all posts
Showing posts with label ide. Show all posts

Saturday, June 25, 2016

Quickly create groovy script in Intellij IDEA

It is sometimes frustrating when you need quickly check db, non-trivial http, or AWS with Groovy. It is fine and great if standard lib is enough for your case, but once you need new libraries, it is not that quick. Grab is amazing, but you need to specify dependency ids and recall exact API and settings. At some point I had a bunch of typical scripts with typical settings, but you have to be in same project to find them, remember not to commit them, and they tend to grow big and have their own lives at some point. Bad for hacking and it is not convenient for majority of one-off cases.

That is where I started using IntelliJ Live Templates: they are available in every project, easily configurable, you can dispose or save results as needed, they accessible from regular files, scratches, even Groovy console.

My typical template consists of everything needed for single use case: grabs, imports, initialisation and bunch of examples. It is always easier to slash unneeded stuff than recall all API specifics. For me, it is more like tldr output, than what typical default template looks like.

There is example of one template I use for AWS S3:


@Grab('com.amazonaws:aws-java-sdk:1.9.31')
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.ObjectMetadata

def client = new AmazonS3Client(new BasicAWSCredentials('access', 'secret'))

println client.listObjects("bucket", "name").getObjectSummaries().collect{it.key}

def meta = new ObjectMetadata()
meta.setContentType("text/plain")
client.putObject("bucket", "name", new ByteArrayInputStream("1".bytes), meta)

client.deleteObject("bucket", "name")

println new String(client.getObject("bucket", "name").objectContent.bytes)

client.listVersions("bucket", "name").versionSummaries.each {
    println "${it.deleteMarker}   ${it.key}   ${it.size}"
}

client.deleteVersion("bucket", "name", "123")

And of course, you can have multiple templates preconfigured for different environments and use cases.

Biggest disadvantages comparing with regular files is that they are stored somewhere deeply in IntelliJ and not easily transferable, still they are easily exportable with other settings. Other disadvantage is that content is not searchable, so you have to pack all keywords as abbreviation or description, but anyway it is not part of universal search.

Wednesday, September 16, 2015

Using IDE scripting console in Intellij IDEA

Some time ago, Intellij added option to write simple scripts to automate it's functions, extract information and write simple plugins. It is located under Tools / IDE scripting console. It is very basic now, just scripting file that runs inside of Intellij JVM and has access to Intellij API.

 There is simple Groovy example that automatically adds non-suspending breakpoint with log expression:

import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.ProjectManager
import com.intellij.xdebugger.breakpoints.SuspendPolicy

def file = LocalFileSystem.getInstance().findFileByPath("/users/test/project/src/Test.java")
def doc = FileDocumentManager.getInstance().getDocument(file)
def bp = ProjectManager.getInstance().getOpenProjects()[0].getComponent("DebuggerManager").getBreakpointManager().addLineBreakpoint(doc, 11)
bp.getXBreakpoint().setLogExpression('"test"')
bp.getXBreakpoint().setSuspendPolicy(SuspendPolicy.NONE)


Of course, this basic task can be done through XML configuration, but IDE scripting has access to much more and can do trickier tasks. There is very cool demo. Generally, from what I have seen, API is pretty impressive, it is intuitive, powerful and easy to start with.

My biggest complains are that it is pretty verbose (as Java itself), it would be great if in future versions it would be solved at least at tooling levels - with auto imports for packages, autocompletion, and more convenience functions, like attaching to actions, getting projects or files quickly. Second problem is total lack of documentation. Basically, what I have found is only GitHub repo of Intellij community edition. It has multiple modules with API scattered across them, most generic classes are under Core API.

Sunday, April 5, 2015

My recipe for IntelliJ IDEA distraction free mode

One of the top features in IntelliJ IDEA 14.1 is distraction free mode. I am totally in love with it and not looking back, working 100% of time in it. Biggest concern about it is: loosing tabs. I was happy with tabs before, but it looks like there is a bunch of better options:

  • Recent Files (Cmd + E) - basically dropdown with tabs, but you can also do text search in it.
  • Switcher (Ctrl + Tab) - same as Recent Files, but opens file once shortcut is released. A little faster, but you are loosing search. This is the closest to original tabs.
  • Search Everywhere (Shift + Shift) - acts as Recent Files, but if you type something - also does search for Classes, Files, Symbols, Actions, Preferences. So together with tabs it basically replaces also Cmd + E, Cmd + O, Shift + Cmd + O, Alt + Cmd + O, Shift + Cmd + A.

I chose Search Everywhere as it is most feature rich and is really awesome. I also remapped it to Ctrl + E as it includes Recent Files, but as shortcut Ctrl + E works faster and is easier to reach.

Tabs had another aspect - tabs pinning, I didn't used it a lot, but for occasional use, bookmarks replace them nicely and provide more features as can pin specific line together with file. So use F3 to bookmark and Cmd + F3 to list bookmarks.

Everything else: like splitting, navigation bar, file path, tool windows, etc. is easy accessible via shortcuts or Find Action... (Shift + Cmd + A) if you forgot shortcut.

Monday, February 24, 2014

Setting different JAVA_HOME for Gradle in IntelliJ IDEA

There are a lot of different options to set JDK folder in Gradle, but in IntelliJ the only option that worked for me was adding this line:

gradle.java.home=C:/Program Files/Java/jdk1.8.0/

into idea.properties file.

Thursday, October 4, 2012

Reusing methods from different hierarchies with Mixins

Grails application can be tested with many different tools: Geb, Selenium, Grails own integration testing framework, etc. To use them, you have to extend some basic test case class, like GebReportingSpec, GroovyTestCase, GrailsUnitTestCase.
Usually there is a lot of methods that are used from all these frameworks, for example for data initialization and manipulation outside of test scenario scope. So how it would be possible to reuse these methods without access to class hierarchy and avoid code duplication. One easy way is to use Groovy Mixins, that allow to add new methods to any class without affecting existing class hierarchy. For example:


@Mixin(GenericTestMethods)
class SpecificTests extends GroovyTestCase {
...


Monday, June 18, 2012

IntelliJ IDEA Select In view with specific view

Intellij IDEA has a lot of great shortcuts, but for some reason there is no special shortcut when you need to find file in specific view, for example Grails view. Normal way to do it, is to press shortcut for "Select In..." (Alt+F1) and select your view (or press number with this view). This is basically 2 shortcuts and not too convenient.
Fortunately it is possible to make it easier by recording a Macro, and assigning it new shortcut. Just go to Edit > Macros > Start Macro recording, press "Select In..." shortcut and select your view.

Monday, November 15, 2010

Debugging grails in Netbeans

Grails plugin for Netbeans lacks some must have features like test launching from IDE and so on. Fortunately, Grails debugging is still super-simple with Netbeans. All you have to do is to run Grails from console with grails-debug instead of simply grails. For example, you want to debug test SeleniumTest.
Just type command in command console:
grails-debug test-app functional: SeleniumTest
Then go to Netbeans and select from menu Debug > Attach Debugger..., by default there is JPDA debugger, connector - SocketAttach, just enter your host and port (default is 5005, you can see yours in Grails console). Press OK. And it should connect to Grails process, so it will stop on breakpoints. Easy.
One thing to notice is that if you want to evaluate some expression in debug mode, you should use Java, but not Groovy, at least now.

Thursday, May 20, 2010

Intellij IDEA debugging trick

Recently discovered one nice debugging feature in Intellij IDEA.

Sometimes it is needed to stop on breakpoint after some other breakpoint was executed. For example there is next code:

1:parent.method(1);
2:parent.method(2);
3:parent.method(3);
4:parent.method(4);

5:void method(int i) {
6: init();
7: doStuff(i);
8:}

You want to stop on line 7 when parameter is 4. Using only basic breakpoints, you will have to skip 3 first stops or use some breakpoints conditions, which is not always handy for some types of parameters.

In Intellij there is nice feature, that let's you specify dependency between breakpoints. So, in this example, you will have to put breakpoint 1 on line 4 and breakpoint 2 on line 7 and configure breakpoint 2 to depend on breakpoint 1. Then breakpoint 2 will only stop after breakpoint 1 was executed.

Nice feature that saved some time for me.

Tuesday, March 16, 2010

Code bubbles IDE

There is  buzz in java world about new IDE concept. Idea is that it combines contexts and helps to keep all related files and information around tasks and ideas, but not around files and classes like traditional IDE.
Generally it is the same Mylyn idea, maybe better implementation, but should be tried.
Interesting presentation anyway.