akinsho / flutter-tools.nvim

Tools to help create flutter apps in neovim using the native lsp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to setup `root_dir` for dartls

Energizer328 opened this issue · comments

Hi! I usually work in flutter projects with multiple packages and I realized that lsp features like references were only analyzing the packages of the currently opened buffers. I did some digging around and the problem seems to be the root directory of dartls. Now, if I setup dartls using lspconfig, I can actually configure this using the root_dir parameter of the configuration. Like this

require("lspconfig").dartls.setup {
  on_attach = commons.on_attach,
  settings = {
    enableSnippets = true,
  },
  init_options = {
    onlyAnalyzeProjectsWithOpenFiles = false,
  },
  root_dir = function() return vim.loop.cwd() end,
}

However, when i try to do a similar thing with flutter-tools

require("flutter-tools").setup {
  lsp = {
    on_attach = commons.on_attach,
    settings = {
      enableSnippets = true,
    },
    root_dir = function() return vim.loop.cwd() end,
    init_options = {
      onlyAnalyzeProjectsWithOpenFiles = false,
    },
  },
  widget_guides = {
    enabled = true,
  },
}

it doesn't work. I also took a quick peek under the hood, and it seems to me that the root_dir parameter is completely ignored by flutter-tools:

get_server_config(user_config, function(c)
c.root_dir = M.get_lsp_root_dir()
or fs.dirname(fs.find(ROOT_PATTERNS, {
path = buffer_path,
upward = true,
})[1])
vim.lsp.start(c)
end)

So my question is: Does flutter-tools support customization of the root directory and, if so, what is the correct way to do this?

Thank you so much for time!

Hi @Energizer328 , please take a look at the "NOTE" mentioned in the Full Configuration section. Seems like that's what you're looking for.

Thanks for the reply @Thesmader! Maybe I'm missing something, but isn't flutter_path and flutter_lookup_cmd related to the path of the flutter binaries? What I'm looking for is a way to configure the directory that the lsp analyses to find references.

@Energizer328 I missed that one. I meant the 2nd note which you can find towards the end of the Full Configuration section.

NOTE: By default this plugin excludes analysis of the packages in the flutter SDK. If for example you jump to the definition of StatelessWidget, the lsp will not try and index the 100s (maybe 1000s) of files in that directory. If for some reason you would like this behaviour set analysisExcludedFolders = {}

What I understand from that note is that it enables the analisys of the packages in the flutter SDK. That's also not what I want. Anyway, I tried it, for the sake of it, but it also didn't fix the issue.

Let me try to be more explicit about my issue: I have a complex project structure consisting of multiple flutter packages. Something like this:

<project_dir>
      |------> <app_1>
      |------> <app_2>
     ...
      |------> <app_n>

There are dependencies between the different packages, so it might be the case that <app_1> references a class defined in <app_2>. Therefore, dartls needs to search the whole <project_dir> for references. This is actually the directory opened in neovim (pwd). However, as I understand, the default way that dartls determines the directory that it searches for references (root_dir), is by searching up the directory tree until it finds a pubspec.yaml file. This doesn't work for me since every app has its own pubspec.yaml, so I need a way to change this.

Usually in lspconfig you can send root_dir as a setup parameter, but I don't know how to do it with flutter-tools (or if it is even supported at all).

commented

@Energizer328 you should be able to send any options you'd normally send to lspconfig to the dart language server using the lsp key in the config, if you have a look at how that is setup you should be able to have it behave as you like