Skip to content

Commit d7b646f

Browse files
committedJan 10, 2025
feat(completions): insert space after marker
## Details Issue: #292 Updates the logic of completion text to check if the marker node (for both list items and block quotes) ends in a space. If it does not then include a space in the completions. Also adds a call to treesitter parser `parse` method for current row to remove various edge cases with treesitter not being up to date after text is inserted depending on when completion plugin runs each completion source. Should not have a noticeable performance impact since we limit range to current row.
1 parent cfe5746 commit d7b646f

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
 

Diff for: ‎doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 January 09
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 January 10
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

Diff for: ‎lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '7.8.5'
8+
M.version = '7.8.6'
99

1010
function M.check()
1111
M.start('version')

Diff for: ‎lua/render-markdown/integ/source.lua

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local Str = require('render-markdown.lib.str')
12
local manager = require('render-markdown.manager')
23
local state = require('render-markdown.state')
34
local util = require('render-markdown.core.util')
@@ -18,14 +19,21 @@ end
1819

1920
---@return string[]
2021
function M.trigger_characters()
21-
return { '-', '*', '+', '>', ' ', '[' }
22+
return { '-', '*', '+', '>', ' ' }
2223
end
2324

2425
---@param buf integer
2526
---@param row integer
2627
---@param col integer
2728
---@return lsp.CompletionItem[]?
2829
function M.items(buf, row, col)
30+
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
31+
if not has_parser then
32+
return nil
33+
end
34+
35+
-- Parse current row to get up to date node
36+
parser:parse({ row, row })
2937
local node = vim.treesitter.get_node({ bufnr = buf, pos = { row, col } })
3038
if node == nil then
3139
return nil
@@ -44,13 +52,14 @@ function M.items(buf, row, col)
4452
if node:type() == 'block_quote' then
4553
local quote_row = node:range()
4654
if quote_row == row then
55+
local prefix = M.space_prefix(buf, node)
4756
for _, component in pairs(config.callout) do
48-
table.insert(items, M.item(component.raw, component.rendered, nil))
57+
table.insert(items, M.item(prefix .. component.raw, component.rendered, nil))
4958
end
5059
end
5160
elseif node:type() == 'list_item' then
5261
local checkbox = config.checkbox
53-
local prefix = M.list_prefix(row, node)
62+
local prefix = M.list_prefix(buf, row, node)
5463
table.insert(items, M.item(prefix .. '[ ] ', checkbox.unchecked.icon, 'unchecked'))
5564
table.insert(items, M.item(prefix .. '[x] ', checkbox.checked.icon, 'checked'))
5665
for name, component in pairs(checkbox.custom) do
@@ -61,22 +70,32 @@ function M.items(buf, row, col)
6170
end
6271

6372
---@private
73+
---@param buf integer
6474
---@param row integer
6575
---@param node TSNode
6676
---@return string
67-
function M.list_prefix(row, node)
77+
function M.list_prefix(buf, row, node)
6878
local marker_node = node:named_child(0)
6979
if marker_node == nil then
7080
return ''
7181
end
7282
local marker_row = marker_node:range()
7383
if marker_row == row then
74-
return ''
84+
return M.space_prefix(buf, marker_node)
7585
end
7686
local marker = list_markers[marker_node:type()]
7787
return marker ~= nil and marker .. ' ' or ''
7888
end
7989

90+
---@private
91+
---@param buf integer
92+
---@param node TSNode
93+
---@return string
94+
function M.space_prefix(buf, node)
95+
local text = vim.treesitter.get_node_text(node, buf)
96+
return Str.spaces('end', text) == 0 and ' ' or ''
97+
end
98+
8099
---@private
81100
---@param raw string
82101
---@param rendered string

0 commit comments

Comments
 (0)
Please sign in to comment.