Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't show errors for workspace dependencies #72

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lua/crates/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ function M.process_api_crate(crate, api_crate)
}
local diagnostics = {}

if crate.path then
if crate.workspace then
info.dep_kind = "workspace"
elseif crate.path then
info.dep_kind = "path"
elseif crate.git then
info.dep_kind = "git"
Expand Down
2 changes: 1 addition & 1 deletion lua/crates/popup/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ local function toggle_feature(ctx, line)
local text = vim.api.nvim_buf_get_lines(ctx.buf, line_nr, line_nr + 1, false)[1]
text = toml.trim_comments(text)

local crate = toml.Crate.new(toml.parse_crate(text, line_nr))
local crate = toml.Crate.new(toml.parse_inline_crate(text, line_nr))
ctx.crate.syntax = crate.syntax
ctx.crate.vers = crate.vers
ctx.crate.feat = crate.feat
Expand Down
2 changes: 1 addition & 1 deletion lua/crates/popup/versions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local function select_version(ctx, line, alt)
local text = vim.api.nvim_buf_get_lines(ctx.buf, line_nr, line_nr + 1, false)[1]
text = toml.trim_comments(text)

local c = toml.parse_crate(text, line_nr)
local c = toml.parse_inline_crate(text, line_nr)
if c and c.vers then
crate.vers = crate.vers or c.vers
crate.vers.line = line_nr
Expand Down
134 changes: 87 additions & 47 deletions lua/crates/toml.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
local M = {Section = {}, Crate = {Vers = {}, Path = {}, Git = {}, Pkg = {}, Def = {}, Feat = {}, }, Feature = {}, Quotes = {}, }
local M = {Section = {}, Crate = {Vers = {}, Path = {}, Git = {}, Pkg = {}, Workspace = {}, Def = {}, Feat = {}, }, Feature = {}, Quotes = {}, }












Expand Down Expand Up @@ -111,25 +120,6 @@ local types = require("crates.types")
local Range = types.Range
local Requirement = types.Requirement

local function inline_table_bool_pattern(name)
return "^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*" .. name .. "%s*=%s*()([^%s,}]*)()%s*()[,]?.*[}]?%s*$"
end

local function inline_table_str_pattern(name)
return [[^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*]] .. name .. [[%s*=%s*(["'])()([^"']*)()(["']?)%s*()[,]?.*[}]?%s*$]]
end

local function inline_table_str_array_pattern(name)
return "^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*" .. name .. "%s*=%s*%[()([^%]]*)()[%]]?%s*()[,]?.*[}]?%s*$"
end

local INLINE_TABLE_VERS_PATTERN = inline_table_str_pattern("version")
local INLINE_TABLE_PATH_PATTERN = inline_table_str_pattern("path")
local INLINE_TABLE_GIT_PATTERN = inline_table_str_pattern("git")
local INLINE_TABLE_PKG_PATTERN = inline_table_str_pattern("package")
local INLINE_TABLE_FEAT_PATTERN = inline_table_str_array_pattern("features")
local INLINE_TABLE_DEF_PATTERN = inline_table_bool_pattern("default[_-]features")

function M.parse_crate_features(text)
local feats = {}
for fds, qs, fs, f, fe, qe, fde, c in text:gmatch([[[,]?()%s*(["'])()([^,"']*)()(["']?)%s*()([,]?)]]) do
Expand Down Expand Up @@ -163,6 +153,9 @@ function Crate.new(obj)
if obj.def then
obj.def.enabled = obj.def.text ~= "false"
end
if obj.workspace then
obj.workspace.enabled = obj.workspace.text ~= "false"
end

return setmetatable(obj, { __index = Crate })
end
Expand Down Expand Up @@ -197,6 +190,10 @@ function Crate:is_def_enabled()
return not self.def or self.def.enabled
end

function Crate:is_workspace()
return not self.workspace or self.workspace.enabled
end

function Crate:package()
return self.pkg and self.pkg.text or self.explicit_name
end
Expand Down Expand Up @@ -271,6 +268,7 @@ function M.parse_section(text, start)
return nil
end


