diff options
author | Romain Gonçalves <me@rgoncalves.se> | 2021-12-23 18:28:03 +0000 |
---|---|---|
committer | Romain Gonçalves <me@rgoncalves.se> | 2021-12-23 18:28:03 +0000 |
commit | 0f08d04698c814955116b6bae50752e64b774d8f (patch) | |
tree | 8cf9a33557093eebfd25aab2872e97639c7e2f62 /.config/nvim/lua/utils.lua | |
download | dots-0f08d04698c814955116b6bae50752e64b774d8f.tar.gz |
Thu Dec 23 06:28:03 PM UTC 2021
Diffstat (limited to '.config/nvim/lua/utils.lua')
-rwxr-xr-x | .config/nvim/lua/utils.lua | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/.config/nvim/lua/utils.lua b/.config/nvim/lua/utils.lua new file mode 100755 index 0000000..ae164e8 --- /dev/null +++ b/.config/nvim/lua/utils.lua @@ -0,0 +1,46 @@ +local utils = {} + +local scopes = {o = vim.o, b = vim.bo, w = vim.wo} + +--- Apply a setting with a specified scope +function utils.opt(scope, key, value) + scopes[scope][key] = value + if scope ~= 'o' then scopes['o'][key] = value end +end + +--- Apply an array of settings +function utils.opts(params) + for i, item in ipairs(params) do + utils.opt(item[1], item[2], item[3]) + end +end + +--- Key mapping +function utils.map(mode, lhs, rhs, opts) + local options = {noremap = true} + if opts then options = vim.tbl_extend('force', options, opts) end + vim.api.nvim_set_keymap(mode, lhs, rhs, options) +end + +function utils.get_python_venv() + local lsputil = require('lspconfig/util') + local match + + if vim.env.VIRTUAL_ENV then + return vim.env.VIRTUAL_ENV + end + + match = vim.fn.glob(lsputil.path.join(vim.fn.getcwd(), 'Pipfile')) + if match ~= '' then + return vim.fn.trim(vim.fn.system('PIPENV_PIPFILE=' .. match .. ' pipenv --venv')) + end + + match = vim.fn.system('test -f poetry.lock && poetry env info -p') + if vim.v.shell_error then + return vim.fn.trim(match) + end + + return '' +end + +return utils |