2019/10/16: Jump to the correct file

Sometimes, simple and tiny scripts can be surprisingly useful, like substituting in the current working directory when working in parallel hierarchies. Recently, I found myself in a similar, but slightly different situation. The standard way for Java to print a stack trace includes the fully-qualified class name, as well as the file name and line number within the file. So far so good. What is not included is the path to the source file relative to the source-tree root (we definitely wouldn't want the absolute paths, as that would leak the working directory of the compilation and thus destroying reproducibility). So we have to find (hint!) the file in the source tree. Of course, Java requires a quite strict mapping between paths and the package structure—provided one keeps in mind that, e.g., test classes are in their own parallel tree. However, something simpler turns out to work even better: Java's preference for verbose class names has the nice side effect that they are almost always unique within the code base. So we can just take the first file with that name. A command line like ecl AbstractFooFactory.java:1234, with, of course, the file-name-colon-line construct copy-and-pasted, and ecl the script quoted below, just opens the file at the right position in the editor. (Also note the confusing but powerful ways of shell variable expansion.)


#!/bin/sh

name=${1%:*}
line=${1#*:}

find . -name "${name}" -print -exec emacsclient -n +"${line}" {} \; -quit
download