Skip to content

dart lsp configuration

lsp configuration for dart/flutter

The first part is a handler for closing tags and the second tells neovim what to do when for a FileType event with pattern dart.

-- handler for dart/textDocument/publishClosingLabels
local namespace = vim.api.nvim_create_namespace("flutter_closing_tags")
local function closing_tags(error, result, ctx, config)
  vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)

  for _, item in ipairs(result.labels) do
    local line = tonumber(item.range["end"].line)
    if line <= vim.api.nvim_buf_line_count(0) then
      vim.api.nvim_buf_set_extmark(0, namespace, line, -1, {
        virt_text = { {
          "// " .. item.label,
          "Comment",
        } },
        virt_text_pos = "eol",
        hl_mode = "combine",
      })
    end
  end
end

vim.api.nvim_create_autocmd('FileType', {
  group = lspautocmds,
  pattern = 'dart',
  callback = function(ev)
    vim.lsp.start({
      name = "dartls",
      cmd = { 'dart', 'language-server', '--protocol=lsp' },
      root_dir = vim.fs.root(ev.buf, { 'pubspec.yaml' }),
      init_options = {
        closingLabels = true,
        flutterOutline = true,
        onlyAnalyzeProjectsWithOpenFiles = true,
        outline = true,
        suggestFromUnimportedLibraries = true
      },
      -- capabilities = require('cmp_nvim_lsp').default_capabilities(),
      capabilities = vim.lsp.protocol.make_client_capabilities(),
      settings = {
        dart = {
          completeFunctionCalls = true,
          showTodos = true,
          enableSnippets = true,
          documentation = "full",
        },
      },
      handlers = {
        ["dart/textDocument/publishClosingLabels"] = closing_tags
      }
    })
    vim.opt.commentstring="// %s"
  end,
})

Comments