Advertisement
voyeg3r

lua.lua

Feb 26th, 2022
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- File: /home/sergio/.config/nvim/ftplugin/lua.lua
  2. -- Last Change: Sat, 19 Feb 2022 15:58
  3. -- source: https://www.reddit.com/r/neovim/comments/pl0p5v/shortcut_to_open_lua_require_file/
  4.  
  5. -- vim.opt_local.suffixesadd:prepend('.lua')
  6. -- vim.opt_local.suffixesadd:prepend('init.lua')
  7. -- vim.opt_local.path:prepend(vim.fn.stdpath('config')..'/lua')
  8.  
  9. local fmt = string.format
  10.  
  11. -- Iterator that splits a string o a given delimiter
  12. local function split(str, delim)
  13.     delim = delim or "%s"
  14.     return string.gmatch(str, fmt('[^%s]+', delim))
  15. end
  16.  
  17. -- Find the proper directory separator depending
  18. -- on lua installation or OS.
  19. local function dir_separator()
  20.     -- Look at package.config for directory separator string (it's the first line)
  21.     if package.config then
  22.         return string.match(package.config, '^[^\n]')
  23.     elseif vim.fn.has('win32') == 1 then
  24.         return '\\'
  25.     else
  26.         return '/'
  27.     end
  28. end
  29.  
  30. -- Search for lua traditional include paths.
  31. -- This mimics how require internally works.
  32. local function include_paths(fname, ext)
  33.     ext = ext or "lua"
  34.     local sep = dir_separator()
  35.     local paths = string.gsub(package.path, '%?', fname)
  36.     for path in split(paths, "%;") do
  37.         if vim.fn.filereadable(path) == 1 then
  38.             return path
  39.         end
  40.     end
  41. end
  42.  
  43. -- Search for nvim lua include paths
  44. local function include_rtpaths(fname, ext)
  45.     ext = ext or "lua"
  46.     local sep = dir_separator()
  47.     local rtpaths = vim.api.nvim_list_runtime_paths()
  48.     local modfile, initfile = fmt('%s.%s', fname, ext), fmt('init.%s', ext)
  49.     for _, path in ipairs(rtpaths) do
  50.         -- Look on runtime path for 'lua/*.lua' files
  51.         local path1 = table.concat({path, ext, modfile}, sep)
  52.         if     vim.fn.filereadable(path1) == 1 then
  53.             return path1
  54.         end
  55.         -- Look on runtime path for 'lua/*/init.lua' files
  56.         local path2 = table.concat({path, ext, fname, initfile}, sep)
  57.         if vim.fn.filereadable(path2) == 1 then
  58.             return path2
  59.         end
  60.     end
  61. end
  62.  
  63. -- Global function that searches the path for the required file
  64. function find_required_path(module)
  65.     -- Look at package.config for directory separator string (it's the first line)
  66.     local sep = string.match(package.config, '^[^\n]')
  67.     -- Properly change '.' to separator (probably '/' on *nix and '\' on Windows)
  68.     local fname = vim.fn.substitute(module, "\\.", sep, "g")
  69.     local f
  70.     ---- First search for lua modules
  71.     f = include_paths(fname, 'lua')
  72.     if f then return f end
  73.     -- This part is just for nvim modules
  74.     f = include_rtpaths(fname, 'lua')
  75.     if f then return f end
  76.     ---- Now search for Fennel modules
  77.     f = include_paths(fname, 'fnl')
  78.     if f then return f end
  79.     -- This part is just for nvim modules
  80.     f = include_rtpaths(fname, 'fnl')
  81.     if f then return f end
  82. end
  83.  
  84.  
  85. -- Set options to open require with gf
  86. vim.opt_local.include = [=[\v<((do|load)file|require)\s*\(?['"]\zs[^'"]+\ze['"]]=]
  87. vim.opt_local.includeexpr = "v:lua.find_required_path(v:fname)"
  88.  
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement