开发者

Shortcuts for testing out small code snippets in IntelliJ IDEA?

I use IntelliJ IDEA and was thinking about how nice it would be to be able to pop up a context where I could write, compile and run some simple core java code and then automatically dispose of the whole thing when I close the context.

Sometimes you just want to try simple something out, like a regular expression (I'm aware there's a regex tester plugin) or some series of bit-wise opera开发者_高级运维tions; for whatever reason its not always feasible to test directly within the project code and creating a whole new project is disruptive to the work flow.

So my question is this: what do other people use to try out their snippets of code?


Take into account IntelliJ Idea's (from version 14) Scratches:

  • Press Ctrl+Shift+Alt+Insert and select type

Shortcuts for testing out small code snippets in IntelliJ IDEA?


Java 9

You can use jshell from any command line (or from the IntelliJ terminal).

  1. View > Tool Windows > Terminal (Alt + F12)

  2. Type jshell:

    user@xyz:~/IdeaProjects/prj$ jshell
    |  Welcome to JShell -- Version 9-ea
    |  For an introduction type: /help intro
    
    
    jshell> 
    
  3. Enter your snippet (you don't need to add ; at the end of the row)

    jshell> Set.of("c", "b", "a").size()
    $1 ==> 3
    
  4. Exit from jshell using one of the following approaches:

    • type /exit
    • press Ctrl + D


I have a "CodePlay" project that exists completely locally (i.e. the sources are local and not in the SCM sandbox) and just add standalone classes to it when I need to play around with some simple code.

I understand this isn't exactly what you wanted ("dispose of the whole thing when I close the context") but since IntelliJ will let you open a project in a new window I don't find it to be disruptive of my main work since the "CodePlay" project window can be minimized or closed without affecting the "real" project window.

Yes, there's the overhead of setting it up once, but once the project is set up it loads pretty quickly because there's not much in it and what is in it is pretty short.


My method to test code is using JUnit and a temporary class. In the class I will create a single zero arg method with a JUnit @Test annotation. The code I am playing with will be inside the method, and then a simple key stroke will run that code.


I would also recommend compileonline.com which can be great for testing small snippets


The answer can be found here. Groovy shell (in IDEA) is a command-line application that lets you evaluate Groovy expressions, functions, define classes and run Groovy commands. The Groovy shell can be launched in Groovy projects and in Grails applications.

Stop Ctrl+F2 Click this button to stop the current process.

Close Ctrl+Shift+F4 Click this button to close the selected tab of the Run tool window and terminate the current process.

Execute Groovy Code (Ctrl+Enter) Run code, entered in the console.

Help F1 Use this icon or shortcut to open the corresponding help page.

and I'll be trying this myself

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