local function parse_crate_table_str(line, line_nr, pattern)
local quote_s, str_s, text, str_e, quote_e = line:match(pattern)
if text then
Expand All @@ -286,6 +284,20 @@ local function parse_crate_table_str(line, line_nr, pattern)
return nil
end

local function parse_crate_table_bool(line, line_nr, pattern)
local bool_s, text, bool_e = line:match(pattern)
if text then
return {
text = text,
line = line_nr,
col = Range.new(bool_s - 1, bool_e - 1),
decl_col = Range.new(0, line:len()),
}
end

return nil
end

function M.parse_crate_table_vers(line, line_nr)
local pat = [[^%s*version%s*=%s*(["'])()([^"']*)()(["']?)%s*$]]
return parse_crate_table_str(line, line_nr, pat)
Expand All @@ -306,6 +318,16 @@ function M.parse_crate_table_pkg(line, line_nr)
return parse_crate_table_str(line, line_nr, pat)
end

function M.parse_crate_table_def(line, line_nr)
local pat = "^%s*default[_-]features%s*=%s*()([^%s]*)()%s*$"
return parse_crate_table_bool(line, line_nr, pat)
end

function M.parse_crate_table_workspace(line, line_nr)
local pat = "^%s*workspace%s*=%s*()([^%s]*)()%s*$"
return parse_crate_table_bool(line, line_nr, pat)
end

function M.parse_crate_table_feat(line, line_nr)
local array_s, text, array_e = line:match("%s*features%s*=%s*%[()([^%]]*)()[%]]?%s*$")
if text then
Expand All @@ -320,20 +342,27 @@ function M.parse_crate_table_feat(line, line_nr)
return nil
end

function M.parse_crate_table_def(line, line_nr)
local bool_s, text, bool_e = line:match("^%s*default[_-]features%s*=%s*()([^%s]*)()%s*$")
if text then
return {
text = text,
line = line_nr,
col = Range.new(bool_s - 1, bool_e - 1),
decl_col = Range.new(0, line:len()),
}
end

return nil
local function inline_table_bool_pattern(name)
return "^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*" .. name .. "%s*=%s*()([^%s,}]*)()%s*()[,]?.*[}]?%s*$"
end

local function inline_table_str_pattern(name)
return [[^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*]] .. name .. [[%s*=%s*(["'])()([^"']*)()(["']?)%s*()[,]?.*[}]?%s*$]]
end

local function inline_table_str_array_pattern(name)
return "^%s*()([^%s]+)()%s*=%s*{.-[,]?()%s*" .. name .. "%s*=%s*%[()([^%]]*)()[%]]?%s*()[,]?.*[}]?%s*$"
end

local INLINE_TABLE_VERS_PATTERN = inline_table_str_pattern("version")
local INLINE_TABLE_PATH_PATTERN = inline_table_str_pattern("path")
local INLINE_TABLE_GIT_PATTERN = inline_table_str_pattern("git")
local INLINE_TABLE_PKG_PATTERN = inline_table_str_pattern("package")
local INLINE_TABLE_FEAT_PATTERN = inline_table_str_array_pattern("features")
local INLINE_TABLE_DEF_PATTERN = inline_table_bool_pattern("default[_-]features")
local INLINE_TABLE_WORKSPACE_PATTERN = inline_table_bool_pattern("workspace")

local function parse_inline_table_str(crate, line, line_nr, entry, pattern)
local name_s, name, name_e, decl_s, quote_s, str_s, text, str_e, quote_e, decl_e = line:match(pattern)
if name then
Expand All @@ -351,7 +380,23 @@ local function parse_inline_table_str(crate, line, line_nr, entry, pattern)
end
end

function M.parse_crate(line, line_nr)
local function parse_inline_table_bool(crate, line, line_nr, entry, pattern)
local name_s, name, name_e, decl_s, str_s, text, str_e, decl_e = line:match(pattern)
if name then
crate.explicit_name = name
crate.explicit_name_col = Range.new(name_s - 1, name_e - 1)
do
(crate)[entry] = {
text = text,
line = line_nr,
col = Range.new(str_s - 1, str_e - 1),
decl_col = Range.new(decl_s - 1, decl_e - 1),
}
end
end
end

