compe.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. local M = {}
  2. M.config = function()
  3. lvim.builtin.compe = {
  4. enabled = true,
  5. autocomplete = true,
  6. debug = false,
  7. min_length = 1,
  8. preselect = "enable",
  9. throttle_time = 80,
  10. source_timeout = 200,
  11. incomplete_delay = 400,
  12. max_abbr_width = 100,
  13. max_kind_width = 100,
  14. max_menu_width = 100,
  15. documentation = true,
  16. source = {
  17. path = { kind = "  (Path)" },
  18. buffer = { kind = "  (Buffer)" },
  19. calc = { kind = "  (Calc)" },
  20. vsnip = { kind = "  (Snippet)" },
  21. nvim_lsp = { kind = "  (LSP)" },
  22. nvim_lua = false,
  23. spell = { kind = "  (Spell)" },
  24. tags = false,
  25. vim_dadbod_completion = false,
  26. snippets_nvim = false,
  27. ultisnips = false,
  28. treesitter = false,
  29. emoji = { kind = " ﲃ (Emoji)", filetypes = { "markdown", "text" } },
  30. -- for emoji press : (idk if that in compe tho)
  31. },
  32. -- FileTypes in this list won't trigger auto-complete when TAB is pressed. Hitting TAB will insert a tab character
  33. exclude_filetypes = { "md", "markdown", "mdown", "mkd", "mkdn", "mdwn", "text", "txt" },
  34. }
  35. end
  36. M.setup = function()
  37. vim.g.vsnip_snippet_dir = lvim.vsnip_dir
  38. local status_ok, compe = pcall(require, "compe")
  39. if not status_ok then
  40. return
  41. end
  42. compe.setup(lvim.builtin.compe)
  43. local t = function(str)
  44. return vim.api.nvim_replace_termcodes(str, true, true, true)
  45. end
  46. local check_back_space = function()
  47. local col = vim.fn.col "." - 1
  48. if col == 0 or vim.fn.getline("."):sub(col, col):match "%s" then
  49. return true
  50. else
  51. return false
  52. end
  53. end
  54. local remap = vim.api.nvim_set_keymap
  55. remap("i", "<Tab>", 'pumvisible() ? "<C-n>" : "<Tab>"', { silent = true, noremap = true, expr = true })
  56. remap("i", "<S-Tab>", 'pumvisible() ? "<C-p>" : "<S-Tab>"', { silent = true, noremap = true, expr = true })
  57. -- Use (s-)tab to:
  58. --- move to prev/next item in completion menuone
  59. --- jump to prev/next snippet's placeholder
  60. _G.tab_complete = function()
  61. if vim.fn.pumvisible() == 1 then
  62. return t "<C-n>"
  63. elseif vim.fn.call("vsnip#available", { 1 }) == 1 then
  64. return t "<Plug>(vsnip-expand-or-jump)"
  65. elseif check_back_space() then
  66. return t "<Tab>"
  67. else
  68. return vim.fn["compe#complete"]()
  69. end
  70. end
  71. _G.s_tab_complete = function()
  72. if vim.fn.pumvisible() == 1 then
  73. return t "<C-p>"
  74. elseif vim.fn.call("vsnip#jumpable", { -1 }) == 1 then
  75. return t "<Plug>(vsnip-jump-prev)"
  76. else
  77. return t "<S-Tab>"
  78. end
  79. end
  80. vim.api.nvim_set_keymap("i", "<C-Space>", "compe#complete()", { noremap = true, silent = true, expr = true })
  81. -- vim.api.nvim_set_keymap("i", "<CR>", "compe#confirm('<CR>')", { noremap = true, silent = true, expr = true })
  82. vim.api.nvim_set_keymap("i", "<C-e>", "compe#close('<C-e>')", { noremap = true, silent = true, expr = true })
  83. vim.api.nvim_set_keymap("i", "<C-f>", "compe#scroll({ 'delta': +4 })", { noremap = true, silent = true, expr = true })
  84. vim.api.nvim_set_keymap("i", "<C-d>", "compe#scroll({ 'delta': -4 })", { noremap = true, silent = true, expr = true })
  85. end
  86. local is_excluded = function(file_type)
  87. for _, type in ipairs(lvim.builtin.compe.exclude_filetypes) do
  88. if type == file_type then
  89. return true
  90. end
  91. end
  92. return false
  93. end
  94. M.set_tab_keybindings = function()
  95. local file_type = vim.fn.expand "%:e"
  96. -- if is_excluded(file_type) == false then
  97. -- vim.api.nvim_buf_set_keymap(0, "i", "<Tab>", "v:lua.tab_complete()", { expr = true })
  98. -- vim.api.nvim_buf_set_keymap(0, "s", "<Tab>", "v:lua.tab_complete()", { expr = true })
  99. -- vim.api.nvim_buf_set_keymap(0, "i", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
  100. -- vim.api.nvim_buf_set_keymap(0, "s", "<S-Tab>", "v:lua.s_tab_complete()", { expr = true })
  101. -- end
  102. end
  103. return M