Advertisement
Guest User

Untitled

a guest
Dec 19th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.91 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------
  2. ---------------------------------------------------------------------------------------
  3. --[[
  4.     __                     __   _    __
  5.    / /   ____  _________ _/ /  | |  / /___ ___________
  6.   / /   / __ \/ ___/ __ `/ /   | | / / __ `/ ___/ ___/
  7.  / /___/ /_/ / /__/ /_/ / /    | |/ / /_/ / /  (__  )
  8. /_____/\____/\___/\__,_/_/     |___/\__,_/_/  /____/
  9.  
  10. --]]
  11. local lsp = require('lspconfig')
  12. local completion = require('completion') --> CAN US BUILT IN OMNIFUNC BUT WE USE THIS FOR NOW
  13. local api = vim.api
  14. local configs = require('lspconfig/configs')
  15. local util = require 'lspconfig/util'
  16. local cwd = vim.loop.cwd
  17. local lsp_status = require('lsp-status') --> LSP progress, errors etc in your statusline
  18.  
  19. --ERROR HANDLING INSPO --> https://raw.githubusercontent.com/nymann/dotfiles/master/dots/nvim/lua/lsp.lua
  20. --local has_lsp, lspconfig = pcall(require, 'lspconfig')
  21. --local has_completion, completion = pcall(require, 'completion')
  22. --local has_diagnostic, diagnostic = pcall(require, 'diagnostic')
  23.  
  24. --if not has_lsp then
  25.     --return
  26. --end
  27.  
  28. --Configure the exclusion pattterns
  29. local exclude_patterns = {
  30.   '**/node_modules/**/*',
  31.   '**/bin/**/*',
  32.   '**/obj/**/*',
  33.   '/tmp/**/*'
  34. }
  35.  
  36.  
  37.  
  38. ---------------------------------------------------------------------------------------
  39. ---------------------------------------------------------------------------------------
  40. --[[
  41.    ______                      __     __  _                ____        __  _
  42.   / ____/___  ____ ___  ____  / /__  / /_(_)___  ____     / __ \____  / /_(_)___  ____  _____
  43.  / /   / __ \/ __ `__ \/ __ \/ / _ \/ __/ / __ \/ __ \   / / / / __ \/ __/ / __ \/ __ \/ ___/
  44. / /___/ /_/ / / / / / / /_/ / /  __/ /_/ / /_/ / / / /  / /_/ / /_/ / /_/ / /_/ / / / (__  )
  45. \____/\____/_/ /_/ /_/ .___/_/\___/\__/_/\____/_/ /_/   \____/ .___/\__/_/\____/_/ /_/____/
  46.                     /_/                                     /_/
  47. --]]
  48. --" Set completeopt to have a better completion experience
  49. api.nvim_command('set completeopt=longest,menuone,noinsert,noselect')
  50. --" Use <Tab> and <S-Tab> to navigate through popup menu
  51. api.nvim_command('inoremap <expr> <Tab>   pumvisible() ? "<C-n>" : "<Tab>"')
  52. api.nvim_command('inoremap <expr> <S-Tab> pumvisible() ? "<C-p>" : "<S-Tab>"')
  53. --" Avoid showing message extra message when using completion
  54. api.nvim_command('set shortmess+=c')
  55. --api.nvim_command('')
  56.  
  57.  
  58.  
  59.  
  60. ---------------------------------------------------------------------------------------
  61. ---------------------------------------------------------------------------------------
  62. --[[
  63.     __  __     __                   ______                 __  _
  64.    / / / /__  / /___  ___  _____   / ____/_  ______  _____/ /_(_)___  ____  _____
  65.   / /_/ / _ \/ / __ \/ _ \/ ___/  / /_  / / / / __ \/ ___/ __/ / __ \/ __ \/ ___/
  66.  / __  /  __/ / /_/ /  __/ /     / __/ / /_/ / / / / /__/ /_/ / /_/ / / / (__  )
  67. /_/ /_/\___/_/ .___/\___/_/     /_/    \__,_/_/ /_/\___/\__/_/\____/_/ /_/____/
  68.             /_/
  69. --]]
  70.  
  71. --A custom mapper function to make mapping our lsp functions to vim key sequences less verbose
  72. local mapper = function(mode, key, result)
  73.   api.nvim_buf_set_keymap(0, mode, key, "<cmd>lua "..result.."<cr>", {noremap = true, silent = true})
  74. end
  75.  
  76. -- stolen from https://github.com/fsouza/vimfiles
  77. local get_python_tool = function(bin_name)
  78.   local result = bin_name
  79.   if os.getenv('VIRTUAL_ENV') then
  80.     local venv_bin_name = os.getenv('VIRTUAL_ENV') .. '/bin/' .. bin_name
  81.     if vim.fn.executable(venv_bin_name) == 1 then
  82.       result = venv_bin_name
  83.     end
  84.   end
  85.   return result
  86. end
  87.  
  88.  
  89.  
  90.  
  91. ---------------------------------------------------------------------------------------
  92. ---------------------------------------------------------------------------------------
  93. --[[
  94.     __   _____ ____     __________  _   _______________________
  95.    / /  / ___// __ \   / ____/ __ \/ | / / ____/  _/ ____/ ___/
  96.   / /   \__ \/ /_/ /  / /   / / / /  |/ / /_   / // / __ \__ \
  97.  / /______/ / ____/  / /___/ /_/ / /|  / __/ _/ // /_/ /___/ /
  98. /_____/____/_/       \____/\____/_/ |_/_/   /___/\____//____/
  99. --]]
  100.  
  101. -- async formatting
  102. -- https://www.reddit.com/r/neovim/comments/jvisg5/lets_talk_formatting_again/
  103. vim.lsp.handlers["textDocument/formatting"] = function(err, _, result, _, bufnr)
  104.     if err ~= nil or result == nil then
  105.         return
  106.     end
  107.     if not vim.api.nvim_buf_get_option(bufnr, "modified") then
  108.         local view = vim.fn.winsaveview()
  109.         vim.lsp.util.apply_text_edits(result, bufnr)
  110.         vim.fn.winrestview(view)
  111.         vim.api.nvim_command("noautocmd :update")
  112.     end
  113. end
  114.  
  115. -- Diagnostics
  116. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
  117.   vim.lsp.diagnostic.on_publish_diagnostics, {
  118.     underline = false,
  119.     virtual_text = true,
  120.     signs = true,
  121.     update_in_insert = false,
  122.   }
  123. )
  124.  
  125. -- Statusline
  126. --lsp_status.config({
  127.   --kind_labels = vim.g.completion_customize_lsp_label
  128. --})
  129. lsp_status.register_progress()
  130.  
  131.  
  132.  
  133.  
  134.  
  135. ---------------------------------------------------------------------------------------
  136. ---------------------------------------------------------------------------------------
  137. --[[
  138.     ___   __  __             __       ______                 __  _
  139.    /   | / /_/ /_____ ______/ /_     / ____/_  ______  _____/ /_(_)___  ____
  140.   / /| |/ __/ __/ __ `/ ___/ __ \   / /_  / / / / __ \/ ___/ __/ / __ \/ __ \
  141.  / ___ / /_/ /_/ /_/ / /__/ / / /  / __/ / /_/ / / / / /__/ /_/ / /_/ / / / /
  142. /_/  |_\__/\__/\__,_/\___/_/ /_/  /_/    \__,_/_/ /_/\___/\__/_/\____/_/ /_/
  143.  
  144. --]]
  145. --When our LSP starts, this is what happens. Completion enabled, set some mappings, print lsp starting message
  146. local custom_attach = function(client,bufnr) --> Added client,bufnr works also without, inspo from https://github.com/kuator/nvim/blob/master/lua/plugins/lsp.lua
  147.   --ERROR HANDLING INSPO
  148.   --if has_status then
  149.         --lsp_status.on_attach(client)
  150.     --end
  151.  
  152.     --if has_diagnostic then
  153.         --diagnostic.on_attach()
  154.     --end
  155.  
  156.     --if has_completion then
  157.         --completion.on_attach({
  158.             --sorter = 'alphabet',
  159.             --matcher = {'exact', 'fuzzy'}
  160.         --})
  161.     --end
  162.   completion.on_attach(client,bufnr)
  163.   lsp_status.on_attach(client) --> REQUIRED for lsp statusbar
  164.   --vim.lsp.set_log_level('debug') ENABLE LOGGING
  165.   -- Move cursor to the next and previous diagnostic
  166.   mapper('n', '<leader>dn', 'vim.lsp.diagnostic.goto_next()')
  167.   mapper('n', '<leader>dp', 'vim.lsp.diagnostic.goto_prev()')
  168.   -- Keybindings for LSPs
  169.   -- Close quickfix with :cclose
  170.   mapper('n', 'gd', 'vim.lsp.buf.definition()')
  171.   mapper('n', 'gh', 'vim.lsp.buf.hover()')
  172.   mapper('n', 'gD', 'vim.lsp.buf.implementation()')
  173.   mapper('n', '<c-k>', 'vim.lsp.buf.signature_help()')
  174.   mapper('n', '1gD', 'vim.lsp.buf.type_definition()')
  175.   mapper('n', 'gr', 'vim.lsp.buf.references()')
  176.   mapper('n', 'g0', 'vim.lsp.buf.document_symbol()')
  177.   mapper('n', 'gW', 'vim.lsp.buf.workspace_symbol()')
  178.   print("LSP Started.. Let's get this bread homie")
  179.   --vim.fn.nvim_set_keymap("n", "<leader>ge", "<cmd>lua vim.lsp.buf.declaration()<CR>", {noremap = true, silent = true})
  180.   --vim.fn.nvim_set_keymap("n", "<leader>gh", "<cmd>lua vim.lsp.buf.hover()<CR>", {noremap = true, silent = true})
  181.   --vim.fn.nvim_set_keymap("n", "<leader>gf", "<cmd>lua vim.lsp.buf.formatting()<CR>", {noremap = true, silent = true})
  182.   --vim.fn.nvim_set_keymap("n", "<leader>gy", "<cmd>lua vim.lsp.buf.type_definition()<CR>", {noremap = true, silent = true})
  183.   --vim.fn.nvim_set_keymap("n", "<leader>gr", "<cmd>lua vim.lsp.buf.references()<CR>", {noremap = true, silent = true})
  184.   --vim.fn.nvim_set_keymap("n", "<leader>gt", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", {noremap = true, silent = true})
  185.   --vim.fn.nvim_set_keymap("n", "<leader>gw", "<cmd>lua vim.lsp.buf.workspace_symbol()<CR>", {noremap = true, silent = true})
  186.   --vim.fn.nvim_set_keymap("n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", {noremap = true, silent = true})
  187.   --vim.fn.nvim_set_keymap("n", "<a-.>", "<cmd>lua vim.lsp.buf.code_action()<CR>", {noremap = true, silent = true})
  188.   --vim.api.nvim_command('setlocal omnifunc=v:lua.vim.lsp.omnifunc')
  189. end
  190.  
  191. ---------------------------------------------------------------------------------------
  192. ---------------------------------------------------------------------------------------
  193. --[[
  194.     __                                                ______            _____
  195.    / /   ____ _____  ____ ___  ______ _____ ____     / ____/___  ____  / __(_)___ ______
  196.   / /   / __ `/ __ \/ __ `/ / / / __ `/ __ `/ _ \   / /   / __ \/ __ \/ /_/ / __ `/ ___/
  197.  / /___/ /_/ / / / / /_/ / /_/ / /_/ / /_/ /  __/  / /___/ /_/ / / / / __/ / /_/ (__  )
  198. /_____/\__,_/_/ /_/\__, /\__,_/\__,_/\__, /\___/   \____/\____/_/ /_/_/ /_/\__, /____/
  199.                   /____/            /____/                                /____/
  200. --]]
  201. --NOTE: The rest of this file deals with language specific configurations
  202. ---------------------------------------------------------------------------------------
  203. ---------------------------------------------------------------------------------------
  204. --[[
  205.  _    ___                          _       __
  206. | |  / (_)___ ___  _______________(_)___  / /_
  207. | | / / / __ `__ \/ ___/ ___/ ___/ / __ \/ __/
  208. | |/ / / / / / / (__  ) /__/ /  / / /_/ / /_
  209. |___/_/_/ /_/ /_/____/\___/_/  /_/ .___/\__/
  210.                                 /_/
  211. --]]
  212. lsp.vimls.setup {on_attach = custom_attach, root_dir = cwd}
  213.  
  214. ---------------------------------------------------------------------------------------
  215. --[[
  216.     ____        __  __
  217.    / __ \__  __/ /_/ /_  ____  ____
  218.   / /_/ / / / / __/ __ \/ __ \/ __ \
  219.  / ____/ /_/ / /_/ / / / /_/ / / / /
  220. /_/    \__, /\__/_/ /_/\____/_/ /_/
  221.       /____/
  222. --]]
  223. lsp.pyls.setup{
  224.   on_attach = custom_attach
  225. }
  226.  
  227.  
  228. ---------------------------------------------------------------------------------------
  229. --[[
  230.   ______                                _       __
  231.  /_  __/_  ______  ___  _______________(_)___  / /_
  232.   / / / / / / __ \/ _ \/ ___/ ___/ ___/ / __ \/ __/
  233.  / / / /_/ / /_/ /  __(__  ) /__/ /  / / /_/ / /_
  234. /_/  \__, / .___/\___/____/\___/_/  /_/ .___/\__/
  235.     /____/_/                         /_/
  236. --]]
  237. lsp.tsserver.setup {on_attach = custom_attach,
  238.   root_dir = function(fname)
  239.     return util.find_git_ancestor(fname) or
  240.       util.path.dirname(fname)
  241.   end,
  242. }
  243.             --root_dir = util.root_pattern('package.json', 'tsconfig.json', '.git') or cwd
  244.             --root_dir = vim.loop.cwd --Sets global cwd
  245.  
  246.   --root_dir = function() --NOT WORKING: RETURNS FUNCTION NAME NOT THE DATA
  247.     --return cwd
  248.   --end,
  249.             --root_dir = util.root_pattern('package.json', 'tsconfig.json', '.git') or cwd
  250. --local util = require 'lspconfig/util'
  251. --local cwd = vim.loop.cwd
  252. --SetTsServerCWD = function()
  253.     --return     util.root_pattern('package.json', 'tsconfig.json', '.git') or cwd
  254. --end
  255.  
  256. ---------------------------------------------------------------------------------------
  257. --[[
  258.        __
  259.       / /___ __   ______ _
  260.  __  / / __ `/ | / / __ `/
  261. / /_/ / /_/ /| |/ / /_/ /
  262. \____/\__,_/ |___/\__,_/
  263. --]]
  264. --NOTE: NOT WORKING
  265. lsp.jdtls.setup {on_attach = custom_attach,
  266.                     root_dir = cwd
  267. }
  268.  
  269.       --configs.jdtls.handlers == {
  270.       --['client/registerCapability'] = function(_, _, _, _)
  271.         --return {
  272.           --result = nil;
  273.           --error = nil;
  274.         --}
  275.       --end
  276.   --},
  277.  
  278.         --root_dir = lsp.util.root_pattern('.git', 'pom.xml', 'build.xml')
  279.  
  280.  
  281. ---------------------------------------------------------------------------------------
  282. --[[
  283.     __
  284.    / /   __  ______ _
  285.   / /   / / / / __ `/
  286.  / /___/ /_/ / /_/ /
  287. /_____/\__,_/\__,_/
  288. --]]
  289.  
  290. lsp.sumneko_lua.setup {
  291.   on_attach = custom_attach,
  292.   root_dir = function(fname)
  293.     return util.find_git_ancestor(fname) or
  294.       util.path.dirname(fname)
  295.   end,
  296.   --root_dir = vim.loop.cwd,
  297.   --root_dir = util.root_pattern('.git') or cwd, --Not working
  298.     capabilities = {
  299.             textDocument = {
  300.               completion = {
  301.                completionItem = {
  302.                  snippetSupport=true
  303.                }
  304.               }
  305.             }
  306.          },
  307.          lsp_status.capabilities, --> REQUIRED FOR LSP STATUS
  308.     settings = {
  309.         Lua = {
  310.             runtime = {
  311.                 -- Tell the language server which version of Lua you're using (LuaJIT in the case of Neovim)
  312.                 version = 'LuaJIT',
  313.                 -- Setup your lua path
  314.                 path = vim.split(package.path, ';'),
  315.             },
  316.             diagnostics = {
  317.                 -- Get the language server to recognize the `vim` global
  318.                 globals = {'vim'},
  319.             },
  320.             workspace = {
  321.                 -- Make the server aware of Neovim runtime files
  322.                 library = {
  323.                     [vim.fn.expand('$VIMRUNTIME/lua')] = true,
  324.                     [vim.fn.expand('$VIMRUNTIME/lua/vim/lsp')] = true,
  325.                 },
  326.             },
  327.         },
  328.     },
  329. }
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement