Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0a5dd39

Browse files
committedMay 16, 2021
Add custom formatting functions.
The custom formatters allow users to intercept the data in different places, providing the option for them to re-format text or change how/what data is getting displayed. (fixes #209)
1 parent efe3a66 commit 0a5dd39

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
 

‎README.md

+33
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,39 @@ vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
326326

327327
Use `compe#confirm()` mapping, as described in section [Mappings](#mappings).
328328

329+
## Advanced configuration
330+
331+
If you want to customize how compe displays completion options, compe allows you
332+
to run custom formatting code before the list get's displayed.
333+
For this, use the `formatting_functions` configuration option.
334+
Here you can functions for each provider to change the displayed data along the way.
335+
Currently, there are two formatters you can set:
336+
337+
- `results`: Change the list of completion results.
338+
- `documentation`: Change the documentation text.
339+
340+
For example, you could use a results formatter to show the type information
341+
and other details in the completion list.
342+
(Note that the `details` feature used here is not supported by many language servers.)
343+
344+
```lua
345+
formatting_functions = {
346+
nvim_lsp = {
347+
results = function(items)
348+
local max_col_width = 0
349+
for _, item in ipairs(items) do
350+
max_col_width = math.max(max_col_width, vim.fn.strwidth(item.abbr))
351+
end
352+
for _, item in ipairs(items) do
353+
local padding = string.rep(" ", max_col_width - vim.fn.strwidth(item.abbr) + 1)
354+
item.abbr = item.abbr .. padding .. item.user_data.compe.completion_item.detail
355+
end
356+
return items
357+
end
358+
}
359+
}
360+
```
361+
329362
## Demo
330363

331364
### Auto Import

‎doc/compe.txt

+34
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,40 @@ source ~
302302
For dictionary, see |compe-source| for the list of source options.
303303

304304

305+
formatting_functions ~
306+
Custom output formatting functions. Optional.
307+
Type: |Dictionary| of:
308+
Key: |String|
309+
Source name.
310+
For the list of builtin sources, see |compe-sources|.
311+
Value: |Dictionary| of:
312+
results ~
313+
This function get's called with the list of completion results
314+
before displaying the list.
315+
It takes and returns the list of completion results,
316+
and may transform it.
317+
Type: |Function|
318+
Takes 1 argument:
319+
completion_items:
320+
Type: |Table| of completion items.
321+
Returns:
322+
The transformed list.
323+
Type: |Table| of completion items.
324+
325+
documentation ~
326+
This function get's called with the documentation of the
327+
currently selected completion item, and may return the text
328+
changed in any way.
329+
Type: |Function|
330+
Takes 1 argument:
331+
document:
332+
Type: |Table| of lines of the documentation text.
333+
Returns:
334+
The transformed documentation.
335+
Type: |Table| of lines of the documentation text.
336+
337+
338+
305339
------------------------------------------------------------------------------
306340
HIGHLIGHT *compe-highlight*
307341

‎lua/compe/config.lua

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Config.get = function()
3232
return Config._bufnrs[vim.api.nvim_get_current_buf()] or Config._config
3333
end
3434

35+
-- get_formatter
36+
Config.get_formatter = function(source_name, subject)
37+
local source_functions = Config.get().formatting_functions[source_name]
38+
return source_functions and source_functions[subject]
39+
end
40+
3541
--- get_metadata
3642
Config.get_metadata = function(source_name)
3743
return Config.get().source[source_name]
@@ -60,6 +66,7 @@ Config._normalize = function(config)
6066
config.max_menu_width = config.max_menu_width or 100
6167
config.autocomplete = Boolean.get(config.autocomplete, true)
6268
config.documentation = Boolean.get(config.documentation, true)
69+
config.formatting_functions = config.formatting_functions or {}
6370

6471
-- normalize source metadata
6572
if config.source then

‎lua/compe/source.lua

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ Source.trigger = function(self, context, callback)
171171
self.trigger_character_offset = state.trigger_character_offset
172172
self.items = self:_normalize_items(context, result.items or {})
173173

174+
local formatter = Config.get_formatter(self.name, "results")
175+
if formatter then
176+
self.items = formatter(self.items)
177+
end
178+
174179
if #self.items == 0 then
175180
self:clear()
176181
end
@@ -225,6 +230,11 @@ Source.documentation = function(self, completed_item)
225230
context = Context.new({}, {});
226231
callback = Async.guard('Source.documentation#callback', vim.schedule_wrap(function(document)
227232
if document and #document ~= 0 then
233+
234+
local formatter = Config.get_formatter(self.name, "documentation")
235+
if formatter then
236+
document = formatter(document)
237+
end
228238
vim.call('compe#documentation#open', document)
229239
else
230240
vim.call('compe#documentation#close')

0 commit comments

Comments
 (0)
This repository has been archived.