From 237d67f82bfefc2f14ae6c70c06ca62af1833590 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sat, 17 Feb 2024 15:16:43 +0100 Subject: [PATCH] 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. --- config/nvim/after/ftplugin/lua.lua | 28 +++++++------------------ config/nvim/lua/fschauen/util.lua | 33 +++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/config/nvim/after/ftplugin/lua.lua b/config/nvim/after/ftplugin/lua.lua index 7616185..d8826fb 100644 --- a/config/nvim/after/ftplugin/lua.lua +++ b/config/nvim/after/ftplugin/lua.lua @@ -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('')) -end - -vim.keymap.set('n', 'gf', lua_go_to_file, buffer) -vim.keymap.set('n', 'x', exec_current_lua_line, buffer) -vim.keymap.set('x', 'x', exec_current_lua_selection, buffer) -vim.keymap.set('n', 'x', 'write | luafile %', buffer) +vim.keymap.set('n', 'gf', lua.go_to_module, opts('Go to module under cursor')) +vim.keymap.set('n', 'x', lua.execute_lines, opts('Execute current line')) +vim.keymap.set('x', 'x', lua.execute_selection, opts('Execute selection')) +vim.keymap.set('n', 'x', lua.execute_file, opts('Execute current file')) diff --git a/config/nvim/lua/fschauen/util.lua b/config/nvim/lua/fschauen/util.lua index 608dbe6..f8f13db 100644 --- a/config/nvim/lua/fschauen/util.lua +++ b/config/nvim/lua/fschauen/util.lua @@ -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('') + + 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