Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 15d1c75

Browse files
committed
feat: support lua per server setup
add todo fix some stuff fix fix
1 parent a7a538c commit 15d1c75

File tree

10 files changed

+89
-58
lines changed

10 files changed

+89
-58
lines changed

lua/completion.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local match = require'completion.matching'
44
local source = require 'completion.source'
55
local signature = require'completion.signature_help'
66
local hover = require'completion.hover'
7+
local opt = require'completion.option'
78
local M = {}
89

910
------------------------------------------------------------------------
@@ -12,7 +13,6 @@ local M = {}
1213

1314
M.completionConfirm = false
1415

15-
1616
-- Manager variable to keep all state accross completion
1717
local manager = {
1818
-- Handle insertCharPre event, turn off imediately when preforming completion
@@ -75,7 +75,7 @@ function M.confirmCompletion()
7575
end
7676
end
7777

78-
if vim.g.completion_enable_auto_paren == 1 then
78+
if opt.get_option('enable_auto_paren') == 1 then
7979
M.autoAddParens(complete_item)
8080
end
8181
if complete_item.kind == 'UltiSnips' then
@@ -97,7 +97,7 @@ function M.on_InsertCharPre()
9797
manager.insertChar = true
9898
manager.textHover = true
9999
manager.selected = -1
100-
if vim.g.completion_auto_change_source == 1 then
100+
if opt.get_option('auto_change_source') == 1 then
101101
manager.autoChange = true
102102
end
103103
end
@@ -117,28 +117,28 @@ function M.on_InsertEnter()
117117
manager.insertLeave = false
118118
manager.insertChar = false
119119
manager.changeSource = false
120-
if vim.g.completion_auto_change_source == 1 then
120+
if opt.get_option('auto_change_source') == 1 then
121121
manager.autoChange = true
122122
end
123123

124124
-- reset source
125125
source.chain_complete_index = 1
126126
source.stop_complete = false
127127
local l_complete_index = source.chain_complete_index
128-
local timer_cycle = vim.g.completion_timer_cycle
128+
local timer_cycle = opt.get_option('timer_cycle')
129129

130130
timer:start(100, timer_cycle, vim.schedule_wrap(function()
131131
local l_changedTick = api.nvim_buf_get_changedtick(0)
132132
-- complete if changes are made
133133
if l_changedTick ~= manager.changedTick then
134134
manager.changedTick = l_changedTick
135-
if vim.g.completion_enable_auto_popup == 1 then
135+
if opt.get_option('enable_auto_popup') == 1 then
136136
source.autoCompletion(manager)
137137
end
138-
if vim.g.completion_enable_auto_hover == 1 then
138+
if opt.get_option('enable_auto_hover') == 1 then
139139
hover.autoOpenHoverInPopup(manager)
140140
end
141-
if vim.g.completion_enable_auto_signature == 1 then
141+
if opt.get_option('enable_auto_signature') == 1 then
142142
signature.autoOpenSignatureHelp()
143143
end
144144
end
@@ -207,16 +207,24 @@ M.prevSource = function()
207207
source.prevCompletion()
208208
end
209209

210-
M.on_attach = function(opt)
210+
M.on_attach = function(option)
211+
-- setup completion_option tables
212+
if option ~= nil then
213+
opt.set_option_table(option)
214+
else
215+
opt.set_option_table(option)
216+
end
217+
-- setup autocommand
218+
-- TODO: Modified this if lua callbacks for autocmd is merged
211219
api.nvim_command("augroup CompletionCommand")
212220
api.nvim_command("autocmd! * <buffer>")
213221
api.nvim_command("autocmd InsertEnter <buffer> lua require'completion'.on_InsertEnter()")
214222
api.nvim_command("autocmd InsertLeave <buffer> lua require'completion'.on_InsertLeave()")
215223
api.nvim_command("autocmd InsertCharPre <buffer> lua require'completion'.on_InsertCharPre()")
216224
api.nvim_command("autocmd CompleteDone <buffer> lua require'completion'.confirmCompletion()")
217225
api.nvim_command("augroup end")
218-
if string.len(vim.g.completion_confirm_key) ~= 0 then
219-
api.nvim_buf_set_keymap(0, 'i', vim.g.completion_confirm_key,
226+
if string.len(opt.get_option('confirm_key')) ~= 0 then
227+
api.nvim_buf_set_keymap(0, 'i', opt.get_option('confirm_key'),
220228
'pumvisible() ? complete_info()["selected"] != "-1" ? "\\<Plug>(completion_confirm_completion)" :'..
221229
' "\\<c-e>\\<CR>" : "\\<CR>"',
222230
{silent=false, noremap=false, expr=true})
@@ -228,17 +236,6 @@ M.on_attach = function(opt)
228236
api.nvim_command("augroup end")
229237
end
230238
api.nvim_buf_set_var(0, 'completion_enable', 1)
231-
if opt == nil then return end
232-
local sorter = opt.sorter
233-
local matcher = opt.matcher
234-
if sorter ~= nil then
235-
vim.validate{sorter={sorter, 'string'}}
236-
vim.b.completion_sorting = sorter
237-
end
238-
if matcher ~= nil then
239-
vim.validate{matcher={matcher, 'table'}}
240-
vim.b.completion_matching_strategy_list = matcher
241-
end
242239
end
243240

244241
return M

lua/completion/chain_completion.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local vim = vim
22
local api = vim.api
33
local util = require 'completion.util'
4+
local opt = require 'completion.option'
45
local M ={}
56

67
--------------------------------------------------------------
@@ -68,7 +69,7 @@ end
6869
-- preserve compatiblity of completion_chain_complete_list
6970
function M.getChainCompleteList(filetype)
7071

71-
local chain_complete_list = chain_list_to_tree(vim.g.completion_chain_complete_list)
72+
local chain_complete_list = chain_list_to_tree(opt.get_option('chain_complete_list'))
7273
-- check if chain_complete_list is a array
7374

7475
if chain_complete_list[filetype] then

lua/completion/complete.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local util = require 'completion.util'
44
local ins = require 'completion.source.ins_complete'
55
local match = require'completion.matching'
66
local lsp = require'completion.source.lsp'
7+
local opt = require 'completion.option'
78

89
local M = {}
910

@@ -33,7 +34,7 @@ M.clearCache = function()
3334
end
3435

3536
-- perform completion
36-
M.performComplete = function(complete_source, complete_items_map, manager, opt)
37+
M.performComplete = function(complete_source, complete_items_map, manager, params)
3738

3839
manager.insertChar = false
3940
if vim.fn.has_key(complete_source, "mode") > 0 then
@@ -56,7 +57,7 @@ M.performComplete = function(complete_source, complete_items_map, manager, opt)
5657
table.insert(callback_array, true)
5758
else
5859
table.insert(callback_array, complete_items.callback)
59-
complete_items.trigger(manager, opt)
60+
complete_items.trigger(manager, params)
6061
end
6162
table.insert(items_array, complete_items.item)
6263
end
@@ -73,14 +74,14 @@ M.performComplete = function(complete_source, complete_items_map, manager, opt)
7374
-- only perform complete when callback_array are all true
7475
if checkCallback(callback_array) == true and timer:is_closing() == false then
7576
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
76-
local items = getCompletionItems(items_array, opt.prefix)
77-
if vim.g.completion_sorting ~= "none" then
77+
local items = getCompletionItems(items_array, params.prefix)
78+
if opt.get_option('sorting') ~= "none" then
7879
util.sort_completion_items(items)
7980
end
8081
if #items ~= 0 then
8182
-- reset insertChar and handle auto changing source
8283
cache_complete_items = items
83-
vim.fn.complete(opt.textMatch+1, items)
84+
vim.fn.complete(params.textMatch+1, items)
8485
manager.changeSource = false
8586
else
8687
manager.changeSource = true
@@ -94,15 +95,15 @@ M.performComplete = function(complete_source, complete_items_map, manager, opt)
9495
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
9596
local items = {}
9697
for _, item in ipairs(cache_complete_items) do
97-
match.matching(items, opt.prefix, item)
98+
match.matching(items, params.prefix, item)
9899
end
99-
if vim.g.completion_sorting ~= "none" then
100+
if opt.get_option('sorting') ~= "none" then
100101
util.sort_completion_items(items)
101102
end
102103
if #items ~= 0 then
103104
-- reset insertChar and handle auto changing source
104105
cache_complete_items = items
105-
vim.fn.complete(opt.textMatch+1, items)
106+
vim.fn.complete(params.textMatch+1, items)
106107
manager.changeSource = false
107108
else
108109
cache_complete_items = {}

lua/completion/health.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local vim = vim
22
local api = vim.api
33
local source = require 'completion.source'
4+
local opt = require 'completion.option'
45

56
local health_start = vim.fn["health#report_start"]
67
local health_ok = vim.fn['health#report_ok']
@@ -14,7 +15,7 @@ local checkCompletionSource = function()
1415
end
1516

1617
local checkSnippetSource = function()
17-
local snippet_source = vim.g.completion_enable_snippet
18+
local snippet_source = opt.get_option('enable_snippet')
1819
if snippet_source == nil then
1920
health_info("You haven't setup any snippet source.")
2021
elseif snippet_source == 'UltiSnips' then

lua/completion/hover.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
local vim = vim
33
local validate = vim.validate
44
local api = vim.api
5+
local opt = require 'completion.option'
56

67
local M = {}
78

@@ -190,11 +191,11 @@ local fancy_floating_markdown = function(contents, opts)
190191
local height = #stripped
191192
local bufnr = api.nvim_create_buf(false, true)
192193
local winnr
193-
if vim.g.completion_docked_hover == 1 then
194-
if height > vim.g.completion_docked_maximum_size then
195-
height = vim.g.completion_docked_maximum_size
196-
elseif height < vim.g.completion_docked_minimum_size then
197-
height = vim.g.completion_docked_minimum_size
194+
if opt.get_option('docked_hover') == 1 then
195+
if height > opt.get_option('docked_maximum_size') then
196+
height = opt.get_option('docked_maximum_size')
197+
elseif height < opt.get_option('docked_minimum_size') then
198+
height = opt.get_option('docked_minimum_size')
198199
end
199200
local row
200201
if vim.fn.winline() > api.nvim_get_option('lines')/2 then

lua/completion/matching.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
local vim = vim
2-
local util = require'completion.util'
2+
local util = require 'completion.util'
3+
local opt = require 'completion.option'
34
local M = {}
45

56
local function fuzzy_match(prefix, word)
6-
if vim.g.completion_matching_ignore_case == 1 then
7+
if opt.get_option('matching_ignore_case') == 1 then
78
prefix = string.lower(prefix)
89
word = string.lower(word)
910
end
@@ -17,7 +18,7 @@ end
1718

1819

1920
local function substring_match(prefix, word)
20-
if vim.g.completion_matching_ignore_case == 1 then
21+
if opt.get_option('matching_ignore_case') == 1 then
2122
prefix = string.lower(prefix)
2223
word = string.lower(word)
2324
end
@@ -29,7 +30,7 @@ local function substring_match(prefix, word)
2930
end
3031

3132
local function exact_match(prefix, word)
32-
if vim.g.completion_matching_ignore_case == 1 then
33+
if opt.get_option('matching_ignore_case') == 1 then
3334
prefix = string.lower(prefix)
3435
word = string.lower(word)
3536
end
@@ -47,7 +48,7 @@ local matching_strategy = {
4748
}
4849

4950
M.matching = function(complete_items, prefix, item)
50-
local matcher_list = vim.b.completion_matching_strategy_list or vim.g.completion_matching_strategy_list
51+
local matcher_list = opt.get_option('matching_strategy_list')
5152
local matching_priority = 2
5253
for _, method in ipairs(matcher_list) do
5354
local is_match, score = matching_strategy[method](prefix, item.word)

lua/completion/option.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local M = {}
2+
3+
4+
-- fallback to using global variable as default
5+
local completion_opt_metatable = {
6+
__index = function(_, key)
7+
key = 'completion_'..key
8+
return vim.g[key]
9+
end
10+
}
11+
12+
local option_table = setmetatable({}, completion_opt_metatable)
13+
14+
M.set_option_table = function(opt)
15+
if opt ~= nil then
16+
option_table = setmetatable(opt, completion_opt_metatable)
17+
else
18+
option_table = setmetatable({}, completion_opt_metatable)
19+
end
20+
end
21+
22+
M.get_option = function(opt)
23+
return option_table[opt]
24+
end
25+
26+
return M

lua/completion/source.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local chain_completion = require 'completion.chain_completion'
66
local lsp = require 'completion.source.lsp'
77
local snippet = require 'completion.source.snippet'
88
local path = require 'completion.source.path'
9+
local opt = require 'completion.option'
910

1011
local M = {}
1112

@@ -74,7 +75,7 @@ local triggerCurrentCompletion = function(manager, bufnr, line_to_cursor, prefix
7475
local source_trigger_character = getTriggerCharacter(complete_source)
7576
local triggered
7677
triggered = util.checkTriggerCharacter(line_to_cursor, source_trigger_character) or
77-
util.checkTriggerCharacter(line_to_cursor, vim.g.completion_trigger_character)
78+
util.checkTriggerCharacter(line_to_cursor, opt.get_option('trigger_character'))
7879

7980
if complete_source.complete_items ~= nil then
8081
for _, source in ipairs(complete_source.complete_items) do
@@ -102,7 +103,7 @@ local triggerCurrentCompletion = function(manager, bufnr, line_to_cursor, prefix
102103
end
103104
end
104105

105-
local length = vim.g.completion_trigger_keyword_length
106+
local length = opt.get_option('trigger_keyword_length')
106107
if #prefix < length and not triggered and not force then
107108
return
108109
end
@@ -150,15 +151,15 @@ function M.autoCompletion(manager)
150151
local prefix = line_to_cursor:sub(textMatch+1)
151152
local rev_textMatch = #cursor_to_end - vim.fn.match(cursor_to_end:reverse(), '\\k*$')
152153
local suffix = cursor_to_end:sub(1, rev_textMatch)
153-
local length = vim.g.completion_trigger_keyword_length
154+
local length = opt.get_option('trigger_keyword_length')
154155

155156
-- reset completion when deleting character in insert mode
156157
if #prefix < M.prefixLength and vim.fn.pumvisible() == 0 then
157158
M.chain_complete_index = 1
158159
-- not sure if I should clear cache here
159160
complete.clearCache()
160161
-- api.nvim_input("<c-g><c-g>")
161-
if vim.g.completion_trigger_on_delete == 1 then
162+
if opt.get_option('trigger_on_delete') == 1 then
162163
M.triggerCompletion(false, manager)
163164
end
164165
M.stop_complete = false

0 commit comments

Comments
 (0)