Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ghost_text): ensure multiline indent is correct #934

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/blink/cmp/completion/windows/ghost_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function ghost_text.draw_preview(bufnr)

if ghost_text.selected_item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
local expanded_snippet = snippets_utils.safe_parse(text_edit.newText)
text_edit.newText = expanded_snippet and tostring(expanded_snippet) or text_edit.newText
text_edit.newText = expanded_snippet and snippets_utils.add_current_line_indentation(tostring(expanded_snippet))
or text_edit.newText
end

local display_lines = vim.split(get_still_untyped_text(text_edit), '\n', { plain = true }) or {}
Expand Down
25 changes: 25 additions & 0 deletions lua/blink/cmp/sources/snippets/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ function utils.read_snippet(snippet, fallback)
return snippets
end

-- Add the current line's identation to all but the first line of
-- the provided text
---@param text string
function utils.add_current_line_indentation(text)
local base_indent = vim.api.nvim_get_current_line():match('^%s*') or ''
local snippet_lines = vim.split(text, '\n', { plain = true })

local shiftwidth = vim.fn.shiftwidth()
local curbuf = vim.api.nvim_get_current_buf()
local expandtab = vim.bo[curbuf].expandtab

local lines = {} --- @type string[]
for i, line in ipairs(snippet_lines) do
-- Replace tabs with spaces
if expandtab then
line = line:gsub('\t', (' '):rep(shiftwidth)) --- @type string
end
-- Add the base indentation
if i > 1 then line = base_indent .. line end
lines[#lines + 1] = line
end

return table.concat(lines, '\n')
end

function utils.get_tab_stops(snippet)
local expanded_snippet = require('blink.cmp.sources.snippets.utils').safe_parse(snippet)
if not expanded_snippet then return end
Expand Down
Loading