Rescript emacs support with rescript-vscode

Btw, as much as I reiterate that we should advertise this as experimental, I do think it’ll be nice if we can at least replace reason-mode with your work here in the future: https://rescript-lang.org/docs/manual/latest/editor-plugins

1 Like

I gave indentation another try today: big careless copy-pastes of large chunks of js-mode resulted in something that seems to work. The JSX support there is not obviously transferrable easily (it depends on font-lock properties created by code that looks fairly hairy that presumably would have to be combined with the existing reason-mode.el font-lock code that I also shovelled over). I’ve made no effort to remove JS knowledge nor add any ReScript knowledge in this indentation code.

Indentation on my project is pretty reasonable (which it has no right to be given how I’ve “implemented” it, if you can call it that!) – except for JSX. Sometimes JSX indentation isn’t too crazy, sometimes it is. It’s never good. I think it’s better than no indentation support when you’re writing JSX, but maybe not a whole lot better. I haven’t actually been coding with this yet, just indenting existing code. You can always ask bsc to indent (, = b in spacemacs) and things look fine. It seems that sometimes emacs takes minutes to apply the formatting changes from the LSP server (this is an emacs issue with updating the emacs buffer with the new text, not an issue affecting VS code nor the LSP server code) – I haven’t investigated to see exactly what problem it’s having, but so far it seems like if you don’t let the formatting get crazy far from the official bsc -format formatting (as in the whole buffer is totally different), that doesn’t happen.

Seems close to a minimally useful version, I think I need to:

  • remove the hack I used to work around an lsp-format-buffer issue, and submit a patch upstream
  • do a very slightly more careful re-do of the js-mode indentation hack, maybe
  • put the code in a repo with basic config examples

There’ll still be plenty of bugs then I’m sure, so keep your expectations not too high!

Edit: now I recall again that people had a different way to bsc -format, so I guess I won’t wait for that to work over LSP. Probably will be at least a week before I get around to publishing it though.

3 Likes

You’re doing awesome work, even if there are bugs, a nice Emacs experience is always a boon!

2 Likes

Seems I’ve got some more coding style guidelines to read before I can push to MELPA, so the install docs won’t work yet and you probably want to wait a day until the MELPA packages show up before trying this, but I’ve pushed what I’ve done here (the LSP config is in a separate repo lsp-rescript… hm maybe I should rename the repo to have emacs in the name):

Please:

  • Keep your expectations realistic: As I mentioned, indentation is “borrowed” from js.el and I left out the JSX indentation support from that entirely.
  • Keep discussion to the github issues or discussions on the rescript-mode github project rather than here

When I’ve pushed it to MELPA and given it a test this afternoon, I’d be glad of feedback of course

I’ll push a PR for the ReScript docs that reference the old reason-mode later.

Edit: first of two MELPA packages submitted (the other one’s the LSP config): Add rescript-mode recipe by jjlee · Pull Request #7545 · melpa/melpa · GitHub done
second one: Add lsp-rescript recipe by jjlee · Pull Request #7558 · melpa/melpa · GitHub – done

6 Likes

I love you, awesome work. Just got a new job in rescript and was super sad about the fact I couldn’t work in emacs.

1 Like

I thought I should mention that since I did this, @CarlOlson has done some work on a different fork of reason-mode, which he has also named rescript-mode (same name as my mode) here:

I’ll leave Carl to describe the differences if he wants.

You should be able to use lsp-rescript with either mode (this is just the lsp-mode config for rescript-vscode + a workaround). Edit: oops, strictly true but in fact you’d have to change lsp-language-id-configuration to point to Carl’s mode in fact. I just made that dependency on my MELPA rescript-mode package explicit in the lsp-rescript package – I won’t try to remove that dependency since this package is mostly useful as installable working (I hope!) boilerplate.

I’d personally be very happy if somebody else maintained emacs support for ReScript. Perhaps it’s not ideal if there are two modes called exactly “rescript-mode”, though (open to suggestions).

2 Likes

It should be stable if indentation isn’t needed, but I’m not treating my rescript-mode as something ready to use yet. I’m removing a lot of code and trying to make it simpler (the indentation code is always a mess in Emacs).

Since the direction is a bit different, I can rename it to not cause any confusion. However, it certainly won’t be in melpa for a long time (if ever).

1 Like

The MELPA code review is done, so the install docs work now. Currently the docs still refer to bsb etc. which I believe have just been renamed in the latest release – I’ll fix that, but I don’t think it should cause any problems (emacs isn’t running any of that directly).

One niggle: you need to explicitly M-x package-install <RET> rescript-mode <RET> if you’re following the “Vanilla emacs” install instructions. The spacemacs install instructions are not affected. I just pushed a fix to lsp-rescript to depend on rescript-mode, it will propagate to MELPA in a day or so I think.

Please keep any discussion on the github project, thanks.

2 Likes

Thanks for putting this together! Aiming to give both your ReScript and LSP packages a shot. Much prefer emacs to vs code

Can someone give me a quick way of getting this in my doom config? I have (!package rescript-mode) but it doesn’t come with a server. - sorry, read comment about keeping discussion on github project, where doom config is documented.