Friday, January 19, 2018

BBEdit Codeless Language Module for Swift

I’ve posted a fork of Curt Clifton’s BBEdit CLM for Swift. My version adds support for some newer Swift keywords, triple-quoted string literals, and class functions. The main improvement is that functions inside of classes (and structs, extensions, and protocols) are now indexed and available for BBEdit’s function pop-up.

Previously, only top-level functions were indexed. This was a direct consequence of the way CLMs require a single regex to match the “function” name and its body (in curly braces). If an entire class is consumed all at once, the functions inside become invisible, as there is no nested matching. This was not very useful for me because I mostly write member functions rather than global ones. Aidan Dysart’s CLM solves this problem by only indexing functions. But I want the other types to be indexed, too. It turns out that this is possible by changing the regex so that it only consumes the body for functions; for other types it stops matching after the name.

My first thought was to implement this by having separate alternations in the pattern for matching functions with content in braces and “functions” without bodies. However, this didn’t work because the function name has to be marked with (?P<function_name>), and you are only allowed to use that once. The solution I found was conditional matches, whereby the braces after the “function” name only match if the keyword before the “function” name was func. The main downside to this approach is that since non-func functions no longer have any braced content, they cannot be folded. However, I think that’s a small price to pay for getting them indexed. I mostly want to fold function bodies rather than whole classes, anyway.

1 Comment RSS · Twitter


[…] perhaps underselling it. I’ve been using the Swift language module for a while now (replacing my CLM) and am very happy with […]

Leave a Comment