HeaderGuard user command
This commit is contained in:
parent
4166a79b7d
commit
42493e3937
1 changed files with 29 additions and 0 deletions
|
@ -54,4 +54,33 @@ vim.api.nvim_create_user_command('WordCount', function()
|
||||||
print('Word count: ' .. words)
|
print('Word count: ' .. words)
|
||||||
end, { desc = 'Count words in current buffer' })
|
end, { desc = 'Count words in current buffer' })
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command('HeaderGuard', function()
|
||||||
|
local filename = vim.fn.expand '%:t' -- filename.ext
|
||||||
|
local stem = vim.fn.expand '%:t:r' -- filename without extension
|
||||||
|
local ext = vim.fn.expand '%:e' -- file extension
|
||||||
|
|
||||||
|
if ext ~= 'h' then
|
||||||
|
print 'HeaderGuard: Not a .h file!'
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local guard = string.upper(stem) .. '_H'
|
||||||
|
|
||||||
|
-- Insert at top
|
||||||
|
vim.api.nvim_buf_set_lines(0, 0, 0, false, {
|
||||||
|
'#ifndef ' .. guard,
|
||||||
|
'#define ' .. guard,
|
||||||
|
'',
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Insert at bottom
|
||||||
|
local last_line = vim.api.nvim_buf_line_count(0)
|
||||||
|
vim.api.nvim_buf_set_lines(0, last_line, last_line, false, {
|
||||||
|
'',
|
||||||
|
'#endif // ' .. guard,
|
||||||
|
})
|
||||||
|
|
||||||
|
print('Header guard inserted: ' .. guard)
|
||||||
|
end, { desc = 'Insert header guard for current .h file' })
|
||||||
|
|
||||||
-- vim: ts=2 sts=2 sw=2 et
|
-- vim: ts=2 sts=2 sw=2 et
|
||||||
|
|
Loading…
Add table
Reference in a new issue