1
0
This commit is contained in:
Rokas Puzonas 2023-05-11 17:31:02 +03:00
commit b6a5589d99
6 changed files with 122 additions and 1 deletions

View File

@ -27,6 +27,9 @@ require("options")
require("bindings")
require("pludin-dev")
require("add-guard")
require("cmacro-align")
-- Misc features
require("highlight-yank")

32
lua/add-guard.lua Normal file
View File

@ -0,0 +1,32 @@
vim.api.nvim_create_user_command("AddIncludeGuard", function(data)
local bufnr = vim.api.nvim_get_current_buf()
local name_pattern = data.fargs[1] or "%s_"
local function formatName(filename)
local parts = vim.split(filename, "[/\\]", {trimempty=true})
local last_part = parts[#parts]
local ext = last_part:match("%.([^%.]+)$")
local name = last_part:gsub("%.[^%.]+$", "")
if ext == "h" or ext == "hpp" then
name = name.."_H"
end
return name_pattern:format(name:upper())
end
local buf_filename = vim.api.nvim_buf_get_name(bufnr)
local guard_name = formatName(buf_filename)
vim.api.nvim_buf_set_lines(bufnr, 0, 0, false, {
"#ifndef "..guard_name,
"#define "..guard_name,
""
})
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, {
"",
"#endif //"..guard_name
})
end, { nargs="?" })

72
lua/cmacro-align.lua Normal file
View File

@ -0,0 +1,72 @@
local ts = vim.treesitter
local query = ts.parse_query("c", "(preproc_function_def value: (preproc_arg) @macro_def)")
local function get_ast_root(bufnr)
local tree = ts.get_parser(bufnr, "c"):parse()
return tree[1]:root()
end
local capture_lookup = {}
for id, name in ipairs(query.captures) do
capture_lookup[name] = id
end
local function formatMacros()
local bufnr = vim.api.nvim_get_current_buf()
local winnr = vim.api.nvim_get_current_win()
for _, match in query:iter_matches(get_ast_root(bufnr), bufnr, 0, -1) do
local macro_def_node = match[capture_lookup.macro_def]
-- local class_name = ts.get_node_text(class_node, bufnr)
local start_line = macro_def_node:start()
local end_line = macro_def_node:end_()
local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line+1, false)
local tabstop = tonumber(vim.bo[bufnr].tabstop) or 8
for i, line in ipairs(lines) do
lines[i] = line:match("^(.-)%s*[\\]?$")
end
local line_length = 0
do
local textwidth = vim.bo[bufnr].textwidth
if textwidth and textwidth ~= "" then
line_length = tonumber(textwidth)
end
if line_length <= 0 then
local colorcolum = vim.wo[winnr].colorcolumn
if colorcolum and colorcolum ~= "" then
line_length = tonumber(colorcolum)
end
end
if line_length <= 0 then
for _, line in ipairs(lines) do
line_length = math.max(line_length, #line+2)
end
end
end
for i = 1, #lines-1 do
local line = lines[i]
local length = #(line:gsub("\t", (" "):rep(tabstop)))
lines[i] = line .. (" "):rep(line_length - length-2) .. " \\"
end
vim.api.nvim_buf_set_lines(bufnr, start_line, end_line+1, false, lines)
end
end
local group = vim.api.nvim_create_augroup("UpdateCMacro", { clear = true })
for _, cmd in ipairs{"InsertLeavePre", "BufWritePre"} do
vim.api.nvim_create_autocmd(cmd, {
group = group,
pattern = {"*.c", "*.h", "*.cpp", "*.hpp", "*.cc"},
callback = function (data)
formatMacros()
end
})
end

View File

@ -0,0 +1,3 @@
require('toggleterm').setup()
vim.keymap.set("n", "<leader>t", ":ToggleTerm<CR>")

View File

@ -104,7 +104,18 @@ opt('smartindent', true, buffer)
opt('shiftwidth', tab_size, buffer)
opt('tabstop', tab_size, buffer)
opt('softtabstop', tab_size, buffer)
opt('cinkeys', '0{,0},0),0],:,!^F,o,O,e')
opt('cinoptions', 's,e0,n0,f0,{0,}0,^0,L-1,:s,=s,l0,b0,gs,hs,N0,E0,ps,ts,is,+s,c3,C0,/0,(2s,us,U0,w0,W0,k0,m0,j0,J0,)20,*70,#1,P0')
-- Place splits below and to the right by default
opt('splitbelow', true)
opt('splitright', true)
-- Remove cmdline when not used
opt('cmdheight', 0)
-- Scale neovide a bit
if vim.g.neovide then
vim.g.neovide_scale_factor = 0.75
vim.g.neovide_hide_mouse_when_typing = true
end

View File

@ -4,7 +4,7 @@
---@diagnostic disable-next-line: unused-local
local function usePlugins(use, use_rocks)
-- Toggle terminal
use {'akinsho/toggleterm.nvim', tag = '*', config=[[require('toggleterm').setup()]]}
use {'akinsho/toggleterm.nvim', tag = '*', config=[[require('config.toggleterm')]]}
-- Colorize ANSI codes
use { 'm00qek/baleia.nvim', tag = 'v1.2.0', config=[[require('config.baleia')]] }