diff --git a/README.md b/README.md index 6230165..b6d1f82 100644 --- a/README.md +++ b/README.md @@ -318,15 +318,12 @@ vim.api.nvim_create_autocmd("LspAttach", { ``` **NOTE:** -By default this plugin excludes analysis of the packages in the flutter SDK. If for example +By default this plugin excludes analysis of the packages in the flutter SDK and in the pub cache +(`$PUB_CACHE`, or `~/.pub-cache` / `%LOCALAPPDATA%\Pub\Cache` when it is unset). 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 = {}` You cannot/should not edit the files in the sdk directly so diagnostic analysis of these file is pointless. -**Exclude Note for Windows:** -To ignore packages installed with pub, consider adding `vim.fn.expand("$HOME/AppData/Local/Pub/Cache")` to -`analysisExcludedFolders` if you are using PowerShell. - #### Project Configuration It is possible to configure how each project is run using neovim's `exrc` functionality (see `:help exrc`). diff --git a/lua/flutter-tools/lsp/init.lua b/lua/flutter-tools/lsp/init.lua index 7132b0c..a935c85 100644 --- a/lua/flutter-tools/lsp/init.lua +++ b/lua/flutter-tools/lsp/init.lua @@ -96,7 +96,8 @@ end ---@param opts table ---@return table local function get_defaults(opts) - local flutter_sdk_path = opts.flutter_sdk + local flutter_sdk_path = opts.flutter_sdk and vim.uv.fs_realpath(opts.flutter_sdk) + or opts.flutter_sdk local config = { init_options = { onlyAnalyzeProjectsWithOpenFiles = true, @@ -112,6 +113,7 @@ local function get_defaults(opts) analysisExcludedFolders = { path.join(flutter_sdk_path, "packages"), path.join(flutter_sdk_path, ".pub-cache"), + path.pub_cache_dir(), }, updateImportsOnRename = true, }, diff --git a/lua/flutter-tools/utils/path.lua b/lua/flutter-tools/utils/path.lua index 1f5d226..7c6192f 100644 --- a/lua/flutter-tools/utils/path.lua +++ b/lua/flutter-tools/utils/path.lua @@ -190,6 +190,16 @@ function M.get_absolute_path(input_path) end end +---Directory where pub stores downloaded packages +---@return string? +function M.pub_cache_dir() + if vim.env.PUB_CACHE and vim.env.PUB_CACHE ~= "" then return vim.env.PUB_CACHE end + if M.is_windows then + return vim.env.LOCALAPPDATA and M.join(vim.env.LOCALAPPDATA, "Pub", "Cache") + end + return vim.env.HOME and M.join(vim.env.HOME, ".pub-cache") +end + function M.is_flutter_dependency_path(full_path) local path_parts = { [[.pub-cache]], [[Pub\Cache]], [[/fvm/versions/]] } if full_path then diff --git a/tests/path_spec.lua b/tests/path_spec.lua index 731dfa0..12e0e38 100644 --- a/tests/path_spec.lua +++ b/tests/path_spec.lua @@ -81,3 +81,31 @@ describe("path.find_root", function() assert.are.equal(workspace_root, path.find_root(patterns, file_path)) end) end) + +describe("path.pub_cache_dir", function() + local original_pub_cache + local original_home + + before_each(function() + original_pub_cache = vim.env.PUB_CACHE + original_home = vim.env.HOME + end) + + after_each(function() + vim.env.PUB_CACHE = original_pub_cache + vim.env.HOME = original_home + end) + + it("should use PUB_CACHE when it is set", function() + vim.env.PUB_CACHE = "/custom/pub-cache" + assert.are.equal("/custom/pub-cache", path.pub_cache_dir()) + end) + + it("should fall back to the default location when PUB_CACHE is unset", function() + vim.env.PUB_CACHE = nil + vim.env.HOME = "/home/user" + local expected = path.is_windows and path.join(vim.env.LOCALAPPDATA, "Pub", "Cache") + or "/home/user/.pub-cache" + assert.are.equal(expected, path.pub_cache_dir()) + end) +end)