Help testing Julia tree-sitter mode in Emacs

Hi!

I, together with @Elrod, @non-Jedi, and @Tamas_Papp, are developing a new Julia major mode for Emacs powered by tree-sitter, using the already available Julia grammar here: GitHub - tree-sitter/tree-sitter-julia: Julia grammar for Tree-sitter

It will improve things, allowing us to fix old bugs that a regex-based parser will never be capable of.

The basic functionality is already implemented. We now need more people to try it out and report bugs or bad behavior (there should be many!).

We are discussing the implementation here: use tree-sitter · Issue #174 · JuliaEditorSupport/julia-emacs · GitHub

And the package can be obtained here: GitHub - ronisbr/julia-ts-mode: Experimental Emacs Julia major mode using tree-sitter

Notice that it requires Emacs 29, which has native tree-sitter support.

35 Likes

This is a very cool effort which has the potential to solve a large number of long-standing bugs in julia-mode as well as providing features that the mode hasn’t even attempted like structural navigation (although I don’t think this is setup yet). I encourage anyone who’s remotely interested to give it a try. Huge props to @Ronis_BR for pushing this forward and rapidly working out kinks as well as to the several people who’ve written the tree-sitter-julia grammar (@savq, @maxbrunsfeld, @pfitzseb, as well as several others who don’t appear to have discourse accounts)!

(I will note in passing that @Ronis_BR is giving me far too much credit here; my contribution has been the programming equivalent of backseat driving)

8 Likes

And mine was merely sitting in the backseat, going along for the ride. Great work!
I think the Julia experience in emacs is excellent thanks to your work!

As an example I enjoyed recently, a little bit of code (and a few dependencies like TestEnv.jl):

(use-package cov)
(defun maybe-cov-mode ()
  "activate cov-mode if the project root contains a coverage file
  currently, the recognized coverage filenames are:
    coverage-final.json
    coverage-lcov.info file"
  (if (or (file-exists-p (expand-file-name "coverage-final.json" (project-root (project-current))))
          (file-exists-p (expand-file-name "coverage-lcov.info" (project-root (project-current)))))
      (cov-mode)))
(add-hook 'prog-mode-hook #'maybe-cov-mode)
(defun project-abs-path ()
  (expand-file-name (project-root (project-current))))
(defun julia-runtest()
  (let* ((julia-project (project-abs-path))
         (lcov-file (concat julia-project "coverage-lcov.info")))
    (setq-local
     cov-lcov-file-name lcov-file
     compile-command
     (concat "julia --startup=no --history-file=no "
             " --project=" julia-project
             " --code-coverage=@" julia-project "src "
             " --code-coverage=" lcov-file " -tauto "
             " -e'import TestEnv; TestEnv.activate(); "
             " include(\"test/runtests.jl\")'"))))

allow me to run a package’s test suite with a keybinding (C-x p c – the standard compile-project keybinding), and then get the code coverage to display on the left fringe next to the code, so I can immediately see what is and isn’t covered.

All the standard IDE features like jump to definition and linting work well. If anyone is on the fence about giving emacs a try, I’d say it is well worth it in the long run.

5 Likes

When the release comes, it’d be good to add it officially to Tree-sitter|Introduction

1 Like

I think we could add it already. The Julia parser (GitHub - tree-sitter/tree-sitter-julia: Julia grammar for Tree-sitter) is in very good shape! There is some rough edges, but they are minor. In fact, I think that the current state of the Julia grammar is already much better than we had in the regex-based approach in both Vim and Emacs.

1 Like

Emacs v29 has not even been released yet. Is your mode really not usable with the version of tree-sitter available from Melpa for ordinary Emacs users?

Hi @Ralph_Smith !

Unfortunately, no. The implementations seem very different from each other.

This looks great!

Unfortunately I’m still on Emacs 28, otherwise I would try it out directly. When I do make the switch I’ll take a closer look at it!

1 Like

Thanks for mentioning me, but I didn’t contribute a much (yet). I think this will solve most of our parsing problems though in julia-mode.

2 Likes

Unfortunately, I cannot switch to pre-release Emacs 29, but I’d like to say: Thanks for making Julia work great with Emacs!!

Emacs may be the Mick Jagger of software, but – it rocks! :grinning:

6 Likes

LOL! :slight_smile: Indeed, I was using Neovim until mid-2022 I think? I switched to Neovim from Emacs because from the user point of view, Emacs development seems stalled, whereas Neovim was advancing very fast.

I could not be more wrong. Emacs 29 is just an incredible work! Tree-sitter, native compilation, LSP, etc.


By the way, for those how want to see how the new mode looks like, here is a picture with all highlighting (this is not the default):

4 Likes

Have considered submitting it to MELPA? That’d make installation for users much easier, it could be easily automated with use-package for example.

1 Like

Hi @giordano !

I am still not sure if this should be a separate package or if we should merge with Julia-mode. Ideas?

It can be a separate package for the time being, when it matures enough it can be merged into the “standard” julia-mode and removed from MELPA (they aren’t picky like the General registry about removal of packages). I don’t see any reasons for not having your package in MELPA right now, it’d only make adoption easier.

1 Like

I finally tried it. A couple of quick comments:

  • C-x n d (narrow-to-defun) works properly! It’s very broken in the standard julia-mode for functions spanning over more than a few lines. I presume it’s because scanning for the function boundaries fails, but I never had similar problems with long functions in any other language modes, I’m glad this works with this tree-sitter-based mode
  • one-line function definitions like f() = nothing aren’t found by imenu (this is also shared with standard julia-mode).
3 Likes

Ok! I will register it then :slight_smile:

Thanks for the feedback. I will investigate why that function does not show in imenu.

3 Likes

It should be working now, can you please test? The problem was that the tree-sitter grammar differentiates a “normal” function definition from a short function definition.

2 Likes

It seems to work now, thanks!

1 Like

And we are on MELPA now :slight_smile:

4 Likes

Tree-sitter will give us

6 Likes