Skip to content

Commit 65b7399

Browse files
authored
fix: do not start new LSP client when navigating to flutter dependency file (#483)
1 parent 3a3f6f5 commit 65b7399

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

ftplugin/dart/init.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@ if vim.b.flutter_tools_did_ftplugin then return end
33
vim.b.flutter_tools_did_ftplugin = 1
44

55
require("flutter-tools.lsp").attach()
6+
local path = require("flutter-tools.utils.path")
67

78
vim.opt_local.comments = [[sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,:///,://]]
89
vim.opt_local.commentstring = [[//%s]]
910
vim.opt.includeexpr = "v:lua.require('flutter-tools.resolve_url').resolve_url(v:fname)"
1011

11-
local function is_nonmodifiable_path()
12-
local path_parts = { [[.pub-cache]], [[Pub\Cache]], [[/fvm/versions/]] }
13-
local full_path = vim.fn.expand("%:p")
14-
if full_path then
15-
for _, path_part in ipairs(path_parts) do
16-
if full_path:find(path_part, nil, true) then return true end
17-
end
18-
end
19-
return false
20-
end
12+
local full_path = vim.fn.expand("%:p")
2113
-- Prevent writes to files in the pub cache and FVM folder.
22-
if is_nonmodifiable_path() then vim.opt_local.modifiable = false end
14+
if path.is_flutter_dependency_path(full_path) then vim.opt_local.modifiable = false end

lua/flutter-tools/lsp/init.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ end
170170
function M.get_project_root_dir()
171171
local conf = require("flutter-tools.config")
172172
local current_buffer_path = path.current_buffer_path()
173-
local root_path = lsp_utils.is_valid_path(current_buffer_path)
174-
and path.find_root(conf.root_patterns, current_buffer_path)
175-
or nil
176-
177-
if root_path ~= nil then return root_path end
178-
173+
-- Check if path is flutter dependency. For dependencies we do not
174+
-- search for a root directory as they are not projects.
175+
if not path.is_flutter_dependency_path(current_buffer_path) then
176+
local root_path = lsp_utils.is_valid_path(current_buffer_path)
177+
and path.find_root(conf.root_patterns, current_buffer_path)
178+
or nil
179+
if root_path ~= nil then return root_path end
180+
end
179181
local client = lsp_utils.get_dartls_client()
180182
return client and client.config.root_dir or nil
181183
end

lua/flutter-tools/utils/path.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ function M.get_absolute_path(input_path)
139139
end
140140
end
141141

142+
function M.is_flutter_dependency_path(full_path)
143+
local path_parts = { [[.pub-cache]], [[Pub\Cache]], [[/fvm/versions/]] }
144+
if full_path then
145+
for _, path_part in ipairs(path_parts) do
146+
if full_path:find(path_part, nil, true) then return true end
147+
end
148+
end
149+
return false
150+
end
151+
142152
return M

0 commit comments

Comments
 (0)