Linter/formatter for naming conventions?

Hi all,

I’m working on writing more idiomatic Julia code and trying to adhere to a common style. I know this is a relatively minor detail, but for whatever reason – old habits die hard, I guess! – I consistently fail to correctly mix CamelCase/snake_case/SCREAMING_SNAKE_CASE/etc for different names as I’m typing.

Since I usually think that style guides are most effective when their rules can be enforced automatically, I was hoping to find a linter or formatter that would handle this for me. I looked through the documentation for great tools like JuliaFormatter.jl, StaticLint.jl, and LanguageServer.jl; while they cover just about every other aspect of the style guides I couldn’t find an option for this.

Building things is fun / educational, so I glued together an imperfect but (relatively) functional implementation using neovim’s treesitter integration. At the same time… “enforcing naming conventions” feels like a common enough issue that I suspect a more standard solution already exists. Did I miss a standard tool or configuration option in the ecosystem for this?

Thanks for any pointers!

3 Likes

While trying to dig into this a bit more I found an open issue over on the JuliaFormatter repo in which some stricken-through text suggested “Function Naming (not possible)”. There wasn’t much information about why it wasn’t possible – and of course: even though nobody has chimed in here yet, absence of evidence is hardly evidence of absence.

I still suspect that I’m just missing something, and that some tool that uses an LSP or that directly performs a semantic analysis of the Julia code probably does this. But in the absence of that, I thought I would clean up and share the glued-together implementation I mentioned above.

It’s an (imperfect) implementation that uses treesitter to parse the code. Obviously this syntactic-rather-than-semantic approach has limitations / sometimes flags things incorrectly, but so far I find it to work surprisingly well (the task of catching definitions is a pretty restricted version of a more general problem, so syntax alone can get us most of the way there for most code).

The plugin – along with more information, and a few demo videos showing how it behaves when applied to existing packages like ForwardDiff.jl and JuMP.jl – can be found over here: https://github.com/DanielMSussman/simpleJuliaTreesitterTools.nvim.

Obviously this is a bit niche, but since I find it helpful to help clean up after my old naming habits I thought it might help others, too. Feedback is welcome, and if someone wants to take the ideas from this plugin generalize them away from neovim’s treesitter integration (or just implement the semantic analysis version) – even better!