Skip to content

feat(api): support for configuring several filetypes in one go #18

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
feat(api): support setting formatters and linters for multiple filety…
…pes in one go

Resolves #14
But it basically doubles the code in filetype.lua, so I'm not sure if it should be merged.
  • Loading branch information
xiaoshihou514 committed Jul 18, 2023
commit c3e56dac45a60a55e895a2e1c1f23221494eff1d
43 changes: 42 additions & 1 deletion lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,49 @@ local function box()
return setmetatable({}, tbl)
end

return setmetatable(M, {

local function box_for_group(fts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very confuse on there. can we reuse exist function instead of create box_for_group lots of things are duplicated

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well yes and no.
The object returned by ft({"foo", "bar"}) would have to use a different set of logic. But the fmt, lint and append methods are really just calling the respective methods on ft("foo") and ft("bar") (in this case). I don't know if the logic could be simplified further.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the metatable related logic could be abstracted tho.

for _, ft in pairs(fts) do
if not rawget(M, ft) then
rawset(M, ft, box())
end
end
local current
local tbl = {}
tbl.__index = tbl

function tbl:fmt(config)
for _, ft in pairs(self) do
M[ft]:fmt(config)
end
current = 'format'
return self
end

function tbl:append(val)
for _, ft in pairs(self) do
local opt = M[ft][current]
opt[#opt + 1] = val
end
return self
end

function tbl:lint(config)
for _, ft in pairs(self) do
M[ft]:lint(config)
end
current = 'linter'
return self
end

return setmetatable(fts, tbl)
end

M = setmetatable(M, {
__call = function(t, ft)
if type(ft) == "table" then
return box_for_group(ft)
end
if not rawget(t, ft) then
rawset(t, ft, box())
end
Expand Down