robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d9ed4c8566
master
1local pathtrails ={} --- @type table<string,true> ta 2vim . _so_trails ={} --- @type string[] 3for s in ( package . cpath .. ';' ): gmatch ( '[^;]*;' ) do 4s =s : sub ( 1 , - 2 ) -- Strip trailing semicolon 5-- Find out path patterns. pathtrail should contain something like 6-- /?.so, \?.dll. This allows not to bother determining what correct 7-- suffixes are. 8local pathtrail =s : match ( '[/\\][^/\\]*%?.*$' ) 9if pathtrail and not pathtrails [ pathtrail ] then 10pathtrails [ pathtrail ] =true 11table . insert ( vim . _so_trails , pathtrail ) 12end 13end 14 15--- @param name string 16function vim . _load_package ( name ) 17local basename =name : gsub ( '%.' , '/' ) 18local paths ={ 'lua/' .. basename .. '.lua' , 'lua/' .. basename .. '/init.lua' } 19local found =vim . api . nvim__get_runtime ( paths , false , { is_lua =true } ) 20if # found > 0 then 21local f , err =loadfile ( found [ 1 ]) 22return f or error ( err ) 23end 24 25local so_paths ={} 26for _ , trail in ipairs ( vim . _so_trails ) do 27local path ='lua' .. trail : gsub ( '?' , basename ) -- so_trails contains a leading slash 28table . insert ( so_paths , path ) 29end 30 31found =vim . api . nvim__get_runtime ( so_paths , false , { is_lua =true } ) 32if # found > 0 then 33-- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is 34-- a) strip prefix up to and including the first dash, if any 35-- b) replace all dots by underscores 36-- c) prepend "luaopen_" 37-- So "foo-bar.baz" should result in "luaopen_bar_baz" 38local dash =name : find ( '-' , 1 , true ) 39local modname =dash and name : sub ( dash + 1 ) or name 40local f , err =package . loadlib ( found [ 1 ], 'luaopen_' .. modname : gsub ( '%.' , '_' )) 41return f or error ( err ) 42end 43return nil 44end 45 46-- TODO(bfredl): dedicated state for this? 47if vim . api then 48-- Insert vim._load_package after the preloader at position 2 49table . insert ( package . loaders , 2 , vim . _load_package ) 50end 51 52-- builtin functions which always should be available 53require ( 'vim._core.shared' ) 54 55vim . _submodules ={ 56inspect =true , 57version =true , 58fs =true , 59glob =true , 60iter =true , 61re =true , 62text =true , 63tty =true , 64provider =true , 65} 66 67-- These are for loading runtime modules in the vim namespace lazily. 68setmetatable ( vim , { 69--- @param t table<any,any> 70__index =function ( t , key ) 71if vim . _submodules [ key ] then 72t [ key ] =require ( 'vim.' .. key ) 73return t [ key ] 74elseif key == 'inspect_pos' or key == 'show_pos' then 75require ( 'vim._inspector' ) 76return t [ key ] 77elseif vim . startswith ( key , 'uri_' ) then 78--- @type any? 79local val =require ( 'vim.uri' )[ key ] 80if val ~= nil then 81-- Expose all `vim.uri` functions on the `vim` module. 82t [ key ] =val 83return t [ key ] 84end 85end 86end , 87} ) 88 89--- <Docs described in |vim.empty_dict()| > 90---@nodoc 91--- TODO: should be in vim.shared when vim.shared always uses nvim-lua 92--- @diagnostic disable-next-line:duplicate-set-field 93function vim . empty_dict () 94return setmetatable ( {} , vim . _empty_dict_mt ) 95end 96 97-- only on main thread: functions for interacting with editor state 98if vim . api and not vim . is_thread () then 99require ( 'vim._core.editor' ) 100require ( 'vim._core.system' ) 101end