Skip to content

Instantly share code, notes, and snippets.

@hwayne
Created April 9, 2025 01:39
Show Gist options
  • Save hwayne/ecc5dd0812897ff774d168ccb6b839ae to your computer and use it in GitHub Desktop.
Save hwayne/ecc5dd0812897ff774d168ccb6b839ae to your computer and use it in GitHub Desktop.
Useful neovim autocommands for TLA+
local function moduleline() -- can't be a global as line can move
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
for idx, line in ipairs(lines) do
if string.match(line, "%-+ MODULE [%w_]+ %-") then
return idx, line
end
end
end
local function fix_module_string()
local module_string = string.format("---- MODULE %s ----", vim.fn.expand("%:r"))
local idx, _ = moduleline()
vim.api.nvim_buf_set_lines(0, idx-1, idx, false, {module_string})
end
local group = vim.api.nvim_create_augroup("tla.custom", { clear = true })
-- Automatically put the module header and footer in new files
vim.api.nvim_create_autocmd({ "BufNewFile" }, {
pattern = { "*.tla" },
callback = function()
local top = string.format("---- MODULE %s ----", vim.fn.expand("%:r"))
local snippet = {top, "", "===="}
vim.api.nvim_buf_set_lines(0, 0, -1, false, snippet)
end,
group = group,
})
-- On save, rename the module header to the filename
vim.api.nvim_create_autocmd({ "BufWrite" }, {
pattern = { "*.tla" },
callback = fix_module_string,
group = group,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment