pinoyyid
pinoyyid

Reputation: 22306

How do I change the formatter line length with neovim lsp dart?

I'm running nvim using the built in LSP (via the superb AstroVim) to develop dart and flutter.

Loving everything, except how the lsp formatting (which formats on save) is wrapping my lines at 80 characters.

I can see how the dart command line formatter supports

--line-length=<value>

My question: how do I include that parameter to the lsp in the

lua vim.lsp.buf.formatting()

command in order to format at a longer line length.

PS. yes I'm fully aware of the religious war over line length.

PPS. I've tried this in my AstroVim user config, but it doesn't seem to work

["server-settings"] = {
  dartls = {
    settings = {
      ["line-length"] = 120
    }
  }
}

Upvotes: 4

Views: 3360

Answers (2)

Maksim Terpilovskii
Maksim Terpilovskii

Reputation: 851

You are probably using flutter-tools.nvim. In that case just create ~/.config/nvim/lua/plugins/flutter-tools.lua file and put something like that in it:

return {
  "akinsho/flutter-tools.nvim",
  opts = {
    lsp = { settings = { lineLength = 120 } },
  },
}

flutter-tools should not be configured using nvim-lspconfig. Here is the quote from flutter-tools GitHub page:

flutter tools does not depend on nvim-lspconfig. The two can co-exist but please ensure you do NOT configure dartls using lspconfig. It will be automatically set up by this plugin instead.

By the way, you can find other dart settings here.

UPD:

If you are using nvim-lspconfig for setting dartls, you can change dart settings that way (here's the example):

return {
    "neovim/nvim-lspconfig",
    config = function()
        require("lspconfig").dartls.setup({
            settings = { dart = { lineLength = 120 } },
        })
    end,
}

Upvotes: 2

lcheylus
lcheylus

Reputation: 2443

With AstroNvim, you could add options for your LSP configuration with lsp.server-settings.<lsp> option.

Replace <lsp> by the name of the LSP server used for dart/flutter and add option for line-length (options could be a table or a function). See examples in https://github.com/AstroNvim/AstroNvim/tree/main/lua/configs/lsp/server-settings and https://github.com/AstroNvim/AstroNvim/blob/main/lua/user_example/init.lua

According to dartls documentation, the correct configuration should be :

["server-settings"] = {
  dartls = {
    settings = {
      dart = {      
        lineLength = 120
      }
    }
  }
}

Upvotes: 0

Related Questions