@@ -3,11 +3,11 @@ local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.util
33local path = lazy .require (" flutter-tools.utils.path" ) --- @module " flutter-tools.utils.path"
44local color = lazy .require (" flutter-tools.lsp.color" ) --- @module " flutter-tools.lsp.color"
55local lsp_utils = lazy .require (" flutter-tools.lsp.utils" ) --- @module " flutter-tools.lsp.utils"
6+ local fvm_utils = lazy .require (" flutter-tools.lsp.fvm_utils" ) --- @module " flutter-tools.lsp.fvm_utils"
67
78local api = vim .api
89local lsp = vim .lsp
910local fmt = string.format
10- local fs = vim .fs
1111
1212local FILETYPE = " dart"
1313
@@ -166,9 +166,16 @@ function M.restart()
166166end
167167
168168--- @return string ?
169- function M .get_lsp_root_dir ()
170- local client = lsp_utils .get_dartls_client ()
171- return client and client .config .root_dir or nil
169+ function M .get_project_root_dir ()
170+ local conf = require (" flutter-tools.config" )
171+ local current_buffer_path = lsp_utils .current_buffer_path_if_valid ()
172+
173+ if (current_buffer_path == nil ) then
174+ local client = lsp_utils .get_dartls_client ()
175+ return client and client .config .root_dir or nil
176+ end
177+
178+ return path .find_root (conf .root_patterns , current_buffer_path ) or current_buffer_path
172179end
173180
174181-- FIXME: I'm not sure how to correctly wait till a server is ready before
@@ -200,7 +207,7 @@ function M.dart_lsp_super()
200207 uri = vim .uri_from_bufnr (0 ), -- gets URI of current buffer
201208 },
202209 position = {
203- line = lsp_line , -- 0-based line number
210+ line = lsp_line , -- 0-based line number
204211 character = lsp_col , -- 0-based character position
205212 },
206213 }
@@ -210,12 +217,14 @@ end
210217function M .dart_reanalyze () lsp .buf_request (0 , " dart/reanalyze" ) end
211218
212219--- @param user_config table
213- --- @param callback fun ( table )
220+ --- @param callback fun ( table , table )
214221local function get_server_config (user_config , callback )
215222 local config = utils .merge ({ name = lsp_utils .SERVER_NAME }, user_config , { " color" })
216223 local executable = require (" flutter-tools.executable" )
224+ executable .reset_paths ()
217225 --- TODO: if a user specifies a command we do not need to call executable.get
218226 executable .get (function (paths )
227+ if paths == nil then return end
219228 local defaults = get_defaults ({ flutter_sdk = paths .flutter_sdk })
220229 local root_path = paths .dart_sdk
221230 local debug_log = create_debug_log (user_config .debug )
@@ -233,20 +242,22 @@ local function get_server_config(user_config, callback)
233242 config .on_init = function (client , _ )
234243 return client .notify (" workspace/didChangeConfiguration" , { settings = config .settings })
235244 end
236- callback (config )
245+ -- TODO: flag something such that we only call attach on exit that has been flagged to
246+ -- re attach.
247+ config .on_exit = function ()
248+ if not M .pending_reattach then
249+ return
250+ end
251+ M .pending_reattach = false
252+ -- vim.schedule does not work, it executes attach too soon and
253+ -- instead of creating a new client, the lsp implementation tries
254+ -- to use the old, stopped client.
255+ vim .defer_fn (M .attach , 0 )
256+ end
257+ callback (config , paths )
237258 end )
238259end
239260
240- --- Checks if buffer path is valid for attaching LSP
241- local function is_valid_path (buffer_path )
242- if buffer_path == " " then return false end
243-
244- local start_index , _ , uri_prefix = buffer_path :find (" ^(%w+://).*" )
245- -- Do not attach LSP if file URI prefix is not file.
246- -- For example LSP will not be attached for diffview:// or fugitive:// buffers.
247- return not start_index or uri_prefix == " file://"
248- end
249-
250261--- This was heavily inspired by nvim-metals implementation of the attach functionality
251262function M .attach ()
252263 local conf = require (" flutter-tools.config" )
@@ -255,16 +266,15 @@ function M.attach()
255266 debug_log (" attaching LSP" )
256267
257268 local buf = api .nvim_get_current_buf ()
269+ if lsp_utils .get_dartls_client (buf ) ~= nil then return end
270+
258271 local buffer_path = api .nvim_buf_get_name (buf )
259272
260- if not is_valid_path (buffer_path ) then return end
273+ if not lsp_utils . is_valid_path (buffer_path ) then return end
261274
262- get_server_config (user_config , function (c )
263- c .root_dir = M .get_lsp_root_dir ()
264- or fs .dirname (fs .find (conf .root_patterns , {
265- path = buffer_path ,
266- upward = true ,
267- })[1 ])
275+ get_server_config (user_config , function (c , paths )
276+ c .root_dir = paths .fvm_dir
277+ or M .get_project_root_dir ()
268278 vim .lsp .start (c )
269279 end )
270280end
0 commit comments