diff options
| author | srdusr <trevorgray@srdusr.com> | 2022-12-16 23:42:45 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2022-12-16 23:42:45 +0200 |
| commit | af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf (patch) | |
| tree | d80cae9fd509089a3472aaf355b598fdca1b4f6f /.config/nvim/lua/user/utils.lua | |
| parent | 6371e90cc598e52b1d057d4b6d9a7b75d60e2fd0 (diff) | |
| parent | 18e3249ba8bfea656b02308225684e893d0273fa (diff) | |
| download | dotfiles-af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf.tar.gz dotfiles-af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf.zip | |
Merge commit 'af4baf569778ef8324ec0c1c6c0f09d6fb79e6a4' as '.config/nvim'
Diffstat (limited to '.config/nvim/lua/user/utils.lua')
| -rw-r--r-- | .config/nvim/lua/user/utils.lua | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/.config/nvim/lua/user/utils.lua b/.config/nvim/lua/user/utils.lua new file mode 100644 index 0000000..bfb5faa --- /dev/null +++ b/.config/nvim/lua/user/utils.lua @@ -0,0 +1,85 @@ +local fn = vim.fn + +local M = {} + +function M.executable(name) + if fn.executable(name) > 0 then + return true + end + + return false +end + +--- check whether a feature exists in Nvim +--- @feat: string +--- the feature name, like `nvim-0.7` or `unix`. +--- return: bool +M.has = function(feat) + if fn.has(feat) == 1 then + return true + end + + return false +end + +--- Create a dir if it does not exist +function M.may_create_dir(dir) + local res = fn.isdirectory(dir) + + if res == 0 then + fn.mkdir(dir, "p") + end +end + +M['unload_lua_namespace'] = function(prefix) + local prefix_with_dot = prefix .. '.' + for key, value in pairs(package.loaded) do + if key == prefix or key:sub(1, #prefix_with_dot) == prefix_with_dot then + package.loaded[key] = nil + end + end +end + +-- toggle cmp completion +vim.g.cmp_toggle_flag = false -- initialize +local normal_buftype = function() + return vim.api.nvim_buf_get_option(0, "buftype") ~= "prompt" +end +M.toggle_completion = function() + local ok, cmp = pcall(require, "cmp") + if ok then + local next_cmp_toggle_flag = not vim.g.cmp_toggle_flag + if next_cmp_toggle_flag then + print("completion on") + else + print("completion off") + end + cmp.setup({ + enabled = function() + vim.g.cmp_toggle_flag = next_cmp_toggle_flag + if next_cmp_toggle_flag then + return normal_buftype + else + return next_cmp_toggle_flag + end + end, + }) + else + print("completion not available") + end +end + +function M.get_nvim_version() + local actual_ver = vim.version() + + local nvim_ver_str = string.format("%d.%d.%d", actual_ver.major, actual_ver.minor, actual_ver.patch) + return nvim_ver_str +end + +function M.add_pack(name) + local status, error = pcall(vim.cmd, "packadd " .. name) + + return status +end + +return M |
