summaryrefslogtreecommitdiff
path: root/start/cmp/lua/cmp/config/mapping.lua
blob: c2028c890e83a9108f8790f6e6db566b76e7cfa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
local types = require('cmp.types')
local misc = require('cmp.utils.misc')
local feedkeys = require('cmp.utils.feedkeys')
local keymap = require('cmp.utils.keymap')

local mapping = setmetatable({}, {
  __call = function(_, invoke, modes)
    if type(invoke) == 'function' then
      local map = {}
      for _, mode in ipairs(modes or { 'i' }) do
        map[mode] = invoke
      end
      return map
    end
    return invoke
  end,
})

---Mapping preset configuration.
mapping.preset = {}

---Mapping preset insert-mode configuration.
mapping.preset.insert = function(override)
  return misc.merge(override or {}, {
    ['<Down>'] = {
      i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Select }),
    },
    ['<Up>'] = {
      i = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Select }),
    },
    ['<C-n>'] = {
      i = mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }),
    },
    ['<C-p>'] = {
      i = mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }),
    },
    ['<C-y>'] = {
      i = mapping.confirm({ select = false }),
    },
    ['<C-e>'] = {
      i = mapping.abort(),
    },
  })
end

---Mapping preset cmdline-mode configuration.
mapping.preset.cmdline = function(override)
  return misc.merge(override or {}, {
    ['<Tab>'] = {
      c = function()
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_next_item()
        else
          feedkeys.call(keymap.t('<C-z>'), 'n')
        end
      end,
    },
    ['<S-Tab>'] = {
      c = function()
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_prev_item()
        else
          feedkeys.call(keymap.t('<C-z>'), 'n')
        end
      end,
    },
    ['<C-n>'] = {
      c = function(fallback)
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_next_item()
        else
          fallback()
        end
      end,
    },
    ['<C-p>'] = {
      c = function(fallback)
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_prev_item()
        else
          fallback()
        end
      end,
    },
    ['<C-e>'] = {
      c = mapping.close(),
    },
  })
end

---Invoke completion
---@param option cmp.CompleteParams
mapping.complete = function(option)
  return function(fallback)
    if not require('cmp').complete(option) then
      fallback()
    end
  end
end

---Complete common string.
mapping.complete_common_string = function()
  return function(fallback)
    if not require('cmp').complete_common_string() then
      fallback()
    end
  end
end

---Close current completion menu if it displayed.
mapping.close = function()
  return function(fallback)
    if not require('cmp').close() then
      fallback()
    end
  end
end

---Abort current completion menu if it displayed.
mapping.abort = function()
  return function(fallback)
    if not require('cmp').abort() then
      fallback()
    end
  end
end

---Scroll documentation window.
mapping.scroll_docs = function(delta)
  return function(fallback)
    if not require('cmp').scroll_docs(delta) then
      fallback()
    end
  end
end

---Select next completion item.
mapping.select_next_item = function(option)
  return function(fallback)
    if not require('cmp').select_next_item(option) then
      local release = require('cmp').core:suspend()
      fallback()
      vim.schedule(release)
    end
  end
end

---Select prev completion item.
mapping.select_prev_item = function(option)
  return function(fallback)
    if not require('cmp').select_prev_item(option) then
      local release = require('cmp').core:suspend()
      fallback()
      vim.schedule(release)
    end
  end
end

---Confirm selection
mapping.confirm = function(option)
  return function(fallback)
    if not require('cmp').confirm(option) then
      fallback()
    end
  end
end

return mapping