Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local packer = require("packer")
- packer.init({
- enable = true,
- threshold = 0,
- })
- local use = packer.use
- packer.reset()
- use 'wbthomason/packer.nvim'
- use({ 'hrsh7th/nvim-cmp', config = function()
- local cmp = require('cmp')
- local sources = require('cmp.config.sources')
- local g = vim.g
- local o = vim.opt
- o.completeopt = 'menu,menuone,noselect'
- g.vsnip_snippet_dir = vim.fn.stdpath('config') .. '/vsnip'
- g.vsnip_filetypes = {
- javascriptreact = { 'javascript', 'html' },
- typescriptreact = { 'typescript', 'html' },
- svelte = { 'javascript' },
- vue = { 'html' },
- }
- local has_words_before = function()
- local line, col = unpack(vim.api.nvim_win_get_cursor(0))
- return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil
- end
- local feedkey = function(key, mode)
- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
- end
- cmp.setup({
- snippet = {
- expand = function(args)
- vim.fn['vsnip#anonymous'](args.body) -- For `vsnip` user.
- end,
- },
- mapping = {
- ['<Down>'] = cmp.mapping(cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }), { 'i' }),
- ['<Up>'] = cmp.mapping(cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }), { 'i' }),
- ['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
- ['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
- ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
- ['<Tab>'] = cmp.mapping(function(fallback)
- if cmp.visible() then
- cmp.select_next_item()
- elseif vim.fn['vsnip#available']() == 1 then
- feedkey('<Plug>(vsnip-expand-or-jump)', '')
- elseif has_words_before() then
- cmp.complete()
- else
- fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
- end
- end, { 'i', 's' }),
- ['<S-Tab>'] = cmp.mapping(function()
- if cmp.visible() then
- cmp.select_prev_item()
- elseif vim.fn['vsnip#jumpable'](-1) == 1 then
- feedkey('<Plug>(vsnip-jump-prev)', '')
- end
- end, { 'i', 's' }),
- ['<C-e>'] = cmp.mapping({
- i = cmp.mapping.abort(),
- c = cmp.mapping.close(),
- }),
- ['<CR>'] = cmp.mapping.confirm({
- behavior = cmp.ConfirmBehavior.Replace,
- select = true,
- }),
- },
- sources = sources({
- { name = 'nvim_lsp' },
- { name = 'nvim_lua' },
- { name = 'vsnip' },
- {
- name = 'buffer',
- option = {
- keyword_length = 3,
- get_bufnrs = function()
- return vim.api.nvim_list_bufs()
- end,
- },
- },
- }),
- })
- end })
- use('hrsh7th/cmp-nvim-lua')
- use('hrsh7th/cmp-nvim-lsp')
- use('hrsh7th/cmp-buffer')
- use('hrsh7th/vim-vsnip')
- use('hrsh7th/cmp-vsnip')
- use('williamboman/nvim-lsp-installer')
- use('b0o/schemastore.nvim')
- use({
- 'neovim/nvim-lspconfig', config = function()
- require("nvim-lsp-installer").setup({
- ensure_installed = {"cssls", "html", "jsonls", "sumneko_lua", "volar"},
- ui = {
- icons = {
- server_installed = "✓",
- server_pending = "➜",
- server_uninstalled = "✗",
- },
- },
- })
- local lspconfig = require('lspconfig')
- local util = require('lspconfig.util')
- local capabilities = vim.lsp.protocol.make_client_capabilities()
- local cmp_capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
- local map = function(lhs, rhs, bufnr)
- vim.keymap.set('n', lhs, rhs, { silent = true, buffer = bufnr })
- end
- local on_attach_common = function(client, bufnr)
- map('gd', ':LspDefination<CR>', bufnr)
- map('gD', ':LspDeclaration<CR>', bufnr)
- map('K', vim.lsp.buf.hover, bufnr)
- map('gi', vim.lsp.buf.implementation, bufnr)
- map('gt', vim.lsp.buf.type_definition, bufnr)
- map('gw', vim.lsp.buf.document_symbol, bufnr)
- map('gW', vim.lsp.buf.workspace_symbol, bufnr)
- map('gac', ':LspCodeAction<CR>', bufnr)
- map('<F2>', ':LspRename<CR>', bufnr)
- map('gQ', vim.lsp.diagnostic.set_loclist, bufnr)
- map('<Space>=', function()
- vim.lsp.buf.format({ async = true })
- end, bufnr)
- print(string.format('LSP attached (%s)', client.name))
- end
- -- sumneko_lua {{{
- local runtime_path = vim.split(package.path, ';')
- local table = require('table')
- table.insert(runtime_path, 'lua/?.lua')
- table.insert(runtime_path, 'lua/?/init.lua')
- lspconfig.sumneko_lua.setup({
- capabilties = cmp_capabilities,
- on_attach = on_attach_common,
- flags = { debounce_text_changes = 150 },
- settings = {
- Lua = {
- runtime = { version = 'LuaJIT', path = runtime_path },
- completion = { enable = true, callSnippet = 'Both' },
- diagnostics = {
- enable = true,
- globals = { 'vim', 'describe' },
- disable = { 'lowercase-global' },
- },
- workspace = {
- library = vim.api.nvim_get_runtime_file('', true),
- maxPreload = 2000,
- preloadFileSize = 1000,
- },
- hint = { enable = true },
- telemetry = { enable = false },
- },
- },
- }) -- }}}
- lspconfig.jsonls.setup({ -- {{{
- capabilties = cmp_capabilities,
- on_attach = on_attach_common,
- flags = { debounce_text_changes = 150 },
- settings = {
- json = {
- schemas = require('schemastore').json.schemas(),
- },
- },
- }) -- }}}
- lspconfig.html.setup({ -- {{{
- capabilties = cmp_capabilities,
- on_attach = on_attach_common,
- flags = { debounce_text_changes = 150 },
- }) -- }}}
- lspconfig.cssls.setup({ -- {{{
- capabilties = cmp_capabilities,
- on_attach = on_attach_common,
- flags = { debounce_text_changes = 150 },
- }) -- }}}
- local function get_typescript_server_path(root_dir)
- local project_root = util.find_node_modules_ancestor(root_dir)
- local local_tsserverlib = project_root ~= nil
- and util.path.join(project_root, 'node_modules', 'typescript', 'lib', 'tsserverlibrary.js')
- local global_tsserverlib = os.getenv('NVM_DIR')
- .. '/versions/node/v18.0.0/lib/node_modules/typescript/lib/tsserverlibrary.js'
- if local_tsserverlib and util.path.exists(local_tsserverlib) then
- return local_tsserverlib
- else
- return global_tsserverlib
- end
- end
- lspconfig.volar.setup({
- capabilties = cmp_capabilities,
- on_attach = on_attach_common,
- flags = { debounce_text_changes = 150 },
- on_new_config = function(new_config, new_root_dir)
- new_config.init_options.typescript.serverPath = get_typescript_server_path(new_root_dir)
- end,
- })
- end
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement