vim: move code from lua ftplugin to utils
The ftplugin is executed via the FileType event every time a lua file is opened, so better to have just the keymaps set there.
This commit is contained in:
parent
7817b927ee
commit
237d67f82b
2 changed files with 37 additions and 24 deletions
|
@ -1,27 +1,13 @@
|
|||
vim.bo.tabstop = 2
|
||||
|
||||
local buffer = { buffer = true }
|
||||
local lua = require('fschauen.util').lua
|
||||
|
||||
local exec_current_lua_line = function()
|
||||
local lineno = vim.fn.line('.')
|
||||
print('Executing line ' .. lineno)
|
||||
vim.fn.luaeval(vim.fn.getline(lineno))
|
||||
local opts = function(desc)
|
||||
return { desc = desc, buffer = true }
|
||||
end
|
||||
|
||||
local exec_current_lua_selection = function()
|
||||
local selection = { vim.fn.line('v'), vim.fn.line('.') }
|
||||
local first, last = vim.fn.min(selection), vim.fn.max(selection)
|
||||
local code = vim.fn.join(vim.fn.getline(first, last), '\n')
|
||||
print('Executing lines ' .. first .. ' to ' .. last)
|
||||
loadstring(code)()
|
||||
end
|
||||
|
||||
local lua_go_to_file = function()
|
||||
require('fschauen.util').edit_lua_module(vim.fn.expand('<cfile>'))
|
||||
end
|
||||
|
||||
vim.keymap.set('n', 'gf', lua_go_to_file, buffer)
|
||||
vim.keymap.set('n', '<localleader>x', exec_current_lua_line, buffer)
|
||||
vim.keymap.set('x', '<localleader>x', exec_current_lua_selection, buffer)
|
||||
vim.keymap.set('n', '<localleader><localleader>x', '<cmd>write | luafile %<cr>', buffer)
|
||||
vim.keymap.set('n', 'gf', lua.go_to_module, opts('Go to module under cursor'))
|
||||
vim.keymap.set('n', '<localleader>x', lua.execute_lines, opts('Execute current line'))
|
||||
vim.keymap.set('x', '<localleader>x', lua.execute_selection, opts('Execute selection'))
|
||||
vim.keymap.set('n', '<localleader><localleader>x', lua.execute_file, opts('Execute current file'))
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ M.edit_file = function(path)
|
|||
end
|
||||
end
|
||||
|
||||
local find_module_source = function(modname)
|
||||
local find_lua_module_sources = function(modname)
|
||||
modname = modname:gsub('^%.+', ''):gsub('/', '.')
|
||||
local base = 'lua/' .. modname:gsub('%.', '/')
|
||||
local candidates = { base .. '.lua', base .. '/init.lua' }
|
||||
|
@ -28,8 +28,33 @@ local find_module_source = function(modname)
|
|||
return results
|
||||
end
|
||||
|
||||
M.edit_lua_module = function(modname)
|
||||
local sources = find_module_source(modname)
|
||||
local lua = {}
|
||||
|
||||
lua.execute_lines = function(first, last)
|
||||
first = first or vim.fn.line('.')
|
||||
last = last or first
|
||||
local code = vim.fn.join(vim.fn.getline(first, last), '\n')
|
||||
loadstring(code)()
|
||||
end
|
||||
|
||||
lua.execute_selection = function()
|
||||
local selection = { vim.fn.line('v'), vim.fn.line('.') }
|
||||
table.sort(selection)
|
||||
lua.execute_lines(unpack(selection))
|
||||
end
|
||||
|
||||
lua.execute_file = function(path)
|
||||
if path then
|
||||
vim.cmd.luafile(path)
|
||||
else
|
||||
lua.execute_lines(1, vim.fn.line('$'))
|
||||
end
|
||||
end
|
||||
|
||||
lua.go_to_module = function(modname)
|
||||
modname = modname or vim.fn.expand('<cfile>')
|
||||
|
||||
local sources = find_lua_module_sources(modname)
|
||||
if #sources == 0 then
|
||||
vim.notify('Not found: ' .. modname, vim.log.levels.WARN)
|
||||
elseif #sources == 1 then
|
||||
|
@ -43,5 +68,7 @@ M.edit_lua_module = function(modname)
|
|||
end
|
||||
end
|
||||
|
||||
M.lua = lua
|
||||
|
||||
return M
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue