Working with large Julia source files in Emacs2017/12/04
When writing software, especially libraries, a natural question is how to organize source code into files. Some languages, eg Matlab, encourage a very fragmented style (one function per file), while for some other languages (C/C++), a separation between the interface (
.h) and the implementation (
.cpp) is traditional.
Julia has no such constraint:
include allows the source code for a module to be organized into small pieces, possibly scattered in multiple directories, or it can be a single monolithic piece of code. The choice on this spectrum is up to the authors, and is largely a matter of personal preference.
When I started working with Julia, I was following the example of some prominent packages, and organized code into small pieces (~ 500 LOC). Lately, whenever I refactored my code, I ended up putting it in a single file.
I found the following Emacs tools very helpful for navigation.
Form feeds and page-break-lines-mode
Form feed, or
\f, is an ASCII control character that was used to request a new page in line printers. Your editor may display it as
^L. It has a long history of being used as a separator, and Emacs supports it in various ways.
C-x [ and
C-x ] take you to the previous and next form feed separators. Combined with numerical prefixes, eg
C-3 C-x [, you can jump across multiple ones very quickly. Other commands with
page in their name allow narrowing, marking, and other functions.
Many Emacs packages provide extra functionality for page breaks. My favorite is page-break-lines, which replaces
^L with a horizontal line, so that the output looks like this:
# general API """ ML_estimator(ModelType, data...) Estimate `ModelType` using maximum likelihood on `data`, which is model-specific. """ function ML_estimator end
Finding things quickly
I am using helm pervasively.
helm-occur is very handy for listing all occurrences of something, and navigating them. The following is an except from
base/operators.jl, looking for
operators.jl:213:types with a canonical total order should implement `isless`. operators.jl:227:<(x, y) = isless(x, y) operators.jl:300:# this definition allows Number types to implement < instead of isless, operators.jl:302:isless(x::Real, y::Real) = x<y operators.jl:303:lexcmp(x::Real, y::Real) = isless(x,y) ? -1 : ifelse(isless(y,x), 1, 0)
You can move across these matches, jump to one in an adjacent buffer while keeping this list open, or save the list for later use. Its big brother
helm-do-grep-ag is even more powerful, using ag to find something in a directory tree.
With these two tools, I find navigating files around 5K LOC very convenient — the better I learn Emacs, the larger my threshold for a “large” file becomes.1
- In case you are wondering, the largest files are around 6K LOC in Julia
Baseat the moment. [return]