Skip to content

Commit 6f37e83

Browse files
committed
feat: expose web-port, uses the same client if they use the same version
1 parent 728a96c commit 6f37e83

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

lua/flutter-tools/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ local config = {
137137
virtual_text_str = "",
138138
background_color = nil,
139139
},
140+
web_port = nil
140141
},
141142
outline = setmetatable({
142143
auto_open = false,

lua/flutter-tools/lsp/init.lua

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function M.get_project_root_dir()
170170
local current_buffer_path = path.current_buffer_path()
171171
local root_path = lsp_utils.is_valid_path(current_buffer_path)
172172
and path.find_root(conf.root_patterns, current_buffer_path)
173-
or nil
173+
or nil
174174

175175
if root_path ~= nil then return root_path end
176176

@@ -207,7 +207,7 @@ function M.dart_lsp_super()
207207
uri = vim.uri_from_bufnr(0), -- gets URI of current buffer
208208
},
209209
position = {
210-
line = lsp_line, -- 0-based line number
210+
line = lsp_line, -- 0-based line number
211211
character = lsp_col, -- 0-based character position
212212
},
213213
}
@@ -229,7 +229,13 @@ local function get_server_config(user_config, callback)
229229
local debug_log = create_debug_log(user_config.debug)
230230
debug_log(fmt("dart_sdk_path: %s", root_path))
231231

232-
config.cmd = config.cmd or { paths.dart_bin, "language-server", "--protocol=lsp" }
232+
local cmd = { paths.dart_bin, "language-server", "--protocol=lsp" }
233+
234+
if (config.web_port) then
235+
table.insert(cmd, "--port=" .. config.web_port)
236+
end
237+
238+
config.cmd = config.cmd or cmd
233239

234240
config.filetypes = { FILETYPE }
235241
config.capabilities = merge_config(defaults.capabilities, config.capabilities)
@@ -239,7 +245,7 @@ local function get_server_config(user_config, callback)
239245
config.commands = merge_config(defaults.commands, config.commands)
240246

241247
config.on_init = function(client, _)
242-
return client.notify("workspace/didChangeConfiguration", { settings = config.settings })
248+
return client:notify("workspace/didChangeConfiguration", { settings = config.settings })
243249
end
244250
callback(config, paths)
245251
end)
@@ -253,15 +259,33 @@ function M.attach()
253259
debug_log("attaching LSP")
254260

255261
local buf = api.nvim_get_current_buf()
262+
263+
local key = "dart_lsp_attaching"
264+
265+
local err, attaching = pcall(vim.api.nvim_buf_get_var, buf, key)
266+
if (err or attaching == true) then return end
267+
vim.api.nvim_buf_set_var(buf, key, true)
268+
256269
if lsp_utils.get_dartls_client(buf) ~= nil then return end
257270

258271
local buffer_path = api.nvim_buf_get_name(buf)
259272

260273
if not lsp_utils.is_valid_path(buffer_path) then return end
261274

262275
get_server_config(user_config, function(c, paths)
263-
c.root_dir = paths.fvm_dir or M.get_project_root_dir()
264-
vim.lsp.start(c)
276+
local project_root = M.get_project_root_dir()
277+
c.root_dir = paths.fvm_dir or project_root
278+
vim.schedule(function()
279+
local client = lsp_utils.get_dartls_client_for_version(c.cmd[1])
280+
if client == nil then
281+
local id = vim.lsp.start(c)
282+
client = vim.lsp.get_clients({ id = id })[1]
283+
if client == nil then return end
284+
end
285+
client:_add_workspace_folder(project_root)
286+
vim.lsp.buf_attach_client(buf, client.id)
287+
end)
288+
vim.api.nvim_buf_set_var(buf, key, false)
265289
end)
266290
end
267291

lua/flutter-tools/lsp/utils.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ function M.get_dartls_client(bufnr)
1717
return utils.find(clients, function(c) return not c:is_stopped() end)
1818
end
1919

20+
function M.get_dartls_server()
21+
local clients = get_clients({ name = M.SERVER_NAME })
22+
return utils.find(clients, function(c) return not c:is_stopped() end)
23+
end
24+
25+
---@param cmd string
26+
---@return vim.lsp.Client?
27+
function M.get_dartls_client_for_version(cmd)
28+
local clients = get_clients({ name = M.SERVER_NAME })
29+
return utils.find(clients, function(c)
30+
local isStopping = c:is_stopped()
31+
if isStopping then return false end
32+
local client_cmd = (c.config.cmd or {})[1]
33+
return cmd == client_cmd
34+
end)
35+
end
36+
2037
--- Checks if buffer path is valid for attaching LSP
2138
--- @param buffer_path string
2239
--- @return boolean

0 commit comments

Comments
 (0)