# Cadence LSP in Neovim

**URL:** <https://forum.flow.com/t/cadence-lsp-in-neovim/3875>\
**Category:** 🛠 Developer Tools\
**Tags:** cadence\
**Created:** [November 18, 2022, 11:44pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875 "2022-11-18T23:44:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![archer](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@archer](https://forum.flow.com/u/archer)\
**Post date:** [November 18, 2022, 11:44pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/1 "2022-11-18T23:44:36Z")

</div>

Neovim has built in LSP support, and the Flow CLI tool has language-server capabilities built into it, so I’m trying to get it working in Neovim. This is in lieu of LSP support from the [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) plugin, which I am using for all of my language servers besides Cadence currently.

#### Preamble:

I am aware of the [coc-cadence](https://github.com/Lucklyric/coc-cadence) plugin. I do not use CoC in my nvim config. Instead I use:

- nvim-cmp (for completion)
- null-ls (for formatting)

So this is what I have in my config right now:

> ```
> vim.api.nvim_create_autocmd("FileType", {
> pattern = "cadence",
> callback = function()
> vim.lsp.start({
> name = "cadence-language-server",
> cmd = { "flow", "cadence", "language-server" },
> root_dir = vim.fs.dirname(vim.fs.find({ "flow.json" }, { upward = true })[1]),
> })
> end,
> })
> 
> ```

But I get the following error upon nvim startup for a flow project:

> Error executing vim.schedule lua callback: …w/Cellar/neovim/0.8.0/share/nvim/runtime/lua/vim/lsp.lua:1285: RPC[Error] code\_name = InternalError, message = “invalid initializat  
> ion options”  
> stack traceback:  
> [C]: in function ‘assert’  
> …w/Cellar/neovim/0.8.0/share/nvim/runtime/lua/vim/lsp.lua:1285: in function ‘’  
> vim/\_editor.lua: in function \<vim/\_editor.lua:0\>

If anyone has luck getting the Cadence LSP that is bundled with the Flow CLI tool working with Neovim built in LSP, please do share here. I will be posting updates as I make progress.

Thanks all!

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [November 19, 2022, 9:12am UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/2 "2022-11-19T09:12:40Z")

</div>

I have something like this:

```auto
local lspconfig = require("lspconfig")
local configs = require "lspconfig.configs"
configs["cadence_lsp"] = {
  default_config = {
    init_options = {
          configPath = "/Users/bluesign/flow/kitty-items/flow.json",
          numberOfAccounts = "1"
    },
    cmd = {'flow', 'cadence', 'language-server'},
    root_dir = lspconfig.util.root_pattern(".git"),
    filetypes = { "cdc","cadence" },
    on_attach = on_attach,
  },
}

```

init\_options can be changed probably.

---

<div class="post-metadata">

**Author:** ![archer](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@archer](https://forum.flow.com/u/archer)\
**Post date:** [November 19, 2022, 6:01pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/3 "2022-11-19T18:01:28Z")

</div>

@bluesign wonderful thank you for sharing your config 🥳!

The issue was not passing in `init_options` – I’m not used to manually setting up LSPs so was unsure if issue was coming from my set up, or the Cadence LSP interface available to nvim.

Here is my working config:

```auto
vim.api.nvim_create_autocmd("FileType", {
	pattern = "cadence",
	callback = function()
		local flow_project_dir = vim.fs.dirname(vim.fs.find({ "flow.json" }, { upward = true })[1])
		vim.lsp.start({
			name = "cadence-language-server",
			cmd = { "flow", "cadence", "language-server" },
			root_dir = flow_project_dir,
			init_options = {
				configPath = flow_project_dir .. "/flow.json",
				numberOfAccounts = "1",
			},
			on_attach = on_attach,
			capabilities = capabilities,
		})
	end,
})

```

Maybe you can try my implementation of `init_options.configPath` so you don’t have nvim hardcoded to use a single project.

#### Things to Figure Out Still

- I like how you are importing from the `lspconfig` plugin. I tried out your implementation and couldn’t get LSP to attach. I will revisit this but for now I got the Cadence LSP running in Nvim which was my main goal.
- I want to explore the rest of the `init_options` available. Feels odd hardcoding `numberOfAccounts = "1"` but not clear if it affects LSP usage in any way.

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [November 19, 2022, 6:10pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/4 "2022-11-19T18:10:42Z")

</div>

I think configPath doesn’t have much effect ( unless you want to deploy to the emulator in LSP )

Probably you may not need options, I just copied from vscode plugin ( I just use LSP part for syntax etc, for deploy I use flow-cli )

> - I like how you are importing from the `lspconfig` plugin. I tried out your implementation and couldn’t get LSP to attach. I will revisit this but for now I got the Cadence LSP running in Nvim which was my main goal.

Full config:

```auto
local on_attach = function(client, bufnr)
    vim.cmd("command! LspDef lua vim.lsp.buf.definition()")
    vim.cmd("command! LspFormatting lua vim.lsp.buf.formatting()")
    vim.cmd("command! LspCodeAction lua vim.lsp.buf.code_action()")
    vim.cmd("command! LspHover lua vim.lsp.buf.hover()")
    vim.cmd("command! LspRename lua vim.lsp.buf.rename()")
    vim.cmd("command! LspRefs lua vim.lsp.buf.references()")
    vim.cmd("command! LspTypeDef lua vim.lsp.buf.type_definition()")
    vim.cmd("command! LspImplementation lua vim.lsp.buf.implementation()")
    vim.cmd("command! LspDiagPrev lua vim.diagnostic.goto_prev()")
    vim.cmd("command! LspDiagNext lua vim.diagnostic.goto_next()")
    vim.cmd("command! LspDiagLine lua vim.diagnostic.open_float()")
    vim.cmd("command! LspSignatureHelp lua vim.lsp.buf.signature_help()")
    buf_map(bufnr, "n", "gd", ":LspDef<CR>")
    buf_map(bufnr, "n", "gr", ":LspRefs<CR>")
    buf_map(bufnr, "n", "gy", ":LspTypeDef<CR>")
    buf_map(bufnr, "n", "K", ":LspHover<CR>")
    buf_map(bufnr, "n", "[a", ":LspDiagPrev<CR>")
    buf_map(bufnr, "n", "]a", ":LspDiagNext<CR>")
    buf_map(bufnr, "n", "ga", ":lua vim.lsp.codelens.run {}<CR>")
    buf_map(bufnr, "n", "<Leader>a", ":LspDiagLine<CR>")
    buf_map(bufnr, "i", "<C-x><C-x>", "<cmd> LspSignatureHelp<CR>")
    
    vim.cmd("autocmd CursorHold,InsertLeave <buffer=%s> lua vim.lsp.codelens.refresh()")
    if client.resolved_capabilities.document_formatting then
        vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()")
    end
end

 local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())

local lspconfig = require("lspconfig")
local configs = require "lspconfig.configs"
configs["cadence_lsp"] = {
  default_config = {
    init_options = {
          configPath = "/Users/bluesign/flow/kitty-items/flow.json",
          numberOfAccounts = "1"
    },
    cmd = {'flow', 'cadence', 'language-server'},
    root_dir = lspconfig.util.root_pattern(".git"),
    filetypes = { "cdc","cadence" },
    on_attach = on_attach,
  },
}

lspconfig.cadence_lsp.setup {
     on_attach = function(client, bufnr)
        on_attach(client, bufnr)
    end, 
    capabilities = capabilities,
}

```

---

<div class="post-metadata">

**Author:** ![bluesign](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bluesign/32/1359_2.png) [@bluesign](https://forum.flow.com/u/bluesign)\
**Post date:** [November 19, 2022, 6:12pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/5 "2022-11-19T18:12:19Z")

</div>

> [@archer](#):
>
> - I want to explore the rest of the `init_options` available. Feels odd hardcoding `numberOfAccounts = "1"` but not clear if it affects LSP usage in any way.

> <https://github.com/onflow/cadence-tools/blob/0d961fc5d7e7bc94eaa19fefe0adb21edc12f5f6/languageserver/integration/integration.go#L86>

---

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [February 5, 2023, 9:09pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/6 "2023-02-05T21:09:32Z")

</div>

Thanks guys with this I was able to create this PR: [https://github.com/neovim/nvim-lspconfig/pull/2435](https://github.com/neovim/nvim-lspconfig/pull/2435).

---

<div class="post-metadata">

**Author:** ![archer](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@archer](https://forum.flow.com/u/archer)\
**Post date:** [February 7, 2023, 3:19am UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/7 "2023-02-07T03:19:13Z")

</div>

fantastic! thank you for making a contribution. glad to see cadence is better supported in neovim 🙂

---

<div class="post-metadata">

**Author:** ![bjartek](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/bjartek/32/143_2.png) [@bjartek](https://forum.flow.com/u/bjartek)\
**Post date:** [May 3, 2023, 5:59pm UTC](https://forum.flow.com/t/cadence-lsp-in-neovim/3875/8 "2023-05-03T17:59:08Z")

</div>

I put together this:

> **[GitHub - bjartek/nvim-cadence: Syntax highlight for the Cadence programming...](https://github.com/bjartek/nvim-cadence)**
>
> Syntax highlight for the Cadence programming language. A high level programming lanaguage intended for smart contract development. :neckbeard: - GitHub - bjartek/nvim-cadence: Syntax highlight for ...

If you clone out lazyvim and add that snippt to plugins/cadence.lua it works pretty well for me 🙂
