diff options
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r-- | src/nvim/lua/vim.lua | 124 |
1 files changed, 51 insertions, 73 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index b0d0bfc74b..9cd8e232d5 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -1,3 +1,39 @@ +-- Nvim-Lua stdlib: the `vim` module (:help lua-stdlib) +-- +-- Lua code lives in one of three places: +-- 1. runtime/lua/vim/ (the runtime): For "nice to have" features, e.g. the +-- `inspect` and `lpeg` modules. +-- 2. runtime/lua/vim/shared.lua: Code shared between Nvim and tests. +-- 3. src/nvim/lua/: Compiled-into Nvim itself. +-- +-- Guideline: "If in doubt, put it in the runtime". +-- +-- Most functions should live directly on `vim.`, not sub-modules. The only +-- "forbidden" names are those claimed by legacy `if_lua`: +-- $ vim +-- :lua for k,v in pairs(vim) do print(k) end +-- buffer +-- open +-- window +-- lastline +-- firstline +-- type +-- line +-- eval +-- dict +-- beep +-- list +-- command +-- +-- Reference (#6580): +-- - https://github.com/luafun/luafun +-- - https://github.com/rxi/lume +-- - http://leafo.net/lapis/reference/utilities.html +-- - https://github.com/torch/paths +-- - https://github.com/bakpakin/Fennel (pretty print, repl) +-- - https://github.com/howl-editor/howl/tree/master/lib/howl/util + + -- Internal-only until comments in #8107 are addressed. -- Returns: -- {errcode}, {output} @@ -118,79 +154,24 @@ local function _update_package_paths() last_nvim_paths = cur_nvim_paths end -local function gsplit(s, sep, plain) - assert(type(s) == "string") - assert(type(sep) == "string") - assert(type(plain) == "boolean" or type(plain) == "nil") - - local start = 1 - local done = false - - local function pass(i, j, ...) - if i then - assert(j+1 > start, "Infinite loop detected") - local seg = s:sub(start, i - 1) - start = j + 1 - return seg, ... - else - done = true - return s:sub(start) - end - end - - return function() - if done then - return - end - if sep == '' then - if start == #s then - done = true - end - return pass(start+1, start) - end - return pass(s:find(sep, start, plain)) - end -end - -local function split(s,sep,plain) - local t={} for c in gsplit(s, sep, plain) do table.insert(t,c) end - return t -end - +--- Trim whitespace (Lua pattern "%%s") from both sides of a string. +--- +--@see https://www.lua.org/pil/20.2.html +--@param s String to trim +--@returns String with whitespace removed from its beginning and end local function trim(s) - assert(type(s) == "string", "Only strings can be trimmed") - local result = s:gsub("^%s+", ""):gsub("%s+$", "") - return result -end - -local deepcopy - -local function id(v) - return v -end - -local deepcopy_funcs = { - table = function(orig) - local copy = {} - for k, v in pairs(orig) do - copy[deepcopy(k)] = deepcopy(v) - end - return copy - end, - number = id, - string = id, - ['nil'] = id, - boolean = id, -} - -deepcopy = function(orig) - return deepcopy_funcs[type(orig)](orig) + assert(type(s) == 'string', 'Only strings can be trimmed') + return s:match('^%s*(.*%S)') or '' end -local function __index(table, key) - if key == "inspect" then - table.inspect = require("vim.inspect") - return table.inspect +local function __index(t, key) + if key == 'inspect' then + t.inspect = require('vim.inspect') + return t.inspect + elseif require('vim.shared')[key] ~= nil then + -- Expose all `vim.shared` functions on the `vim` module. + t[key] = require('vim.shared')[key] + return t[key] end end @@ -200,9 +181,6 @@ local module = { _os_proc_info = _os_proc_info, _system = _system, trim = trim, - split = split, - gsplit = gsplit, - deepcopy = deepcopy, } setmetatable(module, { |