From e075db783d632fbd2adbec0614b97dffafd4f1d1 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 11 May 2023 17:30:35 +0300 Subject: [PATCH 1/4] add toggleterm binding --- lua/config/toggleterm.lua | 3 +++ lua/plugins.lua | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 lua/config/toggleterm.lua diff --git a/lua/config/toggleterm.lua b/lua/config/toggleterm.lua new file mode 100644 index 0000000..22ddcba --- /dev/null +++ b/lua/config/toggleterm.lua @@ -0,0 +1,3 @@ +require('toggleterm').setup() + +vim.keymap.set("n", "t", ":ToggleTerm") diff --git a/lua/plugins.lua b/lua/plugins.lua index 71d1934..aa812dc 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -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')]]} -- Debugger use { 'mfussenegger/nvim-dap', config=[[require('config.dap')]]} From 55016a51e2a5300a32dff5b52eda21293bea3a95 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 11 May 2023 17:30:35 +0300 Subject: [PATCH 2/4] add neovide options --- lua/options.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lua/options.lua b/lua/options.lua index 02f74ff..4329e57 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -106,3 +106,9 @@ opt('softtabstop', tab_size, buffer) -- Place splits below and to the right by default opt('splitbelow', true) opt('splitright', true) + +-- Scale neovide a bit +if vim.g.neovide then + vim.g.neovide_scale_factor = 0.7 + vim.g.neovide_hide_mouse_when_typing = true +end From 3e885141b5b7815273986ab11fa3d7e0289c2060 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 11 May 2023 17:30:35 +0300 Subject: [PATCH 3/4] add utility plugins --- init.lua | 3 ++ lua/add-guard.lua | 32 ++++++++++++++++++++ lua/cmacro-align.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 lua/add-guard.lua create mode 100644 lua/cmacro-align.lua diff --git a/init.lua b/init.lua index 4096322..dcfad18 100644 --- a/init.lua +++ b/init.lua @@ -27,6 +27,9 @@ require("options") require("bindings") require("pludin-dev") +require("add-guard") +require("cmacro-align") + -- Misc features require("highlight-yank") diff --git a/lua/add-guard.lua b/lua/add-guard.lua new file mode 100644 index 0000000..642dd42 --- /dev/null +++ b/lua/add-guard.lua @@ -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="?" }) diff --git a/lua/cmacro-align.lua b/lua/cmacro-align.lua new file mode 100644 index 0000000..0623225 --- /dev/null +++ b/lua/cmacro-align.lua @@ -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 From a3547a458a70e3a062c0da688e3f2d4d06248d19 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 11 May 2023 17:30:35 +0300 Subject: [PATCH 4/4] update how indenting c macros work --- lua/options.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lua/options.lua b/lua/options.lua index 4329e57..7ab2103 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -102,13 +102,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.7 + vim.g.neovide_scale_factor = 0.75 vim.g.neovide_hide_mouse_when_typing = true end