nvim refactor keymap by filetype logic
This commit is contained in:
parent
5050bee3c9
commit
cd1cebc84d
@ -1,86 +1,32 @@
|
|||||||
|
|
||||||
local function autocmd(func)
|
|
||||||
local create_autocmd = vim.api.nvim_create_autocmd
|
|
||||||
|
|
||||||
create_autocmd("BufEnter",
|
local function map_filetype(filetype, key, cmd)
|
||||||
{ pattern = '*', callback = func}
|
local function format_string()
|
||||||
|
local run_script_string = [[:tabnew %% | :terminal %s <CR> :set nocursorline number norelativenumber <CR> G]]
|
||||||
|
local cmd_string = string.format(run_script_string, cmd)
|
||||||
|
|
||||||
|
local map = vim.keymap.set
|
||||||
|
local opts = { noremap = true, silent = true }
|
||||||
|
map('n', key, cmd_string, opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd("FileType",
|
||||||
|
{ pattern = filetype, callback = format_string}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_keymap_base(key, cmd)
|
-- map_filename('manpage', '<Leader>rr', 'man -P cat -l %') -- fix (by filenamej)
|
||||||
local map = vim.keymap.set
|
|
||||||
local opts = { noremap = true, silent = true }
|
|
||||||
local keymap_keys = string.format([[<Leader>r%s]], key)
|
|
||||||
map("n", keymap_keys, cmd, opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_format_file(cmd)
|
map_filetype('*', '<Leader>rs', '$(head -1 % | cut -c 3-) %')
|
||||||
local cmd_string = string.format([[:!%s %% <CR>]], cmd)
|
|
||||||
set_keymap_base("f", cmd_string)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_run_script_base(key, cmd)
|
map_filetype('python', '<Leader>rr', 'python3 %')
|
||||||
local run_script_string = [[:tabnew %% <CR> :terminal %s %% <CR> :set nocursorline number norelativenumber <CR> G <CR>]]
|
map_filetype('python', '<Leader>rt', 'pytest %')
|
||||||
local cmd_string = string.format(run_script_string, cmd)
|
|
||||||
set_keymap_base(key, cmd_string)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_run_script(cmd)
|
map_filetype('go', '<Leader>rr', 'go run')
|
||||||
set_keymap_run_script_base("r", cmd)
|
-- map_filetype('go', '<Leader>rf', 'go fmt') -- fix (without open terminal)
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_test_file_base(key, cmd)
|
map_filetype('rust', '<Leader>rr', 'cargo run')
|
||||||
local run_script_string = [[:tabnew %% <CR> :terminal %s %% <CR> :set nocursorline number norelativenumber <CR> G <CR>]]
|
-- map_filetype('rust', '<Leader>rf', 'cargo fmt -p') -- fix (without open terminal)
|
||||||
local cmd_string = string.format(run_script_string, cmd)
|
|
||||||
set_keymap_base(key, cmd_string)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_test_file(cmd)
|
map_filetype('c', '<Leader>rr', 'gcc % && ./a.out')
|
||||||
set_keymap_test_file_base("t", cmd)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function set_keymap_run_script_by_shebang()
|
|
||||||
set_keymap_run_script_base("s", [[$(head -1 % | cut -c 3-) %]])
|
|
||||||
end
|
|
||||||
|
|
||||||
local function create_function_autocmd_by_filetype(set_keymap_func, ft, cmd)
|
|
||||||
return function()
|
|
||||||
if vim.bo.filetype == ft then
|
|
||||||
set_keymap_func(cmd)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function create_function_autocmd_by_filename(set_keymap_func, fn, cmd)
|
|
||||||
return function()
|
|
||||||
if vim.fn.expand('%:t') == fn then
|
|
||||||
set_keymap_func(cmd)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function autocmd_run_script_by_filetype(ft, cmd)
|
|
||||||
autocmd(create_function_autocmd_by_filetype(set_keymap_run_script, ft, cmd))
|
|
||||||
end
|
|
||||||
|
|
||||||
local function autocmd_format_file_by_filetype(ft, cmd)
|
|
||||||
autocmd(create_function_autocmd_by_filetype(set_keymap_format_file, ft, cmd))
|
|
||||||
end
|
|
||||||
|
|
||||||
local function autocmd_run_tests_by_filetype(ft, cmd)
|
|
||||||
autocmd(create_function_autocmd_by_filetype(set_keymap_test_file, ft, cmd))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
autocmd_run_script_by_filetype('python', 'python3')
|
|
||||||
autocmd_run_script_by_filetype('go', 'go run')
|
|
||||||
autocmd_run_script_by_filetype('rust', 'cargo run')
|
|
||||||
|
|
||||||
autocmd_run_tests_by_filetype('python', 'pytest')
|
|
||||||
|
|
||||||
autocmd(create_function_autocmd_by_filename(set_keymap_run_script, 'manpage', 'man -P cat -l'))
|
|
||||||
|
|
||||||
autocmd_format_file_by_filetype('rust', 'cargo fmt -p')
|
|
||||||
autocmd_format_file_by_filetype('go', 'go fmt')
|
|
||||||
|
|
||||||
autocmd(set_keymap_run_script_by_shebang)
|
|
||||||
Loading…
x
Reference in New Issue
Block a user