chore: format runtime with stylua

This commit is contained in:
Christian Clason
2022-05-09 11:23:51 +02:00
parent 676e9e9334
commit aefdc6783c
37 changed files with 4198 additions and 3569 deletions

View File

@@ -40,26 +40,28 @@ local vim = assert(vim)
-- These are for loading runtime modules lazily since they aren't available in
-- the nvim binary as specified in executor.c
for k,v in pairs {
treesitter=true;
filetype = true;
F=true;
lsp=true;
highlight=true;
diagnostic=true;
keymap=true;
ui=true;
} do vim._submodules[k] = v end
for k, v in pairs({
treesitter = true,
filetype = true,
F = true,
lsp = true,
highlight = true,
diagnostic = true,
keymap = true,
ui = true,
}) do
vim._submodules[k] = v
end
vim.log = {
levels = {
TRACE = 0;
DEBUG = 1;
INFO = 2;
WARN = 3;
ERROR = 4;
OFF = 5;
}
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4,
OFF = 5,
},
}
-- Internal-only until comments in #8107 are addressed.
@@ -77,14 +79,14 @@ function vim._os_proc_info(pid)
if pid == nil or pid <= 0 or type(pid) ~= 'number' then
error('invalid pid')
end
local cmd = { 'ps', '-p', pid, '-o', 'comm=', }
local cmd = { 'ps', '-p', pid, '-o', 'comm=' }
local err, name = vim._system(cmd)
if 1 == err and vim.trim(name) == '' then
return {} -- Process not found.
return {} -- Process not found.
elseif 0 ~= err then
error('command failed: '..vim.fn.string(cmd))
error('command failed: ' .. vim.fn.string(cmd))
end
local _, ppid = vim._system({ 'ps', '-p', pid, '-o', 'ppid=', })
local _, ppid = vim._system({ 'ps', '-p', pid, '-o', 'ppid=' })
-- Remove trailing whitespace.
name = vim.trim(name):gsub('^.*/', '')
ppid = tonumber(ppid) or -1
@@ -101,12 +103,12 @@ function vim._os_proc_children(ppid)
if ppid == nil or ppid <= 0 or type(ppid) ~= 'number' then
error('invalid ppid')
end
local cmd = { 'pgrep', '-P', ppid, }
local cmd = { 'pgrep', '-P', ppid }
local err, rv = vim._system(cmd)
if 1 == err and vim.trim(rv) == '' then
return {} -- Process not found.
return {} -- Process not found.
elseif 0 ~= err then
error('command failed: '..vim.fn.string(cmd))
error('command failed: ' .. vim.fn.string(cmd))
end
local children = {}
for s in rv:gmatch('%S+') do
@@ -124,8 +126,8 @@ end
---
---@see https://github.com/kikito/inspect.lua
---@see https://github.com/mpeterv/vinspect
local function inspect(object, options) -- luacheck: no unused
error(object, options) -- Stub for gen_vimdoc.py
local function inspect(object, options) -- luacheck: no unused
error(object, options) -- Stub for gen_vimdoc.py
end
do
@@ -160,11 +162,11 @@ do
local now = vim.loop.now()
local is_first_chunk = phase < 2
local is_last_chunk = phase == -1 or phase == 3
if is_first_chunk then -- Reset flags.
if is_first_chunk then -- Reset flags.
tdots, tick, got_line1, undo_started, trailing_nl = now, 0, false, false, false
end
if #lines == 0 then
lines = {''}
lines = { '' }
end
if #lines == 1 and lines[1] == '' and not is_last_chunk then
-- An empty chunk can cause some edge cases in streamed pasting,
@@ -172,7 +174,7 @@ do
return true
end
-- Note: mode doesn't always start with "c" in cmdline mode, so use getcmdtype() instead.
if vim.fn.getcmdtype() ~= '' then -- cmdline-mode: paste only 1 line.
if vim.fn.getcmdtype() ~= '' then -- cmdline-mode: paste only 1 line.
if not got_line1 then
got_line1 = (#lines > 1)
-- Escape control characters
@@ -187,9 +189,9 @@ do
if undo_started then
vim.api.nvim_command('undojoin')
end
if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer
if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer
vim.api.nvim_put(lines, 'c', false, true)
elseif phase < 2 and mode:find('^R') and not mode:find('^Rv') then -- Replace mode
elseif phase < 2 and mode:find('^R') and not mode:find('^Rv') then -- Replace mode
-- TODO: implement Replace mode streamed pasting
-- TODO: support Virtual Replace mode
local nchars = 0
@@ -197,26 +199,26 @@ do
nchars = nchars + line:len()
end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local bufline = vim.api.nvim_buf_get_lines(0, row-1, row, true)[1]
local bufline = vim.api.nvim_buf_get_lines(0, row - 1, row, true)[1]
local firstline = lines[1]
firstline = bufline:sub(1, col)..firstline
firstline = bufline:sub(1, col) .. firstline
lines[1] = firstline
lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len())
vim.api.nvim_buf_set_lines(0, row-1, row, false, lines)
elseif mode:find('^[nvV\22sS\19]') then -- Normal or Visual or Select mode
if mode:find('^n') then -- Normal mode
lines[#lines] = lines[#lines] .. bufline:sub(col + nchars + 1, bufline:len())
vim.api.nvim_buf_set_lines(0, row - 1, row, false, lines)
elseif mode:find('^[nvV\22sS\19]') then -- Normal or Visual or Select mode
if mode:find('^n') then -- Normal mode
-- When there was a trailing new line in the previous chunk,
-- the cursor is on the first character of the next line,
-- so paste before the cursor instead of after it.
vim.api.nvim_put(lines, 'c', not trailing_nl, false)
else -- Visual or Select mode
else -- Visual or Select mode
vim.api.nvim_command([[exe "silent normal! \<Del>"]])
local del_start = vim.fn.getpos("'[")
local cursor_pos = vim.fn.getpos('.')
if mode:find('^[VS]') then -- linewise
if cursor_pos[2] < del_start[2] then -- replacing lines at eof
if mode:find('^[VS]') then -- linewise
if cursor_pos[2] < del_start[2] then -- replacing lines at eof
-- create a new line
vim.api.nvim_put({''}, 'l', true, true)
vim.api.nvim_put({ '' }, 'l', true, true)
end
vim.api.nvim_put(lines, 'c', false, false)
else
@@ -227,7 +229,7 @@ do
-- put cursor at the end of the text instead of one character after it
vim.fn.setpos('.', vim.fn.getpos("']"))
trailing_nl = lines[#lines] == ''
else -- Don't know what to do in other modes
else -- Don't know what to do in other modes
return false
end
undo_started = true
@@ -240,9 +242,9 @@ do
vim.api.nvim_command(('echo "%s"'):format(dots))
end
if is_last_chunk then
vim.api.nvim_command('redraw'..(tick > 1 and '|echo ""' or ''))
vim.api.nvim_command('redraw' .. (tick > 1 and '|echo ""' or ''))
end
return true -- Paste will not continue if not returning `true`.
return true -- Paste will not continue if not returning `true`.
end
end
@@ -252,10 +254,12 @@ end
---@see |vim.schedule()|
---@see |vim.in_fast_event()|
function vim.schedule_wrap(cb)
return (function (...)
return function(...)
local args = vim.F.pack_len(...)
vim.schedule(function() cb(vim.F.unpack_len(args)) end)
end)
vim.schedule(function()
cb(vim.F.unpack_len(args))
end)
end
end
-- vim.fn.{func}(...)
@@ -264,7 +268,7 @@ vim.fn = setmetatable({}, {
local _fn
if vim.api[key] ~= nil then
_fn = function()
error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key))
error(string.format('Tried to call API function with vim.fn: use vim.api.%s instead', key))
end
else
_fn = function(...)
@@ -273,7 +277,7 @@ vim.fn = setmetatable({}, {
end
t[key] = _fn
return _fn
end
end,
})
vim.funcref = function(viml_func_name)
@@ -291,9 +295,9 @@ do
--@private
local function make_dict_accessor(scope, handle)
validate {
scope = {scope, 's'};
}
validate({
scope = { scope, 's' },
})
local mt = {}
function mt:__newindex(k, v)
return vim._setvar(scope, handle or 0, k, v)
@@ -343,7 +347,7 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
local region = {}
for l = pos1[1], pos2[1] do
local c1, c2
if regtype:byte() == 22 then -- block selection: take width from regtype
if regtype:byte() == 22 then -- block selection: take width from regtype
c1 = pos1[2]
c2 = c1 + regtype:sub(2)
-- and adjust for non-ASCII characters
@@ -355,10 +359,10 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
c2 = vim.str_byteindex(bufline, c2)
end
else
c1 = (l == pos1[1]) and (pos1[2]) or 0
c1 = (l == pos1[1]) and pos1[2] or 0
c2 = (l == pos2[1]) and (pos2[2] + (inclusive and 1 or 0)) or -1
end
table.insert(region, l, {c1, c2})
table.insert(region, l, { c1, c2 })
end
return region
end
@@ -372,19 +376,22 @@ end
---@param timeout Number of milliseconds to wait before calling `fn`
---@return timer luv timer object
function vim.defer_fn(fn, timeout)
vim.validate { fn = { fn, 'c', true}; }
vim.validate({ fn = { fn, 'c', true } })
local timer = vim.loop.new_timer()
timer:start(timeout, 0, vim.schedule_wrap(function()
timer:stop()
timer:close()
timer:start(
timeout,
0,
vim.schedule_wrap(function()
timer:stop()
timer:close()
fn()
end))
fn()
end)
)
return timer
end
--- Display a notification to the user.
---
--- This function can be overridden by plugins to display notifications using a
@@ -398,9 +405,9 @@ function vim.notify(msg, level, opts) -- luacheck: no unused args
if level == vim.log.levels.ERROR then
vim.api.nvim_err_writeln(msg)
elseif level == vim.log.levels.WARN then
vim.api.nvim_echo({{msg, 'WarningMsg'}}, true, {})
vim.api.nvim_echo({ { msg, 'WarningMsg' } }, true, {})
else
vim.api.nvim_echo({{msg}}, true, {})
vim.api.nvim_echo({ { msg } }, true, {})
end
end
@@ -453,10 +460,10 @@ function vim.on_key(fn, ns_id)
return #on_key_cbs
end
vim.validate {
fn = { fn, 'c', true},
ns_id = { ns_id, 'n', true }
}
vim.validate({
fn = { fn, 'c', true },
ns_id = { ns_id, 'n', true },
})
if ns_id == nil or ns_id == 0 then
ns_id = vim.api.nvim_create_namespace('')
@@ -481,10 +488,13 @@ function vim._on_key(char)
end
if failed_ns_ids[1] then
error(string.format(
"Error executing 'on_key' with ns_ids '%s'\n Messages: %s",
table.concat(failed_ns_ids, ", "),
table.concat(failed_messages, "\n")))
error(
string.format(
"Error executing 'on_key' with ns_ids '%s'\n Messages: %s",
table.concat(failed_ns_ids, ', '),
table.concat(failed_messages, '\n')
)
)
end
end
@@ -511,8 +521,10 @@ function vim._expand_pat(pat, env)
-- Probably just need to do a smarter match than just `:match`
-- Get the last part of the pattern
local last_part = pat:match("[%w.:_%[%]'\"]+$")
if not last_part then return {}, 0 end
local last_part = pat:match('[%w.:_%[%]\'"]+$')
if not last_part then
return {}, 0
end
local parts, search_index = vim._expand_pat_get_parts(last_part)
@@ -529,7 +541,7 @@ function vim._expand_pat(pat, env)
-- Normally, we just have a string
-- Just attempt to get the string directly from the environment
if type(part) == "string" then
if type(part) == 'string' then
key = part
else
-- However, sometimes you want to use a variable, and complete on it
@@ -554,7 +566,7 @@ function vim._expand_pat(pat, env)
local field = rawget(final_env, key)
if field == nil then
local mt = getmetatable(final_env)
if mt and type(mt.__index) == "table" then
if mt and type(mt.__index) == 'table' then
field = rawget(mt.__index, key)
elseif final_env == vim and vim._submodules[key] then
field = vim[key]
@@ -570,18 +582,18 @@ function vim._expand_pat(pat, env)
local keys = {}
---@private
local function insert_keys(obj)
for k,_ in pairs(obj) do
if type(k) == "string" and string.sub(k,1,string.len(match_part)) == match_part then
table.insert(keys,k)
for k, _ in pairs(obj) do
if type(k) == 'string' and string.sub(k, 1, string.len(match_part)) == match_part then
table.insert(keys, k)
end
end
end
if type(final_env) == "table" then
if type(final_env) == 'table' then
insert_keys(final_env)
end
local mt = getmetatable(final_env)
if mt and type(mt.__index) == "table" then
if mt and type(mt.__index) == 'table' then
insert_keys(mt.__index)
end
if final_env == vim then
@@ -602,12 +614,12 @@ vim._expand_pat_get_parts = function(lua_string)
for idx = 1, #lua_string do
local s = lua_string:sub(idx, idx)
if not in_brackets and (s == "." or s == ":") then
if not in_brackets and (s == '.' or s == ':') then
table.insert(parts, accumulator)
accumulator = ''
search_index = idx + 1
elseif s == "[" then
elseif s == '[' then
in_brackets = true
table.insert(parts, accumulator)
@@ -619,7 +631,7 @@ vim._expand_pat_get_parts = function(lua_string)
in_brackets = false
search_index = idx + 1
if string_char == "VAR" then
if string_char == 'VAR' then
table.insert(parts, { accumulator })
accumulator = ''
@@ -631,7 +643,7 @@ vim._expand_pat_get_parts = function(lua_string)
if s == '"' or s == "'" then
string_char = s
elseif s ~= ' ' then
string_char = "VAR"
string_char = 'VAR'
accumulator = s
end
elseif string_char then
@@ -649,7 +661,9 @@ vim._expand_pat_get_parts = function(lua_string)
end
end
parts = vim.tbl_filter(function(val) return #val > 0 end, parts)
parts = vim.tbl_filter(function(val)
return #val > 0
end, parts)
return parts, search_index
end
@@ -677,20 +691,20 @@ function vim._cs_remote(rcid, server_addr, connect_error, args)
local function connection_failure_errmsg(consequence)
local explanation
if server_addr == '' then
explanation = "No server specified with --server"
explanation = 'No server specified with --server'
else
explanation = "Failed to connect to '" .. server_addr .. "'"
if connect_error ~= "" then
explanation = explanation .. ": " .. connect_error
if connect_error ~= '' then
explanation = explanation .. ': ' .. connect_error
end
end
return "E247: " .. explanation .. ". " .. consequence
return 'E247: ' .. explanation .. '. ' .. consequence
end
local f_silent = false
local f_tab = false
local subcmd = string.sub(args[1],10)
local subcmd = string.sub(args[1], 10)
if subcmd == 'tab' then
f_tab = true
elseif subcmd == 'silent' then
@@ -713,16 +727,18 @@ function vim._cs_remote(rcid, server_addr, connect_error, args)
print(vim.fn.rpcrequest(rcid, 'nvim_eval', args[2]))
return { should_exit = true, tabbed = false }
elseif subcmd ~= '' then
return { errmsg='Unknown option argument: ' .. args[1] }
return { errmsg = 'Unknown option argument: ' .. args[1] }
end
if rcid == 0 then
if not f_silent then
vim.notify(connection_failure_errmsg("Editing locally"), vim.log.levels.WARN)
vim.notify(connection_failure_errmsg('Editing locally'), vim.log.levels.WARN)
end
else
local command = {}
if f_tab then table.insert(command, 'tab') end
if f_tab then
table.insert(command, 'tab')
end
table.insert(command, 'drop')
for i = 2, #args do
table.insert(command, vim.fn.fnameescape(args[i]))
@@ -745,11 +761,11 @@ end
---@param plugin string|nil Plugin name that the function will be removed
--- from. Defaults to "Nvim".
function vim.deprecate(name, alternative, version, plugin)
local message = name .. ' is deprecated'
plugin = plugin or "Nvim"
message = alternative and (message .. ', use ' .. alternative .. ' instead.') or message
message = message .. ' See :h deprecated\nThis function will be removed in ' .. plugin .. ' version ' .. version
vim.notify_once(message, vim.log.levels.WARN)
local message = name .. ' is deprecated'
plugin = plugin or 'Nvim'
message = alternative and (message .. ', use ' .. alternative .. ' instead.') or message
message = message .. ' See :h deprecated\nThis function will be removed in ' .. plugin .. ' version ' .. version
vim.notify_once(message, vim.log.levels.WARN)
end
require('vim._meta')