@@ -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 )
266290end
267291
0 commit comments