As I have been preparing to dive into Mojo, I have had trouble finding a definitive guide on setting up the LSP and syntax highlighting for Mojo in Neovim, or any other editor for that matter.
I think this would be a great place to document solutions tips for those of us trying to configure our editor of choice.
Note that you will want to run Neovim from a terminal where you first ran magic shell. That will set up the paths proper to that the mojo-lsp-server executable is available.
That worked, thank you for the response! Hadn’t seen the magic shell solution anywhere so glad we could document that here.
Now for parsing. As I understand, the Mojo parser is not currently open sourced, but I have seen community efforts to build a tree sitter grammar forked from python. Is that the best solution for the time being?
That is likely the best solution. I’m not as familiar with what is happening in that space yet though. What problem, specifically, are you trying to solve?
At the moment the most popular setup in neovim is using nvim-lspconfig to get sensible configurations for lsp servers, mason-lspconfig to install lsp servers and nvim-treesitter to install the language parser to enable things like syntax highlighting for example. Now the problem is these repositories need to be able to download these tools which needs them to be open source (see 2710) OR a way to call them similar to what lspconfig does atm.
Just wanted to drop in here that I was able to get things setup fairly well (lsp and parser). It took me a little while to piece this all together. Hopefully, it is helpful to someone else in the future.
I use nvim-lspconfig and mason for managing language servers. Mojo lsp isn’t on Mason yet so had to manually call require(‘lspconfig’).mojo.setup{} and then just make sure I was opening nvim in an environment with mojo-lsp-server installed. If using pixi as a package manager you just need to run pixi run nvim in a mojo project and the mojo-lsp-server will be available.
For parsing, this repo seems to be the most complete and up-to-date, https://github.com/lsh/tree-sitter-mojo. This I ended up cloning and building locally with the tree-sitter-cli, but I think this actually works just pointing nvim-treesitter to the repo. Then following nvim-treesitter directions for adding languages which is basically to add the following code to your init.lua.
local parser_config = require('nvim-treesitter.parsers').get_parser_configs()
parser_config.mojo = {
install_info = {
url = 'https://github.com/lsh/tree-sitter-mojo.git', -- local path or git repo
files = { 'src/parser.c', 'src/scanner.c' }, -- note: some parsers require scanner.c
-- optional entries:
branch = 'main',
requires_generate_from_grammar = false, -- if src/parser.c needs to be generated
},
filetype = { 'mojo', '🔥' },
}
Then just make sure to :TSInstall mojo to get everything incorporated. I also added mojo scm queries for highlighting, indenting, etc. by just putting ; inherits: pythonat the top of the file. The queries currently in that repo were for Zed and didn’t totally work with neovim. The only one I added more to was highlights.scm