Faster Feedback with Java, JBang, and TextMate


In previous post Using Java for scripting, with JBang, we learned about JBang - a new way of writing scripts with Java. In this post, we will see how we can improve on compile-and-run feedback using TextMate editor.

Following tweet shows what happened -

1. JBang Refresher

JBang, created by Max Andersen, enables you to use java for scripting. It supports Java 8 and onward. It supports dependency resolution, and IDE editing in addition to many other things. You may read Using Java for scripting, with JBang for more details on how to get started on it.

Quick Starter: I installed JBang with the homebrew. Following commands will install it and run a quick demo with JBang.

brew install jbangdev/tap/jbang
jbang version
jbang init HelloWorld.java
jbang HelloWorld.java

JBang supports many editors such VS Code, IntelliJ, Eclipse, Apache Netbeans and more. As the source files are actually pure java files, you can use any editor of your choice.

2. Inspiration

I have seen many presentations by Dr. Venkat Subramaniam. I always liked the way he uses TextMate editor for presentations and get a quick feedback from code. It is always neat and easy to follow. No distractions of switching between different windows.

So, I wanted to configure TextMate for quickly running java source files with JBang and get execution feedback.

In subsequent sections, we will see how to do it but here is a peak into final result -

jbang-textmate-helloworld-1

3. Configuring TextMate

TextMate is a -

Powerful and customizable text editor with support for a huge list of programming languages and developed as open source.
— From Macronotes home

TextMate uses bundles that can add or customize a lot of functionality. As we are going to use Java, we will work with Java Language bundle and customize it. To access bundle editor, go to BundlesEdit Bundles menu.

At the time of writing this article, I am using TextMate 2.0.6.

3.1 Disable existing run menu items

Firstly, we will disable existing "Compile & Run" and "Compile & Run (with args)" menus from Java Bundle.

You can do it by unchecking "Enable this item" checkbox for both menu items -

jbang-textmate-helloworld-2
This is primarily to reuse the same hot key combination as standard menu items. If you intent to use different key combinations, you can keep these enabled.

3.2 Add JBang Run menu

On bundle editor screen -

  1. Select path JavaMenu Actions.

  2. Press CMD(⌘)+N to add new menu action.

  3. In "Create New Item" modal, choose type to be "Command".

Configure command as shown in below. You can name it whatever you want.

jbang-textmate-helloworld-3

Some important parts are -

  • Key Equivalent: Hot key for triggering this action - CMD(⌘)+R. As defined in scope selector, it will be triggered on .java file extensions only.

  • Save: Selecting "Current Document" will save file before compiling.

  • Input: Select "Document" as an input for this command.

  • Output: The essence, Select "Show in Tool Tip" with "Text" as Format. This will render script output as tool tip. The whole point of all this :).

In the script editor section of this command, add following script and save command with CMD(⌘)+S.

#!/bin/bash
#https://manual.macromates.com/en/commands
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

OUTPUT=$(/usr/local/opt/jbang/bin/jbang $TM_FILENAME) (1)

echo "${OUTPUT}" (2)
1 Invoke JBang on our file. $TM_FILENAME, a TextMate variable, points to current file name. We capture the script execution in a variable called OUTPUT.
2 Print the output. This will get written to Tool tip in the editor.
If you don’t know where your JBang is installed, run which jbang command to find the path.
Writing output to a variable is necessary. Otherwise, if script execution results in compilation error then TextMate will show it in modal instead of ToolTip.

2. Inspiration section shows the result when script succeeds.

When script errors, the output looks like below -

jbang-textmate-helloworld-4

3.3 Add Jbang Run with Args menu

It is common to require some arguments for your script. To run script with arguments, we will add another menu item named "JBang Run with Args". Follow the same steps as we did in 3.2 Add JBang Run menu for adding "JBang Run" menu item. This time, we will set it to be invoked with CMD(⌘)+SHIFT+R key combination.

Also, we will use following script as command action -

#!/bin/bash
#https://manual.macromates.com/en/commands
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

args=$(CocoaDialog inputbox --title "Run with Arguments" \ (1)
--informative-text "Please provide arguments:" \
--button1 "Ok" --button2 "Cancel")
[[ $(head -n1 <<<"$args") == "2" ]] && exit_discard
args=$(tail -n1 <<<"$args")

OUTPUT=$(/usr/local/opt/jbang/bin/jbang $TM_FILENAME $args) (2)

echo "${OUTPUT}" (3)
1 Configures dialog box for capturing arguments
2 Pass captured $args to jbang command
3 Output script execution result.

Now, when you run source file with CMD(⌘)+SHIFT+R, you will get a dialoag for entering arguments. Whatever arguments you enter (space separated), are passed to the script. The script output is presented in tool tip, same as earlier option.

jbang-textmate-helloworld-5

4. What next?

There you have a faster feedback from your java as a script file. Keep modifying, run it with keys to see output in tool tip.

Go ahead and keep playing with the scripting, Java, and JBang!

If you have any thoughts or feedback on this article, feel free to comment on this article or find me on Twitter.

5. References

  • Using Java for scripting, with Jbang

  • Inspiration from Countless presentations by Dr. Venkat Subramaniam and his Running in TextMate.

  • For running with Arguments, I fiddled with the existing ruby script used for "Compile and Run (with Args)" Java menu action. It wasn’t so good for tool tip experience. Then I found this another similar article by Ivo Woltring. The current working option for argument dialog is from kotlin script on that article.

on twitter to get updates on new posts.

Stay updated!

On this blog, I post articles about different technologies like Java, MuleSoft, and much more.

You can get updates for new Posts in your email by subscribing to JavaStreets feed here -


Lives on Java Planet, Walks on Java Streets, Read/Writes in Java, JCP member, Jakarta EE enthusiast, MuleSoft Integration Architect, MuleSoft Community Ambassador, Open Source Contributor and Supporter, also writes at Unit Testers, A Family man!