FYI: simple lsp setup in LazyNvim

Just for reference setting up the Mojo Language Server in LazyVim is as trivial as creating a file in the lua/plugins folder, for example mojo,lua, with the following content

-- Load the LSP.
require("lspconfig").mojo.setup({})

I have also collected some good error format strings for mixed Python/Mojo projects: init.lua/lua/local/error_formats.lua at main · winding-lines/init.lua · GitHub

I am using these with an async runner and parsing in the quick fix list. My mojo.lua file that puts them all together is here: init.lua/lua/plugins/mojo.lua at main · winding-lines/init.lua · GitHub

This is my first neovim lua project and Claude.ai wrote a bunch of this so the code may not be idiomatic but seems to work.

I hope this helps,

Marius

2 Likes

It turns out you can also pass cli arguments. In the mini projects I’ve used so far the LSP was not doing well in tests but passing the current working directory as an include dir solved the problem.

require("lspconfig").mojo.setup({ cmd = { "mojo-lsp-server", "-I", "." } })

1 Like

@mseritan did you ever figure out how to get formatting to work nicely?

Good question, I just format from the cli.

With LazyVim I have a mojo.lua file in the nvim/lua/plugins/ directory of my configuration. I’m using this snippet to set up the LSP to do formatting:

{
  "neovim/nvim-lspconfig",
  opts = function()
    -- Format on save using LSP
    vim.api.nvim_create_autocmd("BufWritePre", {
      pattern = "*.mojo",
      callback = function()
        local clients = vim.lsp.get_clients({ bufnr = 0, name = "mojo" })
        if #clients > 0 and clients[1].supports_method("textDocument/formatting") then
          vim.lsp.buf.format({ async = false })
        end
      end,
    })
  end,
},

It seems to work well.

1 Like

You can also use conform.nvim like this:

  {
    "stevearc/conform.nvim",
    optional = true,
    opts = {
      formatters_by_ft = {
        mojo = { "mojo" },
      },
      formatters = {
        mojo = {
          command = "mojo",
          args = { "format", "-" },
          stdin = true,
        },
      },
    },
  },

1 Like

Conform is what I ended up setting up and it works great!

1 Like

this also seems to work
opts = {
formatters_by_ft = {
mojo = { “mojo_format” },
}