简体   繁体   English

在IntelliJ IDEA中测试小代码片段的快捷方式?

[英]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. 我使用IntelliJ IDEA并且考虑能够弹出一个上下文,我可以编写,编译和运行一些简单的核心java代码然后在关闭上下文时自动处理整个事情。

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 operations; 有时你只想尝试简单的东西,比如正则表达式(我知道有一个正则表达式测试器插件)或一些按位运算; 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 : 考虑到IntelliJ Idea(来自版本14) 划痕

  • Press Ctrl+Shift+Alt+Insert and select type Ctrl+Shift+Alt+Insert并选择类型

在此输入图像描述

Java 9 Java 9

You can use jshell from any command line (or from the IntelliJ terminal ). 您可以从任何命令行(或从IntelliJ终端 )使用jshell

  1. View > Tool Windows > Terminal ( Alt + F12 ) 查看 > 工具窗口 > 终端Alt + F12

  2. Type jshell : 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: 使用以下方法之一退出jshell

    • type /exit 输入/exit
    • press Ctrl + D Ctrl + D.

I have a "CodePlay" project that exists completely locally (ie 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. 我有一个完全本地存在的“CodePlay”项目(即源是本地的,而不是在SCM沙箱中),当我需要使用一些简单的代码时,只需添加独立的类。

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. 我明白这不是你想要的(“当我关闭上下文时处理整个事情”)但是因为IntelliJ会让你在新窗口中打开一个项目我不认为它会破坏我的主要工作因为可以最小化或关闭“CodePlay”项目窗口而不影响“真实”项目窗口。

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. 我测试代码的方法是使用JUnit和一个临时类。 In the class I will create a single zero arg method with a JUnit @Test annotation. 在类中,我将使用JUnit @Test注释创建一个零arg方法。 The code I am playing with will be inside the method, and then a simple key stroke will run that code. 我正在玩的代码将在方法内部,然后一个简单的键击将运行该代码。

我还建议使用compileonline.com ,这对于测试小片段非常有用

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. Groovy shell(在IDEA中)是一个命令行应用程序,它允许您评估Groovy表达式,函数,定义类和运行Groovy命令。 The Groovy shell can be launched in Groovy projects and in Grails applications. Groovy shell可以在Groovy项目和Grails应用程序中启动。

Stop Ctrl+F2 Click this button to stop the current process. 停止按Ctrl + F2点击此按钮,停止当前进程。

Close Ctrl+Shift+F4 Click this button to close the selected tab of the Run tool window and terminate the current process. 关闭 Ctrl + Shift + F4单击此按钮可关闭“运行”工具窗口的选定选项卡并终止当前进程。

Execute Groovy Code (Ctrl+Enter) Run code, entered in the console. 执行Groovy代码 (Ctrl + Enter)运行在控制台中输入的代码。

Help F1 Use this icon or shortcut to open the corresponding help page. 帮助 F1使用此图标或快捷方式打开相应的帮助页面。

and I'll be trying this myself 我会自己尝试一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM