Skip to content

Instantly share code, notes, and snippets.

@fira42073
Last active March 19, 2025 12:25
Show Gist options
  • Save fira42073/fdef5e87507636c03c89a7a40adb99a1 to your computer and use it in GitHub Desktop.
Save fira42073/fdef5e87507636c03c89a7a40adb99a1 to your computer and use it in GitHub Desktop.
Add SqlcGenerate command to neovim for sqlc.dev generation
local Job = require("plenary.job")
local Popup = require("nui.popup")
-- Function to calculate popup height based on message length
local function calculate_popup_height(message)
local lines = vim.split(message, "\n")
local line_count = #lines
return math.min(line_count + 2, 50) -- max 50 lines long
end
-- Function to show popup notification
local function show_popup(title, message, timeout)
local popup_height = calculate_popup_height(message) -- Dynamically calculate the height
local popup = Popup({
enter = false,
focusable = false,
border = {
style = "rounded",
text = {
top = title,
top_align = "center",
},
},
position = "50%",
size = {
width = 100,
height = popup_height,
},
win_options = {
winblend = 10,
},
})
-- Set the popup content (message can be multiline)
vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, vim.split(message, "\n"))
-- Mount the popup
popup:mount()
-- Auto-close popup after timeout
vim.defer_fn(function() popup:unmount() end, timeout or 3000) -- default to 3 seconds
end
vim.api.nvim_create_user_command("SqlcGenerate", function()
-- Variables to store stdout and stderr
local stdout_data = {}
local stderr_data = {}
-- Run the sqlc generate command in the background
Job:new({
command = "sqlc",
args = { "generate" },
on_stdout = function(_, data)
-- Append stdout data
if data then table.insert(stdout_data, data) end
end,
on_stderr = function(_, data)
-- Append stderr data
if data then table.insert(stderr_data, data) end
end,
on_exit = function(_, return_val)
-- Notify the user based on the result
vim.schedule(function()
if return_val == 0 then
show_popup("SQLC Generation", "Generation complete!\n\n" .. table.concat(stdout_data, "\n"), 500)
else
show_popup("SQLC Error", "Generation failed:\n\n" .. table.concat(stderr_data, "\n"), 8000)
end
end)
end,
}):start()
end, { desc = "Run sqlc generate silently with detailed output" })
vim.api.nvim_create_user_command("MockeryGenerate", function()
-- Variables to store stdout and stderr
local stdout_data = {}
local stderr_data = {}
-- Run the sqlc generate command in the background
Job:new({
command = "mockery",
on_stdout = function(_, data)
-- Append stdout data
if data then table.insert(stdout_data, data) end
end,
on_stderr = function(_, data)
-- Append stderr data
if data then table.insert(stderr_data, data) end
end,
on_exit = function(_, return_val)
-- Notify the user based on the result
vim.schedule(function()
if return_val == 0 then
show_popup("Mockery Generation", "Generation complete!\n\n" .. table.concat(stdout_data, "\n"), 500)
else
show_popup("Mockery Error", "Generation failed:\n\n" .. table.concat(stderr_data, "\n"), 8000)
end
end)
end,
}):start()
end, { desc = "Run mockery generate silently with detailed output" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment