Skip to content

Commit

Permalink
feat(render): support source_id and source_name in menu render (#400
Browse files Browse the repository at this point in the history
)

* feat(render): support `source_id` and `source_name` in menu render

* doc: fix inconsistent readme

* docs: add source_name hl group and draw docs

---------

Co-authored-by: Liam Dyer <liamcdyer@gmail.com>
  • Loading branch information
tim3nd and Saghen authored Nov 28, 2024
1 parent b94172c commit d5f62f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ MiniDeps.add({
text = function(ctx) return ctx.label_description end,
highlight = 'BlinkCmpLabelDescription',
},

source_name = {
width = { max = 30 },
text = function(ctx) return ctx.source_name end,
highlight = 'BlinkCmpSource',
},
},
},
},
Expand Down Expand Up @@ -655,9 +661,10 @@ MiniDeps.add({
| `BlinkCmpLabel` | Pmenu | Label of the completion item |
| `BlinkCmpLabelDeprecated` | Comment | Deprecated label of the completion item |
| `BlinkCmpLabelMatch` | Pmenu | (Currently unused) Label of the completion item when it matches the query |
| `BlinkCmpGhostText` | Comment | Preview item with ghost text |
| `BlinkCmpKind` | Special | Kind icon/text of the completion item |
| `BlinkCmpKind<kind>` | Special | Kind icon/text of the completion item |
| `BlinkCmpSourceName` | NonText | Source name of the completion item |
| `BlinkCmpGhostText` | Comment | Preview item with ghost text |
| `BlinkCmpDoc` | NormalFloat | The documentation window |
| `BlinkCmpDocBorder` | NormalFloat | The documentation window border |
| `BlinkCmpDocCursorLine` | Visual | The documentation window cursor line |
Expand Down Expand Up @@ -796,6 +803,13 @@ completion.menu.draw = {
text = function(ctx) return ctx.label_description end,
highlight = 'BlinkCmpLabelDescription',
},

source_name = {
width = { max = 30 },
-- source_name or source_id are supported
text = function(ctx) return ctx.source_name end,
highlight = 'BlinkCmpSource',
},
},
}
```
Expand Down
6 changes: 6 additions & 0 deletions lua/blink/cmp/completion/windows/render/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
--- @field kind_icon string
--- @field icon_gap string
--- @field deprecated boolean
--- @field source_id string
--- @field source_name string

local context = {}

Expand Down Expand Up @@ -46,6 +48,8 @@ function context.new(draw, item, matched_indices)

local label_detail = (item.labelDetails and item.labelDetails.detail or ''):gsub('\n', newline_char)
local label_description = (item.labelDetails and item.labelDetails.description or ''):gsub('\n', newline_char)
local source_id = item.source_id
local source_name = item.source_name

return {
self = draw,
Expand All @@ -58,6 +62,8 @@ function context.new(draw, item, matched_indices)
kind_icon = kind_icon,
icon_gap = config.nerd_font_variant == 'mono' and '' or ' ',
deprecated = item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)) or false,
source_id = source_id,
source_name = source_name,
}
end

Expand Down
7 changes: 7 additions & 0 deletions lua/blink/cmp/config/completion/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ local window = {
text = function(ctx) return ctx.label_description end,
highlight = 'BlinkCmpLabelDescription',
},

source_name = {
width = { max = 30 },
-- source_name or source_id are supported
text = function(ctx) return ctx.source_name end,
highlight = 'BlinkCmpSource',
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions lua/blink/cmp/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function highlights.setup()
set_hl('BlinkCmpLabelDetail', { link = use_nvim_cmp and 'CmpItemMenu' or 'NonText' })
set_hl('BlinkCmpLabelDescription', { link = use_nvim_cmp and 'CmpItemMenu' or 'NonText' })
set_hl('BlinkCmpKind', { link = use_nvim_cmp and 'CmpItemKind' or 'Special' })
set_hl('BlinkCmpSource', { link = use_nvim_cmp and 'CmpItemMenu' or 'NonText' })
for _, kind in ipairs(require('blink.cmp.types').CompletionItemKind) do
set_hl('BlinkCmpKind' .. kind, { link = use_nvim_cmp and 'CmpItemKind' .. kind or 'BlinkCmpKind' })
end
Expand Down

0 comments on commit d5f62f9

Please sign in to comment.