better
This commit is contained in:
13
nvim/lua/custom/chadrc.lua
Normal file
13
nvim/lua/custom/chadrc.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
M.nvimtree = {
|
||||
git = {
|
||||
enable = false,
|
||||
},
|
||||
renderer = {
|
||||
highlight_git = false,
|
||||
icons = {
|
||||
show = {
|
||||
git = false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
145
nvim/lua/custom/cpp_gen.lua
Normal file
145
nvim/lua/custom/cpp_gen.lua
Normal file
@@ -0,0 +1,145 @@
|
||||
local M = {}
|
||||
|
||||
-- Helper function to trim whitespace.
|
||||
local function trim(s)
|
||||
return (s:gsub("^%s*(.-)%s*$", "%1"))
|
||||
end
|
||||
|
||||
-- Main function:
|
||||
-- filename: The header file name, e.g. "MyClass.hpp"
|
||||
function M.generate_cpp_file(header_file)
|
||||
-- Read the header file contents.
|
||||
if header_file == nil or header_file == "" then
|
||||
header_file = vim.api.nvim_buf_get_name(0)
|
||||
end
|
||||
local file = io.open(header_file, "r")
|
||||
if not file then
|
||||
print("Error: could not open file " .. header_file)
|
||||
return
|
||||
end
|
||||
local content = file:read("*a")
|
||||
file:close()
|
||||
|
||||
-- Extract the class name.
|
||||
-- In this example, we assume the header file name is the same as the class name.
|
||||
local class_name = header_file:match("([^/\\]+)%.hpp$")
|
||||
if not class_name then
|
||||
print("Error: could not determine class name from header file name.")
|
||||
return
|
||||
end
|
||||
|
||||
-- Prepare a table to store function definitions.
|
||||
local functions = {}
|
||||
|
||||
-- A very simple parser: we assume each function is declared on a single line.
|
||||
-- Look for setters (e.g., void setSomething(...);)
|
||||
for line in content:gmatch("[^\r\n]+") do
|
||||
line = trim(line)
|
||||
-- Match setters: assume declaration is like "void setX(type x);" (ignoring qualifiers).
|
||||
local set_func, args = line:match("void%s+(set%u%w*)%s*%(([^)]*)%)")
|
||||
if set_func then
|
||||
table.insert(functions, {name = set_func, args = trim(args), return_type = "void"})
|
||||
end
|
||||
|
||||
-- Match getters: assume declaration is like "type getX(void) const;" or similar.
|
||||
-- We capture the return type, function name and arguments.
|
||||
local ret_type, get_func, args = line:match("([%w_:<>]+)%s+(get%u%w*)%s*%(([^)]*)%)")
|
||||
if ret_type and get_func then
|
||||
table.insert(functions, {name = get_func, args = trim(args), return_type = ret_type})
|
||||
end
|
||||
end
|
||||
|
||||
-- Start building the content for the .cpp file.
|
||||
local cpp_lines = {}
|
||||
|
||||
table.insert(cpp_lines, '#include "' .. class_name .. '.hpp"')
|
||||
table.insert(cpp_lines, "") -- blank line
|
||||
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ CONSTRUCTOR ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
|
||||
-- Default constructor.
|
||||
table.insert(cpp_lines, class_name .. "::" .. class_name .. "() {")
|
||||
table.insert(cpp_lines, "}")
|
||||
table.insert(cpp_lines, "")
|
||||
-- Copy constructor.
|
||||
table.insert(cpp_lines, class_name .. "::" .. class_name .. "(const " .. class_name .. " &other) { *this = other; }")
|
||||
table.insert(cpp_lines, "")
|
||||
-- Assignment operator.
|
||||
table.insert(cpp_lines, class_name .. " &" .. class_name .. "::operator=(const " .. class_name .. " &other) {")
|
||||
table.insert(cpp_lines, " if (this == &other) { return *this; }")
|
||||
table.insert(cpp_lines, "")
|
||||
table.insert(cpp_lines, " // copy")
|
||||
table.insert(cpp_lines, " return *this;")
|
||||
table.insert(cpp_lines, "}")
|
||||
table.insert(cpp_lines, "")
|
||||
-- Destructor.
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ DESTRUCTOR ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
table.insert(cpp_lines, class_name .. "::~" .. class_name .. "() {")
|
||||
table.insert(cpp_lines, "}")
|
||||
table.insert(cpp_lines, "")
|
||||
|
||||
-- Generate stubs for other member functions (e.g., set and get functions).
|
||||
if #functions > 0 then
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ GET() ┃ SET() ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
for _, func in ipairs(functions) do
|
||||
-- If no arguments, we insert void explicitly.
|
||||
local args = func.args
|
||||
if args == "" then
|
||||
args = "void"
|
||||
end
|
||||
local line = func.return_type .. " " .. class_name .. "::" .. func.name .. "(" .. args .. ")"
|
||||
-- For getters, you might want to mark the function as const.
|
||||
if func.name:sub(1, 3) == "get" then
|
||||
line = line .. " const"
|
||||
end
|
||||
table.insert(cpp_lines, line .. " {")
|
||||
if func.name:sub(1, 3) == "get" then
|
||||
local string = ""
|
||||
table.insert(cpp_lines, " return this->" .. string.lower(func.name:sub(4, 4)) .. func.name:sub(5, func.name.len(func.name)) .. ";")
|
||||
end
|
||||
if func.name:sub(1, 3) == "set" then
|
||||
local string = ""
|
||||
table.insert(cpp_lines, " this->" .. string.lower(func.name:sub(4, 4)) .. func.name:sub(5, func.name.len(func.name)) .. " = " .. func.args:gsub('.*% ', '') .. ";")
|
||||
end
|
||||
table.insert(cpp_lines, "}")
|
||||
table.insert(cpp_lines, "")
|
||||
end
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ MEMBER ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
else
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ GET() ┃ SET() ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
table.insert(cpp_lines, "// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓")
|
||||
table.insert(cpp_lines, "// ┃ MEMBER ┃")
|
||||
table.insert(cpp_lines, "// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛")
|
||||
table.insert(cpp_lines, "")
|
||||
end
|
||||
|
||||
-- Write the generated content to the .cpp file.
|
||||
local cpp_file = class_name .. ".cpp"
|
||||
local dir = header_file:match("^(.*[/\\])") or ""
|
||||
local out = io.open(dir .. cpp_file, "w")
|
||||
if not out then
|
||||
print("Error: could not write to file " .. cpp_file)
|
||||
return
|
||||
end
|
||||
out:write(table.concat(cpp_lines, "\n"))
|
||||
out:close()
|
||||
|
||||
print("Generated " .. cpp_file)
|
||||
end
|
||||
|
||||
return M
|
||||
93
nvim/lua/custom/function_lines.lua
Normal file
93
nvim/lua/custom/function_lines.lua
Normal file
@@ -0,0 +1,93 @@
|
||||
local function show_function_lines()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local ft = vim.bo[bufnr].filetype
|
||||
local lang = vim.treesitter.language.get_lang(ft) or ft
|
||||
local parser = vim.treesitter.get_parser(bufnr, lang)
|
||||
if not parser then
|
||||
return
|
||||
end
|
||||
local ns = vim.api.nvim_create_namespace('function_lines')
|
||||
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
|
||||
-- Tree-sitter query for C functions, with three cases:
|
||||
-- Case 1: Regular function (no pointer)
|
||||
-- Case 2: Function returning a pointer (one pointer_declarator)
|
||||
-- Case 3: Function returning a double pointer (two pointer_declarator nodes)
|
||||
local query = vim.treesitter.query.parse(lang, [[
|
||||
; Case 1: Regular function (no pointer)
|
||||
(function_definition
|
||||
declarator: (function_declarator
|
||||
declarator: (identifier) @func_name
|
||||
)
|
||||
body: (compound_statement) @func_body
|
||||
) @func;
|
||||
; Case 2: Function returning a pointer (one pointer_declarator)
|
||||
(function_definition
|
||||
declarator: (pointer_declarator
|
||||
declarator: (function_declarator
|
||||
declarator: (identifier) @func_name
|
||||
)
|
||||
)
|
||||
body: (compound_statement) @func_body
|
||||
) @func;
|
||||
; Case 3: Function returning a double pointer (two pointer_declarator nodes)
|
||||
(function_definition
|
||||
declarator: (pointer_declarator
|
||||
declarator: (pointer_declarator
|
||||
declarator: (function_declarator
|
||||
declarator: (identifier) @func_name
|
||||
)
|
||||
)
|
||||
)
|
||||
body: (compound_statement) @func_body
|
||||
) @func
|
||||
]])
|
||||
for _, tree in ipairs(parser:parse()) do
|
||||
local root = tree:root()
|
||||
-- Iterate over each capture from the query
|
||||
for id, node, _ in query:iter_captures(root, bufnr, 0, -1) do
|
||||
if query.captures[id] == "func_body" then
|
||||
-- Get the range of the compound statement.
|
||||
-- The range includes the braces, so we subtract 2 to get the interior.
|
||||
local start_line, _, end_line, _ = node:range()
|
||||
local total_lines = end_line - start_line - 1
|
||||
if total_lines < 0 then
|
||||
total_lines = 0
|
||||
end
|
||||
-- Get the lines between the braces.
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, start_line + 1, end_line, false)
|
||||
local comment_lines = 0
|
||||
for _, line in ipairs(lines) do
|
||||
-- Adjust this pattern as needed for your comment style.
|
||||
if line:match("^%s*//") then
|
||||
comment_lines = comment_lines + 1
|
||||
end
|
||||
end
|
||||
local non_comment_lines = total_lines - comment_lines
|
||||
-- Prepare the virtual text.
|
||||
local virt_text = nil
|
||||
if comment_lines > 0 then
|
||||
-- If there are comment lines, display two numbers:
|
||||
-- Total lines / non-comment lines.
|
||||
virt_text = {{ ' ' .. total_lines .. ' / ' .. non_comment_lines, 'Comment' }}
|
||||
else
|
||||
-- If no comments, display only the total line count.
|
||||
virt_text = {{ ' ' .. total_lines, 'Comment' }}
|
||||
end
|
||||
-- Place the virtual text on the closing brace line.
|
||||
vim.api.nvim_buf_set_extmark(bufnr, ns, end_line, -1, {
|
||||
virt_text = virt_text,
|
||||
virt_text_pos = 'eol',
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Auto-command to run the function for C files on buffer enter or after writing.
|
||||
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, {
|
||||
pattern = '*.c',
|
||||
callback = show_function_lines,
|
||||
})-- Auto-command to run the function for C files on buffer enter or after writing.
|
||||
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost' }, {
|
||||
pattern = '*.c',
|
||||
callback = show_function_lines,
|
||||
})
|
||||
42
nvim/lua/custom/hpp_gen.lua
Normal file
42
nvim/lua/custom/hpp_gen.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
local M = {}
|
||||
|
||||
function M.generate_hpp_file(class_name)
|
||||
|
||||
local class = class_name
|
||||
local hpp_lines = {}
|
||||
|
||||
table.insert(hpp_lines, "#ifndef " .. class.upper(class) .. "_HPP")
|
||||
table.insert(hpp_lines, "# define " .. class.upper(class) .. "_HPP")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, "class " .. class .. " {")
|
||||
table.insert(hpp_lines, " protected:")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, " private:")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, " public:")
|
||||
table.insert(hpp_lines, " " .. cCass .. "();")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, " ~" .. class .. "();")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, " " .. class .. "(const " .. class .. " &other);")
|
||||
table.insert(hpp_lines, " " .. class .. " &operator=(const " .. class .. " &other);")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, "};")
|
||||
table.insert(hpp_lines, " // Member")
|
||||
table.insert(hpp_lines, "")
|
||||
table.insert(hpp_lines, "#endif")
|
||||
|
||||
local hpp_file = class .. ".hpp"
|
||||
local out = io.open(hpp_file, "w")
|
||||
|
||||
if not out then
|
||||
print("Error: Could not create hpp file")
|
||||
return
|
||||
end
|
||||
out:write(table.concat(hpp_lines, "\n"))
|
||||
out:close()
|
||||
|
||||
print("Generated " .. hpp_file)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user