Skip to content

Commit

Permalink
feat(ghost_text): show_on_unselected (#965)
Browse files Browse the repository at this point in the history
* feat(ghost_text): show_on_unselected

* feat: support `show_with(out)_selection`

---------

Co-authored-by: Liam Dyer <[email protected]>
  • Loading branch information
xzbdmw and Saghen authored Jan 9, 2025
1 parent 80945db commit 4a380c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ completion.documentation = {
-- Displays a preview of the selected item on the current line
completion.ghost_text = {
enabled = false,
-- Show the ghost text when an item has been selected
show_with_selection = true,
-- Show the ghost text when no item has been selected, defaulting to the first item
show_without_selection = false,
},
```

Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function completion.setup()
-- setup ghost text
if config.completion.ghost_text.enabled then
list.select_emitter:on(
function(event) require('blink.cmp.completion.windows.ghost_text').show_preview(event.item) end
function(event) require('blink.cmp.completion.windows.ghost_text').show_preview(event.items, event.idx) end
)
list.hide_emitter:on(function() require('blink.cmp.completion.windows.ghost_text').clear_preview() end)
end
Expand Down
17 changes: 14 additions & 3 deletions lua/blink/cmp/completion/windows/ghost_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local snippets_utils = require('blink.cmp.sources.snippets.utils')
--- @field extmark_id integer?
---
--- @field is_open fun(): boolean
--- @field show_preview fun(item: blink.cmp.CompletionItem)
--- @field show_preview fun(items: blink.cmp.CompletionItem[], idx: number)
--- @field clear_preview fun()
--- @field draw_preview fun(bufnr: number)

Expand All @@ -36,8 +36,19 @@ vim.api.nvim_create_autocmd({ 'CursorMovedI', 'TextChangedI' }, {

function ghost_text.is_open() return ghost_text.extmark_id ~= nil end

--- @param selected_item? blink.cmp.CompletionItem
function ghost_text.show_preview(selected_item)
function ghost_text.show_preview(items, selection_idx)
-- check if we're supposed to show
if not config.show_with_selection and selection_idx ~= nil then
ghost_text.clear_preview()
return
end
if not config.show_without_selection and selection_idx == nil then
ghost_text.clear_preview()
return
end

local selected_item = items[selection_idx or 1]

-- nothing to show, clear the preview
if not selected_item then
ghost_text.clear_preview()
Expand Down
6 changes: 6 additions & 0 deletions lua/blink/cmp/config/completion/ghost_text.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
--- Displays a preview of the selected item on the current line
--- @class (exact) blink.cmp.CompletionGhostTextConfig
--- @field enabled boolean
--- @field show_with_selection boolean Show the ghost text when an item has been selected
--- @field show_without_selection boolean Show the ghost text when no item has been selected, defaulting to the first item

local validate = require('blink.cmp.config.utils').validate
local ghost_text = {
--- @type blink.cmp.CompletionGhostTextConfig
default = {
enabled = false,
show_with_selection = true,
show_without_selection = false,
},
}

function ghost_text.validate(config)
validate('completion.ghost_text', {
enabled = { config.enabled, 'boolean' },
show_with_selection = { config.show_with_selection, 'boolean' },
show_without_selection = { config.show_without_selection, 'boolean' },
}, config)
end

Expand Down

0 comments on commit 4a380c1

Please sign in to comment.