Motoko development in Neovim

I recently switched back from VSCode to Neovim.
Setting up Motoko with syntax highlighting and LSP (the one that’s inside the VSCode plugin by @rvanasa) took some extra steps.
I’m planing to clean it up and create some PRs to eventually get it upstream in nvim-lspconfig, mason.nvim, and treesitter to make it as easy to set up as any other language.

If you’re interested in setting up Neovim for Motoko development, feel free to reach out. I’ll share what I have so far.

4 Likes

Motoko language server is now part of the mason registry and can now be installed with Mason as motoko-lsp

Next step is getting it in nvim-lspconfig.

2 Likes

Thanks for doing this, I used to use Vim as well, but switched somewhere along the way.
Some old code: GitHub - aviate-labs/motoko.vim: Vim syntax files for Motoko
Maybe it is a good time to switch back!

1 Like

The PRs for nvim-lspconfig and mason-lspconfig.nvim are merged as well.
This means the Motoko-LSP can be installed in Neovim from the standard registries.

LSP

If you use something like kickstart.nvim for your configuration, you just have to add motko_lsp = {} to your list of LSPs and register the filetype motoko:

-- init.lua
-- [...]
  -- Enable the following language servers
  --  Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  --  [..]
local servers = {                                                                                                                                                                                                                                                              
    motoko_lsp = {}
}

-- [...]
vim.filetype.add { extension = { mo = 'motoko' } }

Formatter and syntax highlighting are not included in the above setup.

Formatter

I currently use conform with the prettier plugin (which is also used in the VSCode extension). I have prettier with the plugin installed globally, not sure if that is required or not.

 { -- Autoformat
    'stevearc/conform.nvim',
    lazy = false,
    keys = {
      {
        '<leader>=',
        function()
          require('conform').format { async = true, lsp_fallback = false }
        end,
        mode = '',
        desc = 'Format buffer',
      },
    },
    opts = {
     -- [...]
      formatters_by_ft = {
        -- [...]
        motoko = { 'prettier' },
      },
      formatters = {
        prettier = {
          prepend_args = function(self, ctx)
            return { '--plugin', 'prettier-plugin-motoko' }
          end,
        },
      },
    },
  },

Syntax highlighting

The syntax file linked by @quint above could be a quick way to get started (Btw. thanks for the reply. It’s nice to know someone is interested in this as well).

I’m currently using tree-sitter with the grammar from polychromatist/tree-sitter-motoko, compiled locally. The next step is to make this easier to install and probably also update the grammar to support the newer Motoko features.

1 Like