Skip to content

Commit 335c198

Browse files
committed
chore: small cleanup of unused stuff and add some types
1 parent 72972ab commit 335c198

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

lua/metals.lua

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737

3838
M.analyze_stacktrace = function()
3939
local trace = fn.getreg("*")
40-
if trace:len() > 0 then
40+
if trace and trace:len() > 0 then
4141
execute_command({ command = "metals.analyze-stacktrace", arguments = { trace } })
4242
else
4343
log.warn_and_show("No text found in your register.")
@@ -126,28 +126,30 @@ end
126126
-- floating window.
127127
M.info = function()
128128
local config = conf.get_config_cache()
129-
if not util.has_bins(conf.metals_bin()) and config.settings.metals.useGlobalExecutable then
129+
if not util.has_bins(conf.metals_bin()) and config and config.settings.metals.useGlobalExecutable then
130130
log.error_and_show(messages.use_global_set_but_cant_find)
131-
elseif not util.has_bins(conf.metals_bin()) and config.settings.metals.metalsBinaryPath then
131+
elseif not util.has_bins(conf.metals_bin()) and config and config.settings.metals.metalsBinaryPath then
132132
log.error_and_show(messages.binary_path_set_but_cant_find)
133133
elseif not util.has_bins(conf.metals_bin()) then
134134
log.warn_and_show(messages.metals_not_installed)
135135
else
136-
local metals_info = fn.system(conf.metals_bin() .. " --version")
137-
138136
local output = {}
139-
for s in metals_info:gmatch("[^\r\n]+") do
140-
-- A little hacky but the version output is weird and we want to coerce
141-
-- it to markdown, so we give the verstion line a # and then strip the
142-
-- other lines of their #
143-
if util.starts_with(s, "#") then
144-
table.insert(output, s:sub(2))
145-
else
146-
table.insert(output, "# " .. s)
137+
138+
local metals_info = fn.system(conf.metals_bin() .. " --version")
139+
if metals_info then
140+
for s in metals_info:gmatch("[^\r\n]+") do
141+
-- A little hacky but the version output is weird and we want to coerce
142+
-- it to markdown, so we give the verstion line a # and then strip the
143+
-- other lines of their #
144+
if util.starts_with(s, "#") then
145+
table.insert(output, s:sub(2))
146+
else
147+
table.insert(output, "# " .. s)
148+
end
147149
end
148150
end
149151

150-
if config.settings.metals then
152+
if config and config.settings.metals then
151153
table.insert(output, "")
152154
table.insert(output, "## Current settings")
153155
table.insert(output, "```json")
@@ -161,7 +163,7 @@ M.info = function()
161163
table.insert(output, string.format(" - nvim-metals log file: %s", log.nvim_metals_log))
162164
table.insert(output, string.format(" - nvim lsp log file: %s", lsp.get_log_path()))
163165
local loc_msg = " - metals install location:"
164-
if config.settings.metals.useGlobalExecutable then
166+
if config and config.settings.metals.useGlobalExecutable then
165167
table.insert(output, string.format("%s %s", loc_msg, "Using metals executable on $PATH"))
166168
else
167169
table.insert(output, string.format("%s %s", loc_msg, conf.metals_bin()))
@@ -271,31 +273,35 @@ M.did_focus = function()
271273
end
272274

273275
M.find_in_dependency_jars = function()
274-
local function send_request(mask, query)
275-
lsp.buf_request(util.find_metals_buffer(), "metals/findTextInDependencyJars", {
276-
options = { include = mask },
277-
query = { pattern = query },
278-
})
279-
end
276+
local metals_buf = util.find_metals_buffer()
277+
278+
if metals_buf then
279+
local function send_request(mask, query)
280+
lsp.buf_request(metals_buf, "metals/findTextInDependencyJars", {
281+
options = { include = mask },
282+
query = { pattern = query },
283+
})
284+
end
285+
286+
local function get_query_and_send(mask)
287+
vim.ui.input({
288+
prompt = "Query: ",
289+
}, function(query)
290+
if query ~= nil then
291+
send_request(mask, query)
292+
end
293+
end)
294+
end
280295

281-
local function get_query_and_send(mask)
282296
vim.ui.input({
283-
prompt = "Query: ",
284-
}, function(query)
285-
if query ~= nil then
286-
send_request(mask, query)
297+
prompt = "File mask: ",
298+
default = ".conf",
299+
}, function(mask)
300+
if mask ~= nil then
301+
get_query_and_send(mask)
287302
end
288303
end)
289304
end
290-
291-
vim.ui.input({
292-
prompt = "File mask: ",
293-
default = ".conf",
294-
}, function(mask)
295-
if mask ~= nil then
296-
get_query_and_send(mask)
297-
end
298-
end)
299305
end
300306

301307
M.organize_imports = function()

lua/metals/util.lua

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ M.has_bins = function(...)
1515
return true
1616
end
1717

18-
-- Checks to see if a user given table is defined. If so, merge it with a default.
19-
-- If not, just return the default table. This function favors the userTable on merge.
20-
-- @param defaultTable The default table to return or merge
21-
-- @param userTable The user defined table to check if exists and then merge
22-
-- @return a new table that is either the default or merged with the user one.
18+
--- Checks to see if a user given table is defined. If so, merge it with a default.
19+
--- If not, just return the default table. This function favors the userTable on merge.
20+
--- @param defaultTable table The default table to return or merge
21+
--- @param userTable table The user defined table to check if exists and then merge
22+
--- @return table that is either the default or merged with the user one.
2323
M.check_exists_and_merge = function(defaultTable, userTable)
2424
-- TODO should we add another check in here to ensure that a key that a user
2525
-- is trying to set actually exists in the default table?
@@ -30,21 +30,14 @@ M.check_exists_and_merge = function(defaultTable, userTable)
3030
end
3131
end
3232

33-
-- Location of any files or executables that nvim-metals will create on your system
33+
--- Location of any files or executables that nvim-metals will create on your system
34+
---@type string
3435
M.nvim_metals_cache_dir = Path.new(vim.fn.stdpath("cache"), "nvim-metals")
3536

36-
--- Strip the leading and trailing spaces of a string
37-
--- @param s string the string you want to trim.
38-
M.full_trim = function(s)
39-
return (s:gsub("^%s*(.-)%s*$", "%1"))
40-
end
41-
42-
--- Strip trailing whites and trailing empty lines
43-
--- @param s string the string you want to trim.
44-
M.trim_end = function(s)
45-
return string.gsub(s, "[ \t]+%f[\r\n%z]", "")
46-
end
47-
37+
--- Given a delimter split a string into a table.
38+
---@param s string
39+
---@param delimiter string
40+
---@return table of split results
4841
M.split_on = function(s, delimiter)
4942
local result = {}
5043
local from = 1
@@ -105,6 +98,7 @@ end
10598

10699
M.is_windows = vim.loop.os_uname().version:match("Windows")
107100

101+
---@return integer|nil
108102
M.find_metals_buffer = function()
109103
local metals_buf = nil
110104
local bufs = api.nvim_list_bufs()

0 commit comments

Comments
 (0)