Skip to content

Commit c3bd63b

Browse files
committed
fix: invalid dependency section warnings
1 parent 403a0ab commit c3bd63b

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

lua/crates/toml.lua

+55-52
Original file line numberDiff line numberDiff line change
@@ -267,72 +267,76 @@ end
267267
function M.parse_section(text, line_nr, header_col)
268268
---@type string, integer, string
269269
local prefix, suffix_s, suffix = text:match("^(.*)dependencies()(.*)$")
270-
if prefix and suffix then
271-
prefix = vim.trim(prefix)
272-
suffix = vim.trim(suffix)
273-
---@type TomlSection
274-
local section = {
275-
text = text,
276-
invalid = false,
277-
kind = TomlSectionKind.DEFAULT,
278-
---end bound is assigned when the section ends
279-
---@diagnostic disable-next-line: param-type-mismatch
280-
lines = Span.new(line_nr, nil),
281-
header_col = header_col,
282-
}
270+
if not (prefix and suffix) then
271+
return nil
272+
end
283273

284-
local target = prefix
274+
prefix = vim.trim(prefix)
275+
suffix = vim.trim(suffix)
276+
277+
---@type TomlSection
278+
local section = {
279+
text = text,
280+
invalid = false,
281+
kind = TomlSectionKind.DEFAULT,
282+
---end bound is assigned when the section ends
283+
---@diagnostic disable-next-line: param-type-mismatch
284+
lines = Span.new(line_nr, nil),
285+
header_col = header_col,
286+
}
285287

286-
local dev_target = prefix:match("^(.*)dev%-$")
287-
if dev_target then
288-
target = vim.trim(dev_target)
289-
section.kind = TomlSectionKind.DEV
290-
end
288+
local target = prefix
291289

292-
local build_target = prefix:match("^(.*)build%-$")
293-
if build_target then
294-
target = vim.trim(build_target)
295-
section.kind = TomlSectionKind.BUILD
296-
end
290+
local dev_target = prefix:match("^(.*)dev%-$")
291+
if dev_target then
292+
target = vim.trim(dev_target)
293+
section.kind = TomlSectionKind.DEV
294+
end
297295

298-
local workspace_target = target:match("^(.*)workspace%s*%.$")
299-
if workspace_target then
300-
section.workspace = true
301-
target = vim.trim(workspace_target)
302-
end
296+
local build_target = prefix:match("^(.*)build%-$")
297+
if build_target then
298+
target = vim.trim(build_target)
299+
section.kind = TomlSectionKind.BUILD
300+
end
303301

304-
if target then
305-
local t = target:match("^target%s*%.(.+)%.$")
306-
if t then
307-
section.target = vim.trim(t)
308-
target = ""
309-
end
310-
end
302+
local workspace_target = target:match("^(.*)workspace%s*%.$")
303+
if workspace_target then
304+
section.workspace = true
305+
target = vim.trim(workspace_target)
306+
end
311307

312-
if suffix then
313-
local n_s, n, n_e = suffix:match("^%.%s*()(.+)()%s*$")
314-
if n then
315-
section.name = vim.trim(n)
316-
local offset = header_col.s + 1 + suffix_s - 1
317-
section.name_col = Span.new(n_s - 1 + offset, n_e - 1 + offset)
318-
suffix = ""
319-
end
308+
if target ~= "" then
309+
local t = target:match("^target%s*%.(.+)%.$")
310+
if t then
311+
section.target = vim.trim(t)
312+
target = ""
313+
else
314+
-- not a depndency section
315+
return nil
320316
end
317+
end
321318

322-
section.invalid = (target ~= "" or suffix ~= "")
323-
or (section.workspace and section.kind ~= TomlSectionKind.DEFAULT)
324-
or (section.workspace and section.target ~= nil)
325-
326-
return Section.new(section)
319+
if suffix then
320+
local n_s, n, n_e = suffix:match("^%.%s*()(.+)()%s*$")
321+
if n then
322+
section.name = vim.trim(n)
323+
local offset = header_col.s + 1 + suffix_s - 1
324+
section.name_col = Span.new(n_s - 1 + offset, n_e - 1 + offset)
325+
suffix = ""
326+
end
327327
end
328328

329-
return nil
329+
section.invalid = (suffix ~= "")
330+
or (section.workspace and section.kind ~= TomlSectionKind.DEFAULT)
331+
or (section.workspace and section.target ~= nil)
332+
333+
return Section.new(section)
330334
end
331335

332336
---@param name string
333337
---@return string
334338
local function table_bool_pattern(name)
335-
return "^%s*".. name .. "%s*=%s*()([^%s]*)()%s*$"
339+
return "^%s*" .. name .. "%s*=%s*()([^%s]*)()%s*$"
336340
end
337341

338342
---@param name string
@@ -677,7 +681,6 @@ function M.parse_crates(buf)
677681
dep_section_crate = dep_section_crate or empty_crate
678682
dep_section_crate.feat = feat
679683
end
680-
681684
elseif dep_section then
682685
local crate = M.parse_inline_crate(line, line_nr)
683686
if crate then

0 commit comments

Comments
 (0)