Skip to content

fix: add type annotations, improve code structure, autocmd groupped#2

Open
DrKJeff16 wants to merge 4 commits intoJ-Cowsert:masterfrom
DrKJeff16:patch/fixup
Open

fix: add type annotations, improve code structure, autocmd groupped#2
DrKJeff16 wants to merge 4 commits intoJ-Cowsert:masterfrom
DrKJeff16:patch/fixup

Conversation

@DrKJeff16
Copy link
Copy Markdown

Changes

  • Took the liberty to add much needed LuaLS type annotations, for easier coding.
  • Restructured some parts of the code to improve efficiency and readability.
  • Autocommand is now under a group (ClassLayout), as recommended.

The plugin works as intended, no errors have been introduced, to my knowledge.

Any feedback would be appreciated!

classlayout.mp4

Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
Comment thread lua/classlayout/init.lua
args = {},
compile_commands = true, -- auto-detect flags from compile_commands.json
}
M.augroup = vim.api.nvim_create_augroup("ClassLayout", { clear = true })
Copy link
Copy Markdown
Author

@DrKJeff16 DrKJeff16 Apr 18, 2026

Choose a reason for hiding this comment

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

Was not sure how to declare the augroup, but I decided to make it part of the module PROVISIONALLY since I'm planning to introduce another autocmd in another PR.

The aforementioned future PR will not be created unless this PR gets (partially) merged.

Comment thread lua/classlayout/init.lua
Comment on lines -4 to +23
keymap = "<leader>cl",
compiler = "clang",
args = {},
compile_commands = true, -- auto-detect flags from compile_commands.json
}
M.augroup = vim.api.nvim_create_augroup("ClassLayout", { clear = true })

--- @class ClassLayoutOpts
--- @field args? string[]
--- @field compile_commands? boolean
--- @field compiler? string
--- @field keymap? string

---@return ClassLayoutOpts defaults
local function get_defaults()
return {
keymap = "<leader>cl",
compiler = "clang",
args = {},
compile_commands = true, -- auto-detect flags from compile_commands.json
}
end
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll explain my reasoning: If you wish to have default values, they should be as immutable as possible (i.e. users shouldn't be able to tamper with it). Best solution is to make a wrapper that returns the table.

Comment thread lua/classlayout/init.lua
local dir = vim.fn.fnamemodify(start_path, ":h")
local prev = nil
while dir and dir ~= prev do
local candidate = vim.fs.joinpath(dir, "compile_commands.json")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Using vim.fs.joinpath() is a saner solution, in my opinion.

Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
Comment thread lua/classlayout/init.lua
prev = dir
dir = vim.fn.fnamemodify(dir, ":h")
end
return nil
Copy link
Copy Markdown
Author

@DrKJeff16 DrKJeff16 Apr 18, 2026

Choose a reason for hiding this comment

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

Doing return nil on its own is redundant and unnecessary. This of course doesn't apply to, say, multiple returs:

function foo()
  -- ...
  return nil, 1, "foo"
end

Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant