refactor: remove fn_bool

It's better to use vim.fn directly instead of creating minor
abstractions like fn_bool.
This commit is contained in:
dundargoc 2024-04-02 12:36:13 +02:00 committed by dundargoc
parent 7560aee595
commit 9dd112dd48
6 changed files with 48 additions and 65 deletions

View File

@ -1,18 +1,6 @@
local M = {}
local health = require('vim.health')
local fn_bool = function(key)
return function(...)
return vim.fn[key](...) == 1
end
end
local has = fn_bool('has')
local executable = fn_bool('executable')
local empty = fn_bool('empty')
local filereadable = fn_bool('filereadable')
local filewritable = fn_bool('filewritable')
local shell_error = function()
return vim.v.shell_error ~= 0
end
@ -62,11 +50,11 @@ local function check_config()
local init_lua = vim.fn.stdpath('config') .. '/init.lua'
local init_vim = vim.fn.stdpath('config') .. '/init.vim'
local vimrc = empty(vim.env.MYVIMRC) and init_lua or vim.env.MYVIMRC
local vimrc = vim.env.MYVIMRC or init_lua
if not filereadable(vimrc) and not filereadable(init_vim) then
if vim.fn.filereadable(vimrc) == 0 and vim.fn.filereadable(init_vim) == 0 then
ok = false
local has_vim = filereadable(vim.fn.expand('~/.vimrc'))
local has_vim = vim.fn.filereadable(vim.fn.expand('~/.vimrc')) == 1
health.warn(
('%s user config file: %s'):format(
-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable',
@ -77,7 +65,7 @@ local function check_config()
end
-- If $VIM is empty we don't care. Else make sure it is valid.
if not empty(vim.env.VIM) and not filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') then
if vim.env.VIM and vim.fn.filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') == 0 then
ok = false
health.error('$VIM is invalid: ' .. vim.env.VIM)
end
@ -121,17 +109,17 @@ local function check_config()
local writeable = true
local shadaopt = vim.fn.split(vim.o.shada, ',')
local shadafile = (
empty(vim.o.shada) and vim.o.shada
vim.o.shada == '' and vim.o.shada
or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '')
)
shadafile = (
empty(vim.o.shadafile)
and (empty(shadafile) and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand(
vim.o.shadafile == ''
and (shadafile == '' and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand(
shadafile
))
or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)
)
if not empty(shadafile) and empty(vim.fn.glob(shadafile)) then
if shadafile ~= '' and vim.fn.glob(shadafile) == '' then
-- Since this may be the first time Nvim has been run, try to create a shada file.
if not pcall(vim.cmd.wshada) then
writeable = false
@ -139,12 +127,15 @@ local function check_config()
end
if
not writeable
or (not empty(shadafile) and (not filereadable(shadafile) or not filewritable(shadafile)))
or (
shadafile ~= ''
and (vim.fn.filereadable(shadafile) == 0 or vim.fn.filewritable(shadafile) ~= 1)
)
then
ok = false
health.error(
'shada file is not '
.. ((not writeable or filereadable(shadafile)) and 'writeable' or 'readable')
.. ((not writeable or vim.fn.filereadable(shadafile) == 1) and 'writeable' or 'readable')
.. ':\n'
.. shadafile
)
@ -160,7 +151,7 @@ local function check_performance()
-- Check buildtype
local buildtype = vim.fn.matchstr(vim.fn.execute('version'), [[\v\cbuild type:?\s*[^\n\r\t ]+]])
if empty(buildtype) then
if buildtype == '' then
health.error('failed to get build type from :version')
elseif vim.regex([[\v(MinSizeRel|Release|RelWithDebInfo)]]):match_str(buildtype) then
health.ok(buildtype)
@ -196,12 +187,12 @@ local function check_rplugin_manifest()
local require_update = false
local handle_path = function(path)
local python_glob = vim.fn.glob(path .. '/rplugin/python*', true, true)
if empty(python_glob) then
if python_glob == '' then
return
end
local python_dir = python_glob[1]
local python_version = vim.fn.fnamemodify(python_dir, ':t')
local python_version = vim.fs.basename(python_dir)
local scripts = vim.fn.glob(python_dir .. '/*.py', true, true)
vim.list_extend(scripts, vim.fn.glob(python_dir .. '/*/__init__.py', true, true))
@ -213,12 +204,12 @@ local function check_rplugin_manifest()
script = vim.fn.tr(vim.fn.fnamemodify(script, ':h'), '\\', '/')
end
if not existing_rplugins[script] then
local msg = vim.fn.printf('"%s" is not registered.', vim.fn.fnamemodify(path, ':t'))
local msg = vim.fn.printf('"%s" is not registered.', vim.fs.basename(path))
if python_version == 'pythonx' then
if not has('python3') then
if vim.fn.has('python3') == 0 then
msg = msg .. ' (python3 not available)'
end
elseif not has(python_version) then
elseif vim.fn.has(python_version) == 0 then
msg = msg .. vim.fn.printf(' (%s not available)', python_version)
else
require_update = true
@ -232,7 +223,7 @@ local function check_rplugin_manifest()
end
end
for _, path in ipairs(vim.fn.map(vim.fn.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do
for _, path in ipairs(vim.fn.map(vim.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do
handle_path(path)
end
@ -244,7 +235,7 @@ local function check_rplugin_manifest()
end
local function check_tmux()
if empty(vim.env.TMUX) or not executable('tmux') then
if not vim.env.TMUX or vim.fn.executable('tmux') == 0 then
return
end
@ -255,7 +246,7 @@ local function check_tmux()
if shell_error() then
health.error('command failed: ' .. cmd .. '\n' .. out)
return 'error'
elseif empty(val) then
elseif val == '' then
cmd = 'tmux show-option -qvgs ' .. option -- try session scope
out = vim.fn.system(vim.fn.split(cmd))
val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
@ -274,7 +265,7 @@ local function check_tmux()
{ 'set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10', suggest_faq }
local tmux_esc_time = get_tmux_option('escape-time')
if tmux_esc_time ~= 'error' then
if empty(tmux_esc_time) then
if tmux_esc_time == '' then
health.error('`escape-time` is not set', suggestions)
elseif tonumber(tmux_esc_time) > 300 then
health.error('`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', suggestions)
@ -286,7 +277,7 @@ local function check_tmux()
-- check focus-events
local tmux_focus_events = get_tmux_option('focus-events')
if tmux_focus_events ~= 'error' then
if empty(tmux_focus_events) or tmux_focus_events ~= 'on' then
if tmux_focus_events == '' or tmux_focus_events ~= 'on' then
health.warn(
"`focus-events` is not enabled. |'autoread'| may not work.",
{ '(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on' }
@ -301,7 +292,7 @@ local function check_tmux()
local cmd = 'tmux show-option -qvg default-terminal'
local out = vim.fn.system(vim.fn.split(cmd))
local tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
if empty(tmux_default_term) then
if tmux_default_term == '' then
cmd = 'tmux show-option -qvgs default-terminal'
out = vim.fn.system(vim.fn.split(cmd))
tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
@ -341,7 +332,7 @@ local function check_tmux()
end
local function check_terminal()
if not executable('infocmp') then
if vim.fn.executable('infocmp') == 0 then
return
end
@ -354,13 +345,12 @@ local function check_terminal()
if
shell_error()
and (
not has('win32')
or empty(
vim.fn.matchstr(
vim.fn.has('win32') == 0
or vim.fn.matchstr(
out,
[[infocmp: couldn't open terminfo file .\+\%(conemu\|vtpcon\|win32con\)]]
)
)
== ''
)
then
health.error('command failed: ' .. cmd .. '\n' .. out)
@ -368,14 +358,14 @@ local function check_terminal()
health.info(
vim.fn.printf(
'key_backspace (kbs) terminfo entry: `%s`',
(empty(kbs_entry) and '? (not found)' or kbs_entry)
(kbs_entry == '' and '? (not found)' or kbs_entry)
)
)
health.info(
vim.fn.printf(
'key_dc (kdch1) terminfo entry: `%s`',
(empty(kbs_entry) and '? (not found)' or kdch1_entry)
(kbs_entry == '' and '? (not found)' or kdch1_entry)
)
)
end

View File

@ -1,5 +1,4 @@
local health = vim.health
local executable = health.executable
local M = {}
@ -8,8 +7,8 @@ function M.check()
if
os.getenv('TMUX')
and executable('tmux')
and executable('pbpaste')
and vim.fn.executable('tmux') == 1
and vim.fn.executable('pbpaste') == 1
and not health.cmd_ok('pbpaste')
then
local tmux_version = string.match(vim.fn.system('tmux -V'), '%d+%.%d+')

View File

@ -1,5 +1,4 @@
local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {}
@ -12,8 +11,12 @@ function M.check()
end
if
not executable('node')
or (not executable('npm') and not executable('yarn') and not executable('pnpm'))
vim.fn.executable('node') == 0
or (
vim.fn.executable('npm') == 0
and vim.fn.executable('yarn') == 0
and vim.fn.executable('pnpm') == 0
)
then
health.warn(
'`node` and `npm` (or `yarn`, `pnpm`) must be in $PATH.',
@ -50,9 +53,9 @@ function M.check()
health.info('Nvim node.js host: ' .. host)
local manager = 'npm'
if executable('yarn') then
if vim.fn.executable('yarn') == 1 then
manager = 'yarn'
elseif executable('pnpm') then
elseif vim.fn.executable('pnpm') == 1 then
manager = 'pnpm'
end

View File

@ -1,5 +1,4 @@
local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {}
@ -60,7 +59,7 @@ local function check_bin(bin)
if not is(bin, 'file') and (not iswin or not is(bin .. '.exe', 'file')) then
health.error('"' .. bin .. '" was not found.')
return false
elseif not executable(bin) then
elseif vim.fn.executable(bin) == 0 then
health.error('"' .. bin .. '" is not executable.')
return false
end
@ -69,7 +68,7 @@ end
-- Fetch the contents of a URL.
local function download(url)
local has_curl = executable('curl')
local has_curl = vim.fn.executable('curl') == 1
if has_curl and vim.fn.system({ 'curl', '-V' }):find('Protocols:.*https') then
local out, rc = health.system({ 'curl', '-sL', url }, { stderr = true, ignore_error = true })
if rc ~= 0 then
@ -77,7 +76,7 @@ local function download(url)
else
return out
end
elseif executable('python') then
elseif vim.fn.executable('python') == 1 then
local script = "try:\n\
from urllib.request import urlopen\n\
except ImportError:\n\
@ -284,7 +283,7 @@ function M.check()
if
path_bin ~= vim.fs.normalize(python_exe)
and vim.tbl_contains(python_multiple, path_bin)
and executable(path_bin)
and vim.fn.executable(path_bin) == 1
then
python_multiple[#python_multiple + 1] = path_bin
end
@ -472,7 +471,7 @@ function M.check()
bin_dir,
table.concat(
vim.tbl_map(function(v)
return vim.fn.fnamemodify(v, ':t')
return vim.fs.basename(v)
end, venv_bins),
', '
)

View File

@ -1,5 +1,4 @@
local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {}
@ -11,7 +10,7 @@ function M.check()
return
end
if not executable('ruby') or not executable('gem') then
if vim.fn.executable('ruby') == 0 or vim.fn.executable('gem') == 0 then
health.warn(
'`ruby` and `gem` must be in $PATH.',
'Install Ruby and verify that `ruby` and `gem` commands work.'

View File

@ -386,7 +386,7 @@ local path2name = function(path)
path = path:gsub('^.*/lua/', '')
-- Remove the filename (health.lua)
path = vim.fn.fnamemodify(path, ':h')
path = vim.fs.dirname(path)
-- Change slashes to dots
path = path:gsub('/', '.')
@ -497,11 +497,4 @@ function M._check(mods, plugin_names)
vim.print('')
end
local fn_bool = function(key)
return function(...)
return vim.fn[key](...) == 1
end
end
M.executable = fn_bool('executable')
return M