sway icon indicating copy to clipboard operation
sway copied to clipboard

Add language server support to Emacs

Open JoshuaBatty opened this issue 3 years ago • 8 comments

I suspect this will involve a similar process to getting vim support.

JoshuaBatty avatar Dec 06 '22 03:12 JoshuaBatty

Looks like we'd need to add it to https://github.com/emacs-lsp/lsp-mode

It's a little involved and since we don't have a ton of demand for this right now, I'm going to put it back in the backlog.

sdankel avatar Apr 19 '23 21:04 sdankel

This would be a feature I would use. I prefer to use the built-in eglot package (because it's built-in. Maybe lsp-mode is better, I'm not sure). https://github.com/joaotavora/eglot

It might be as simple as customizing eglot-server-programs, something that could be added to documentation for the user to do with no code change to eglot.

I have not looked into it

MorganJamesSmith avatar Jan 30 '25 14:01 MorganJamesSmith

Thanks @MorganJamesSmith . Eglot looks like it should work. Not sure if we will get time to add this support any time soon but happy to help advise if you wanted to have a go in the mean time :)

JoshuaBatty avatar Jan 30 '25 21:01 JoshuaBatty

So I have added this code snippet to my init file:

(define-derived-mode sway-mode prog-mode "sway")

(add-to-list 'auto-mode-alist '("\\.sw\\'" . sway-mode))

(add-to-list
 'eglot-server-programs
 '((sway-mode) .
   ("nix" "shell" "github:fuellabs/fuel.nix#fuel" "--command" "forc-lsp")))

As far as I can tell the language server part works great!

I'm able to jump to definitions and view documentation in the mode-line via eldoc.

The only problem seems to be that there is zero code highlighting. No colors. Like staring at a plain text file.

This is sub-optimal.

I think the current thing holding back emacs support is not actually the LSP server, but the lack of a major mode for sway. I was unable to find anything myself but could you confirm there is no emacs major mode for sway?

MorganJamesSmith avatar Jan 31 '25 17:01 MorganJamesSmith

Actually if we define sway mode like this (inheriting the rust tree-sitter mode (emacs doesn't have a normal rust-mode, only a tree-sitter version)):

(define-derived-mode sway-mode rust-ts-mode "sway")

Then the highlighting seems about right. Are there any major differences between sway and rust? Can I rely on rust tree-sitter (https://github.com/tree-sitter/tree-sitter-rust) to parse sway code correctly?

MorganJamesSmith avatar Jan 31 '25 18:01 MorganJamesSmith

Nice! sounds like good progress. We actually have a tree-sitter definition for sway here that you could use https://github.com/FuelLabs/tree-sitter-sway

JoshuaBatty avatar Feb 01 '25 03:02 JoshuaBatty

Hey @sdankel @JoshuaBatty, I'm part of the Mira core team through ChartHouse Labs working on some major tooling, ecosystem, and design overhauls.

Also an Emacs (Doom) user that would really appreciate LSP support as I'm learning Sway currently, could be interested in contributing :)

Image

Oh look @MorganJamesSmith is here too 👀

MFarabi619 avatar Apr 23 '25 00:04 MFarabi619

Hello! I have written sway-ts-mode to add sway syntax highlighting to Emacs. It was a lot more work then I was expecting. Here is the link: https://git.sr.ht/~morgansmith/sway-ts-mode

It seems to work but I haven't tested it extensively. If other people could try it I would appreciate that. I should probably add better documentation but that will have to wait until at least tomorrow

MorganJamesSmith avatar May 15 '25 21:05 MorganJamesSmith