mirror of
https://github.com/DefectingCat/nvim
synced 2025-07-15 16:51:33 +00:00
add custom commands
This commit is contained in:
65
lua/rua/core/autocmd.lua
Normal file
65
lua/rua/core/autocmd.lua
Normal file
@ -0,0 +1,65 @@
|
||||
local autocmd = vim.api.nvim_create_autocmd
|
||||
local augroup = vim.api.nvim_create_augroup
|
||||
|
||||
-- set env files to sh
|
||||
autocmd({ "BufNewFile", "BufRead" }, {
|
||||
pattern = { ".env.example", ".env.local", ".env.development", ".env.production" },
|
||||
callback = function()
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
vim.api.nvim_buf_set_option(buf, "filetype", "sh")
|
||||
end,
|
||||
})
|
||||
|
||||
-- Automatically update changed file in Vim
|
||||
-- Triger `autoread` when files changes on disk
|
||||
-- https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
|
||||
-- https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
|
||||
autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, {
|
||||
command = [[silent! if mode() != 'c' && !bufexists("[Command Line]") | checktime | endif]],
|
||||
})
|
||||
|
||||
-- Notification after file change
|
||||
-- https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
|
||||
autocmd("FileChangedShellPost", {
|
||||
command = [[echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None]],
|
||||
})
|
||||
|
||||
---- 用o换行不要延续注释
|
||||
local myAutoGroup = vim.api.nvim_create_augroup("myAutoGroup", {
|
||||
clear = true,
|
||||
})
|
||||
autocmd("BufEnter", {
|
||||
group = myAutoGroup,
|
||||
pattern = "*",
|
||||
callback = function()
|
||||
vim.opt.formatoptions = vim.opt.formatoptions
|
||||
- "o" -- O and o, don't continue comments
|
||||
+ "r" -- But do continue when pressing enter.
|
||||
end,
|
||||
})
|
||||
|
||||
-- Highlight on yank
|
||||
local highlight_group = vim.api.nvim_create_augroup("YankHighlight", { clear = true })
|
||||
autocmd("TextYankPost", {
|
||||
callback = function()
|
||||
vim.highlight.on_yank()
|
||||
end,
|
||||
group = highlight_group,
|
||||
pattern = "*",
|
||||
})
|
||||
|
||||
-- This autocmd will restore cursor position on file open
|
||||
autocmd("BufReadPost", {
|
||||
pattern = "*",
|
||||
callback = function()
|
||||
local line = vim.fn.line("'\"")
|
||||
if
|
||||
line > 1
|
||||
and line <= vim.fn.line("$")
|
||||
and vim.bo.filetype ~= "commit"
|
||||
and vim.fn.index({ "xxd", "gitrebase" }, vim.bo.filetype) == -1
|
||||
then
|
||||
vim.cmd('normal! g`"')
|
||||
end
|
||||
end,
|
||||
})
|
@ -1,2 +1,4 @@
|
||||
require("rua.core.options")
|
||||
require("rua.core.keymaps")
|
||||
require("rua.core.usercmd")
|
||||
require("rua.core.autocmd")
|
||||
|
@ -20,3 +20,9 @@ map("i", "<A-j>", "<Esc>:m .+1<CR>==gi")
|
||||
map("i", "<A-k>", "<Esc>:m .-2<CR>==gi")
|
||||
map("v", "<A-j>", ":m '>+1<CR>gv=gv")
|
||||
map("v", "<A-k>", ":m '<-2<CR>gv=gv")
|
||||
|
||||
map("n", "<C-a>", "gg<S-v>G")
|
||||
map("n", "$", "g_")
|
||||
map("v", "$", "g_")
|
||||
map("v", ">", ">gv")
|
||||
map("v", "<", "<gv")
|
||||
|
31
lua/rua/core/usercmd.lua
Normal file
31
lua/rua/core/usercmd.lua
Normal file
@ -0,0 +1,31 @@
|
||||
local user_command = vim.api.nvim_create_user_command
|
||||
|
||||
-- Toggle conform format on save
|
||||
user_command("FormatDisable", function(args)
|
||||
if args.bang then
|
||||
-- FormatDisable! will disable formatting just for this buffer
|
||||
vim.b.disable_autoformat = true
|
||||
else
|
||||
vim.g.disable_autoformat = true
|
||||
end
|
||||
end, {
|
||||
desc = "Disable autoformat-on-save",
|
||||
bang = true,
|
||||
})
|
||||
user_command("FormatEnable", function()
|
||||
vim.b.disable_autoformat = false
|
||||
vim.g.disable_autoformat = false
|
||||
end, {
|
||||
desc = "Re-enable autoformat-on-save",
|
||||
})
|
||||
user_command("FormatToggle", function()
|
||||
vim.b.disable_autoformat = not vim.b.disable_autoformat
|
||||
vim.g.disable_autoformat = not vim.g.disable_autoformat
|
||||
if vim.g.disable_autoformat then
|
||||
vim.notify("autoformat disabled")
|
||||
else
|
||||
vim.notify("autoformat enabled")
|
||||
end
|
||||
end, {
|
||||
desc = "Re-enable autoformat-on-save",
|
||||
})
|
@ -35,11 +35,23 @@ return {
|
||||
-- have other formatters configured.
|
||||
["_"] = { "trim_whitespace" },
|
||||
},
|
||||
format_on_save = {
|
||||
lsp_fallback = true,
|
||||
-- async = false,
|
||||
timeout_ms = 1000,
|
||||
},
|
||||
format_on_save = function(bufnr)
|
||||
-- Disable with a global or buffer-local variable
|
||||
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
||||
return
|
||||
end
|
||||
return { timeout_ms = 1000, lsp_fallback = true }
|
||||
end,
|
||||
-- If this is set, Conform will run the formatter asynchronously after save.
|
||||
-- It will pass the table to conform.format().
|
||||
-- This can also be a function that returns the table.
|
||||
format_after_save = function(bufnr)
|
||||
-- Disable with a global or buffer-local variable
|
||||
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
||||
return { lsp_fallback = false }
|
||||
end
|
||||
return { lsp_fallback = true }
|
||||
end,
|
||||
notify_on_error = true,
|
||||
formatters = {
|
||||
injected = {
|
||||
@ -126,9 +138,9 @@ return {
|
||||
vim.keymap.set({ "n", "v" }, "<leader>fm", function()
|
||||
conform.format({
|
||||
lsp_fallback = true,
|
||||
-- async = false,
|
||||
timeout_ms = 1000,
|
||||
})
|
||||
end, { desc = "Format file or range (in visual mode)" })
|
||||
vim.keymap.set("n", "<leader>tf", "<cmd> FormatToggle <cr>", { desc = "Re-enable autoformat-on-save" })
|
||||
end,
|
||||
}
|
||||
|
Reference in New Issue
Block a user