Textmate - get Run output as Tooltip

Challenge

You want to give some simple demos in java, kotlin or python (or any other language for that matter) and have the output shown immediately as a tooltip.

Solution

Java

  • go to Bundles > Edit Bundles... > Java > Menu Actions > Compile & Run
  • Change according the picutre

  • The original code looks like this:
1
2
#!/bin/sh
"$TM_BUNDLE_SUPPORT/bin/javamate.rb"
  • You need to change it to the commands below:
1
2
3
4
5
6
7
#!/bin/sh
if [ -f "./compile.sh" ]; then
/bin/sh ./compile.sh
else
cd "$TM_DIRECTORY"
javac "$TM_FILENAME" && java "${TM_FILENAME%.java}"
fi
  • The script above will look for a compile.sh script in the folder of your java file and if found it wil run that. You can do whatever you like in that compile.sh file. If no such file is found it will compile the current java file being edited and run that one.
  • Now just command+R on a Java class…
  • The output will be shown as a tooltip at your cursor.

Kotlin

  • The Kotlin plugin is to be gotten here it is strongly based on this bundle but has added a run and run with args command to the bundle. I did a pull request to the original repo but have not gotten any response yet.

Again this bundle is setup in such a way that it will look for a kotlin.sh file in the folder where the file you want to compile is located. If found it will run that file. Do whatever you want there. If not found the script will determine if the kotlin is a script file (*.kts) and if so it will run it accordingly. If it is a standard kotlin file (*.kt) it will compile it and then run it. It of course needs a top level declaration in that case.

The whole idea of this way of compiling is to give small demos, so it is not really set up to do much more than accomodate that.

If you want to do it yourself here is the code for the Run command (cmd+r)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
if [ -f "./kotlin.sh" ]; then
/bin/sh ./kotlin.sh
else
cd "$TM_DIRECTORY"
JAVA8="$(ls /Library/Java/JavaVirtualMachines|grep jdk1.8|awk '{ print "/Library/Java/JavaVirtualMachines/" $1 "/Contents/Home"}')"
if [ -z "$JAVA8" ]; then
echo "No java 8 jdk found..."
exit 1
fi
if [ "${TM_FILENAME##*.}" = "kts" ]; then
echo "$(JAVA_HOME=${JAVA8} /usr/local/bin/kotlinc -script "$TM_FILENAME")"
else
echo "$(JAVA_HOME=${JAVA8} /usr/local/bin/kotlinc "$TM_FILENAME")"
class=${TM_FILENAME%.kt}
class="$(tr '[:lower:]' '[:upper:]' <<< ${class:0:1})${class:1}"
class="${class}Kt"
echo "$(JAVA_HOME=${JAVA8} /usr/local/bin/kotlin "${class}")"
rm -rf META-INF "${class}".class
fi
fi

Code for the run with parameters (option+cmd+r):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#https://manual.macromates.com/en/commands
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
res=$(CocoaDialog inputbox --title "Input" \
--informative-text "Please provide arguments:" \
--button1 "Ok" --button2 "Cancel")
[[ $(head -n1 <<<"$res") == "2" ]] && exit_discard
res=$(tail -n1 <<<"$res")
if [ -f "./kotlin.sh" ]; then
#if kotlin.sh exists in the folder where the file resides that will be used as run job
/bin/sh ./kotlin.sh $res
else
cd "$TM_DIRECTORY"
JAVA8="$(ls /Library/Java/JavaVirtualMachines|grep jdk1.8|awk '{ print "/Library/Java/JavaVirtualMachines/" $1 "/Contents/Home"}')"
if [ -z "$JAVA8" ]; then
echo "No java 8 jdk found..."
exit 1
fi
if [ "${TM_FILENAME##*.}" = "kts" ]; then
#interpret a kts script
JAVA_HOME=${JAVA8} /usr/local/bin/kotlinc -script "$TM_FILENAME" $res
else
#compile and run the file as a main
JAVA_HOME=${JAVA8} /usr/local/bin/kotlinc "$TM_FILENAME"
class=${TM_FILENAME%.kt}
class="$(tr '[:lower:]' '[:upper:]' <<< ${class:0:1})${class:1}"
class="${class}Kt"
JAVA_HOME=${JAVA8} /usr/local/bin/kotlin "${class}" $res
rm -rf META-INF "${class}".class
fi
fi

Note that the bundle needs a Java 1.8 JDK to work. When running on Java 9 it wil give warnings about illegal accessing stuff. So this bundle explicitely sets the java home to a java 1.8 location. When that problem has been solved it might not be nessesary to do that anymore.
Note als that the commands are performed in an echo. This is because (compile) errors will in this case also be shown as a tooltip. Otherwise this would result in an textmate popup window.

Python

Just change the settings in the python bundle to give the results in the tooltip window (see java section)

Change tooltip font size

In a terminal perform the following command:

1
defaults write com.macromates.textmate NSToolTipsFontSize 24

Where 24 will be the size of your tooltip font

Comments / tips and tricks

Comments on this article and other tips and tricks are much appreciated…