compe.lua 3.9 KB

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