@@ -36,4 +36,98 @@ function utils.is_special_buffer()
36
36
return buftype ~= ' '
37
37
end
38
38
39
+ function utils .split_lines (text )
40
+ local lines = {}
41
+ for s in text :gmatch (' [^\r\n ]+' ) do
42
+ table.insert (lines , s )
43
+ end
44
+ return lines
45
+ end
46
+
47
+ --- Combines adjacent paragraph lines together
48
+ --- @param lines string[]
49
+ --- @return string[]
50
+ --- TODO: Likely buggy
51
+ function utils .combine_markdown_lines (lines )
52
+ local combined_lines = {}
53
+
54
+ local special_starting_chars = { ' #' , ' >' , ' -' , ' |' }
55
+ local in_code_block = false
56
+ local prev_is_special = false
57
+ for _ , line in ipairs (lines ) do
58
+ if line :match (' ^%s*```' ) then in_code_block = not in_code_block end
59
+
60
+ local is_special = line :match (' ^%s*[' .. table.concat (special_starting_chars ) .. ' ]' ) or line :match (' ^%s*%d\\ .$' )
61
+ local is_empty = line :match (' ^%s*$' )
62
+ local has_linebreak = line :match (' %s%s$' )
63
+
64
+ if # combined_lines == 0 or in_code_block or is_special or prev_is_special or is_empty or has_linebreak then
65
+ table.insert (combined_lines , line )
66
+ elseif line :match (' ^%s*$' ) then
67
+ table.insert (combined_lines , ' ' )
68
+ else
69
+ combined_lines [# combined_lines ] = combined_lines [# combined_lines ] .. ' ' .. line
70
+ end
71
+
72
+ prev_is_special = is_special
73
+ end
74
+
75
+ return combined_lines
76
+ end
77
+
78
+ function utils .highlight_with_treesitter (bufnr , root_lang , start_line , end_line )
79
+ local Range = require (' vim.treesitter._range' )
80
+
81
+ local trees = vim .treesitter .get_parser (bufnr , root_lang )
82
+ trees :parse ({ start_line , end_line })
83
+ if not trees then return end
84
+
85
+ trees :for_each_tree (function (tree , tstree )
86
+ local lang = tstree :lang ()
87
+ local highlighter_query = vim .treesitter .query .get (lang , ' highlights' )
88
+ if not highlighter_query then return end
89
+
90
+ local root_node = tree :root ()
91
+ local _ , _ , root_end_row , _ = root_node :range ()
92
+
93
+ local iter = highlighter_query :iter_captures (tree :root (), bufnr , start_line , end_line )
94
+ local line = start_line
95
+ while line < end_line do
96
+ local capture , node , metadata , _ = iter (line )
97
+ if capture == nil then break end
98
+
99
+ local range = { root_end_row + 1 , 0 , root_end_row + 1 , 0 }
100
+ if node then range = vim .treesitter .get_range (node , bufnr , metadata and metadata [capture ]) end
101
+ local start_row , start_col , end_row , end_col = Range .unpack4 (range )
102
+
103
+ if capture then
104
+ local name = highlighter_query .captures [capture ]
105
+ local hl = 0
106
+ if not vim .startswith (name , ' _' ) then hl = vim .api .nvim_get_hl_id_by_name (' @' .. name .. ' .' .. lang ) end
107
+
108
+ -- The "priority" attribute can be set at the pattern level or on a particular capture
109
+ local priority = (
110
+ tonumber (metadata .priority or metadata [capture ] and metadata [capture ].priority )
111
+ or vim .highlight .priorities .treesitter
112
+ )
113
+
114
+ -- The "conceal" attribute can be set at the pattern level or on a particular capture
115
+ local conceal = metadata .conceal or metadata [capture ] and metadata [capture ].conceal
116
+
117
+ if hl and end_row >= line then
118
+ vim .api .nvim_buf_set_extmark (bufnr , require (' blink.cmp.config' ).highlight .ns , start_row , start_col , {
119
+ end_line = end_row ,
120
+ end_col = end_col ,
121
+ hl_group = hl ,
122
+ priority = priority ,
123
+ conceal = conceal ,
124
+ })
125
+ end
126
+ end
127
+
128
+ if start_row > line then line = start_row end
129
+ end
130
+ end )
131
+ end
132
+
39
133
return utils
0 commit comments