local o = vim.opt

o.compatible = false
o.number = true

o.cmdheight = 2

o.tabstop = 2
o.shiftwidth = 2
o.expandtab = true

vim.cmd('source ~/.config/nvim/bundles/vim-plug/plug.vim')

local Plug = vim.fn['plug#']
vim.call('plug#begin')

Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'
Plug 'williamboman/mason.nvim'
Plug 'mfussenegger/nvim-jdtls'
Plug 'preservim/nerdtree'

-- Plug('neoclide/coc.nvim', {branch = 'release'})

vim.call('plug#end')


local ok, res = pcall(require, 'mason')

if not ok then
  vim.notify('Couldn load mason', vim.log.levels.ERROR)
  vim.notify(res, vim.log.levels.ERROR)
else
  res.setup()
end

-- local executable = vim.fn[':

vim.api.nvim_create_autocmd({'LspAttach'}, {
  pattern = { '*.c', '*.cpp' },
  callback = function()
    -- vim.api.nvim_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
    if (vim.fn['executable']('clangd')) then
      vim.lsp.start({
        name = 'clangd',
        cmd = {'clangd'}
      })
    end

    o.omnifunc = vim.lsp.omnifunc
  end
})

vim.api.nvim_create_autocmd({'FileType'}, {
  pattern = { 'java' },
  callback = function()
    local config = {
      cmd = { vim.fn.expand('~/.local/share/nvim/mason/bin/jdtls') },
      root_dir = vim.fs.dirname(vim.fs.find({ 'gradlew', '.git', 'mvnw' }, { upward = true })[1]),
    }
    require('jdtls').start_or_attach(config)
    o.omnifunc = 'v:lua.vim.lsp.omnifunc'
  end
})

