summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/utils.lua
blob: ae164e83052f1f5d808a6b78bc43493de15e4465 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
remember that computers suck.