function M.parse_inline_crate(line, line_nr)

do
local name_s, name, name_e, quote_s, str_s, text, str_e, quote_e = line:match([[^%s*()([^%s]+)()%s*=%s*(["'])()([^"']*)()(["']?)%s*$]])
Expand Down Expand Up @@ -383,6 +428,9 @@ function M.parse_crate(line, line_nr)
parse_inline_table_str(crate, line, line_nr, "git", INLINE_TABLE_GIT_PATTERN)
parse_inline_table_str(crate, line, line_nr, "pkg", INLINE_TABLE_PKG_PATTERN)

parse_inline_table_bool(crate, line, line_nr, "def", INLINE_TABLE_DEF_PATTERN)
parse_inline_table_bool(crate, line, line_nr, "workspace", INLINE_TABLE_WORKSPACE_PATTERN)

do
local name_s, name, name_e, decl_s, array_s, text, array_e, decl_e = line:match(INLINE_TABLE_FEAT_PATTERN)
if name then
Expand All @@ -397,20 +445,6 @@ function M.parse_crate(line, line_nr)
end
end

do
local name_s, name, name_e, decl_s, bool_s, text, bool_e, decl_e = line:match(INLINE_TABLE_DEF_PATTERN)
if name then
crate.explicit_name = name
crate.explicit_name_col = Range.new(name_s - 1, name_e - 1)
crate.def = {
text = text,
line = line_nr,
col = Range.new(bool_s - 1, bool_e - 1),
decl_col = Range.new(decl_s - 1, decl_e - 1),
}
end
end

if crate.explicit_name then
return crate
end
Expand Down Expand Up @@ -493,6 +527,12 @@ function M.parse_crates(buf)
dep_section_crate.pkg = pkg
end

local workspace = M.parse_crate_table_workspace(line, line_nr)
if workspace then
dep_section_crate = dep_section_crate or empty_crate
dep_section_crate.workspace = workspace
end

local feat = M.parse_crate_table_feat(line, line_nr)
if feat then
dep_section_crate = dep_section_crate or empty_crate
Expand All @@ -505,7 +545,7 @@ function M.parse_crates(buf)
dep_section_crate.def = def
end
elseif dep_section then
local crate = M.parse_crate(line, line_nr)
local crate = M.parse_inline_crate(line, line_nr)
if crate then
crate.section = dep_section
table.insert(crates, Crate.new(crate))
Expand Down
1 change: 1 addition & 0 deletions lua/crates/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ local M = {CrateInfo = {}, Diagnostic = {}, Crate = {}, Version = {}, Features =






local Diagnostic = M.Diagnostic
Expand Down
4 changes: 3 additions & 1 deletion teal/crates/diagnostic.tl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ function M.process_api_crate(crate: toml.Crate, api_crate: Crate): CrateInfo, {D
}
local diagnostics = {}

if crate.path then
if crate.workspace then
info.dep_kind = "workspace"
elseif crate.path then
info.dep_kind = "path"
elseif crate.git then
info.dep_kind = "git"
Expand Down
2 changes: 1 addition & 1 deletion teal/crates/popup/features.tl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ local function toggle_feature(ctx: FeatureContext, line: integer)
local text = vim.api.nvim_buf_get_lines(ctx.buf, line_nr, line_nr + 1, false)[1]
text = toml.trim_comments(text)

local crate = toml.Crate.new(toml.parse_crate(text, line_nr))
local crate = toml.Crate.new(toml.parse_inline_crate(text, line_nr))
ctx.crate.syntax = crate.syntax
ctx.crate.vers = crate.vers
ctx.crate.feat = crate.feat
Expand Down
2 changes: 1 addition & 1 deletion teal/crates/popup/versions.tl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local function select_version(ctx: VersContext, line: integer, alt: boolean|nil)
local text = vim.api.nvim_buf_get_lines(ctx.buf, line_nr, line_nr + 1, false)[1]
text = toml.trim_comments(text)

local c = toml.parse_crate(text, line_nr)
local c = toml.parse_inline_crate(text, line_nr)
if c and c.vers then
crate.vers = crate.vers or c.vers
crate.vers.line = line_nr
Expand Down
Loading
Loading