Skip to content

Commit 818ad42

Browse files
authored
* Improved automatic opening of Devtools in browser (#415)
* Made function 'start_browser' in module 'devtools' public, so that the user may call the function on their own
1 parent 824faf5 commit 818ad42

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

lua/flutter-tools/dev_tools.lua

+26-13
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,26 @@ local function try_get_profiler_url_chrome(data)
4141
return data:match("(ws%:%/%/127%.0%.0%.1%:%d+/.+/ws)$")
4242
end
4343

44-
local function start_browser()
44+
function M.start_browser()
4545
local auto_open_browser = config.dev_tools.auto_open_browser
4646
if not auto_open_browser then return end
47+
4748
local url = M.get_profiler_url()
49+
if not url then return end
50+
4851
local open_command = utils.open_command()
4952
if not open_command then
5053
return ui.notify(
5154
"Sorry your Operating System is not supported, please raise an issue",
5255
ui.ERROR
5356
)
5457
end
55-
if url and open_command then vim.fn.jobstart({ open_command, url }, { detach = true }) end
58+
59+
Job:new({
60+
command = open_command,
61+
args = { url },
62+
detached = true
63+
}):start()
5664
end
5765

5866
function M.handle_log(data)
@@ -84,7 +92,7 @@ function M.register_profiler_url(url)
8492
end
8593

8694
function M.handle_devtools_available()
87-
start_browser()
95+
M.start_browser()
8896
ui.notify("Detected devtools url, execute FlutterCopyProfilerUrl to copy it")
8997
end
9098

@@ -103,16 +111,21 @@ end
103111
---@param data string
104112
---@param _ Job
105113
local function handle_start(_, data, _)
106-
if #data > 0 then
107-
local json = fn.json_decode(data)
108-
if json and json.params then
109-
devtools_pid = json.params.pid
110-
devtools_url = string.format("http://%s:%s", json.params.host, json.params.port)
111-
start_browser()
112-
local msg = string.format("Serving DevTools at %s", devtools_url)
113-
ui.notify(msg, ui.INFO, { timeout = 20000 })
114-
end
115-
end
114+
if #data <= 0 then return end
115+
116+
local json = fn.json_decode(data)
117+
if not json or not json.params then return end
118+
119+
devtools_pid = json.params.pid
120+
if not json.params.host or not json.params.port then return end
121+
122+
devtools_url = string.format("http://%s:%s", json.params.host, json.params.port)
123+
M.start_browser()
124+
ui.notify(
125+
string.format("Serving DevTools at %s", devtools_url),
126+
ui.INFO,
127+
{ timeout = 10000 }
128+
)
116129
end
117130

118131
---Handler errors whilst opening dev tools

0 commit comments

Comments
 (0)