diff --git a/ftplugin/dart/init.lua b/ftplugin/dart/init.lua index a5f6442..7f39379 100644 --- a/ftplugin/dart/init.lua +++ b/ftplugin/dart/init.lua @@ -3,20 +3,12 @@ if vim.b.flutter_tools_did_ftplugin then return end vim.b.flutter_tools_did_ftplugin = 1 require("flutter-tools.lsp").attach() +local path = require("flutter-tools.utils.path") vim.opt_local.comments = [[sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,:///,://]] vim.opt_local.commentstring = [[//%s]] vim.opt.includeexpr = "v:lua.require('flutter-tools.resolve_url').resolve_url(v:fname)" -local function is_nonmodifiable_path() - local path_parts = { [[.pub-cache]], [[Pub\Cache]], [[/fvm/versions/]] } - local full_path = vim.fn.expand("%:p") - if full_path then - for _, path_part in ipairs(path_parts) do - if full_path:find(path_part, nil, true) then return true end - end - end - return false -end +local full_path = vim.fn.expand("%:p") -- Prevent writes to files in the pub cache and FVM folder. -if is_nonmodifiable_path() then vim.opt_local.modifiable = false end +if path.is_flutter_dependency_path(full_path) then vim.opt_local.modifiable = false end diff --git a/lua/flutter-tools/lsp/init.lua b/lua/flutter-tools/lsp/init.lua index 6b3ab03..2f5e7dd 100644 --- a/lua/flutter-tools/lsp/init.lua +++ b/lua/flutter-tools/lsp/init.lua @@ -170,12 +170,14 @@ end function M.get_project_root_dir() local conf = require("flutter-tools.config") local current_buffer_path = path.current_buffer_path() - local root_path = lsp_utils.is_valid_path(current_buffer_path) - and path.find_root(conf.root_patterns, current_buffer_path) - or nil - - if root_path ~= nil then return root_path end - + -- Check if path is flutter dependency. For dependencies we do not + -- search for a root directory as they are not projects. + if not path.is_flutter_dependency_path(current_buffer_path) then + local root_path = lsp_utils.is_valid_path(current_buffer_path) + and path.find_root(conf.root_patterns, current_buffer_path) + or nil + if root_path ~= nil then return root_path end + end local client = lsp_utils.get_dartls_client() return client and client.config.root_dir or nil end diff --git a/lua/flutter-tools/utils/path.lua b/lua/flutter-tools/utils/path.lua index 42cef25..49bc7f6 100644 --- a/lua/flutter-tools/utils/path.lua +++ b/lua/flutter-tools/utils/path.lua @@ -139,4 +139,14 @@ function M.get_absolute_path(input_path) end end +function M.is_flutter_dependency_path(full_path) + local path_parts = { [[.pub-cache]], [[Pub\Cache]], [[/fvm/versions/]] } + if full_path then + for _, path_part in ipairs(path_parts) do + if full_path:find(path_part, nil, true) then return true end + end + end + return false +end + return M