updating all of my config, specifically doing this to fix the nvim configurations
This commit is contained in:
102
nvim/lua/compilation.lua
Normal file
102
nvim/lua/compilation.lua
Normal file
@@ -0,0 +1,102 @@
|
||||
-- File: ~/.config/nvim/lua/compilation.lua
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Function to compile and run the current file
|
||||
function M.compile_and_run()
|
||||
-- Get the current file path
|
||||
local file = vim.fn.expand('%')
|
||||
|
||||
-- Check if the current buffer is a directory
|
||||
if vim.fn.isdirectory(file) == 1 then
|
||||
vim.notify("Cannot compile a directory. Please open a source file.", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
-- Determine the filetype (c, cpp, etc.)
|
||||
local filetype = vim.bo.filetype
|
||||
local compiler = ''
|
||||
local flags = ''
|
||||
|
||||
-- Set compiler and flags based on filetype
|
||||
if filetype == 'c' then
|
||||
compiler = 'gcc'
|
||||
flags = '-lm -O3'
|
||||
elseif filetype == 'cpp' then
|
||||
compiler = 'g++'
|
||||
flags = '-lm -O3'
|
||||
else
|
||||
vim.notify("Unsupported file type: " .. filetype, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
-- Get the base name of the file without extension
|
||||
local output_name = vim.fn.expand('%:t:r') -- %:t:r extracts the filename without extension
|
||||
|
||||
-- Construct the compile command
|
||||
local compile_cmd = string.format('%s %s "%s" -o "%s"', compiler, flags, file, output_name)
|
||||
|
||||
-- Notify the user about the compilation process
|
||||
vim.notify("Compiling...", vim.log.levels.INFO)
|
||||
|
||||
-- Execute the compile command
|
||||
local compile_output = vim.fn.system(compile_cmd)
|
||||
local compile_exit = vim.v.shell_error
|
||||
|
||||
-- Check if compilation was successful
|
||||
if compile_exit ~= 0 then
|
||||
vim.notify("Compilation failed:\n" .. compile_output, vim.log.levels.ERROR)
|
||||
-- Populate quickfix list with errors
|
||||
vim.fn.setqflist({}, ' ')
|
||||
for line in compile_output:gmatch("[^\r\n]+") do
|
||||
if line:match("error") or line:match("warning") then
|
||||
vim.fn.setqflist({}, 'a', { lines = { line } })
|
||||
end
|
||||
end
|
||||
vim.cmd('copen') -- Open the quickfix list to show errors
|
||||
return
|
||||
end
|
||||
|
||||
-- Construct the run command
|
||||
local run_cmd = string.format('./%s', output_name)
|
||||
|
||||
-- Notify the user about the execution process
|
||||
vim.notify("Running executable...", vim.log.levels.INFO)
|
||||
|
||||
-- Function to check if a terminal is already open
|
||||
local function is_terminal_open()
|
||||
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
||||
local buf = vim.api.nvim_win_get_buf(win)
|
||||
if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Run the executable in a terminal split at the bottom
|
||||
if not is_terminal_open() then
|
||||
vim.cmd('botright split') -- Open split at the bottom
|
||||
vim.cmd('resize 15') -- Optional: Adjust the size of the split
|
||||
vim.cmd('terminal ' .. run_cmd) -- Run the executable
|
||||
else
|
||||
-- If a terminal is already open, send the run command to it
|
||||
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
||||
local buf = vim.api.nvim_win_get_buf(win)
|
||||
if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then
|
||||
vim.api.nvim_set_current_win(win)
|
||||
vim.api.nvim_feedkeys(run_cmd .. '\n', 'n', true)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Function to set up keybindings
|
||||
function M.setup()
|
||||
-- Bind <leader>c to compile_and_run in normal mode
|
||||
vim.keymap.set('n', '<leader>c', M.compile_and_run, { noremap = true, silent = true })
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user