@@ -2,6 +2,11 @@ local config = require("nvim-surround.config")
22
33local M = {}
44
5+ M .namespace = {
6+ highlight = vim .api .nvim_create_namespace (" nvim-surround-highlight" ),
7+ extmark = vim .api .nvim_create_namespace (" nvim-surround-extmark" ),
8+ }
9+
510--[====================================================================================================================[
611 Cursor helper functions
712--]====================================================================================================================]
@@ -24,11 +29,12 @@ M.set_curpos = function(pos)
2429end
2530
2631-- Move the cursor to a location in the buffer, depending on the `move_cursor` setting.
27- --- @param pos { first_pos : position , old_pos : position } Various positions in the buffer.
32+ --- @param pos { first_pos : position , sticky_pos : position , old_pos : position } Various positions in the buffer.
2833M .restore_curpos = function (pos )
29- -- TODO: Add a `last_pos` field for if `move_cursor` is set to "end"
3034 if config .get_opts ().move_cursor == " begin" then
3135 M .set_curpos (pos .first_pos )
36+ elseif config .get_opts ().move_cursor == " sticky" then
37+ M .set_curpos (pos .sticky_pos )
3238 elseif not config .get_opts ().move_cursor then
3339 M .set_curpos (pos .old_pos )
3440 end
@@ -117,6 +123,29 @@ M.set_operator_marks = function(motion)
117123 M .set_mark (" >" , visual_marks [2 ])
118124end
119125
126+ -- Gets extmark position for the current buffer.
127+ --- @param extmark integer The extmark ID number.
128+ --- @return position @The position of the extmark in the buffer.
129+ --- @nodiscard
130+ M .get_extmark = function (extmark )
131+ local pos = vim .api .nvim_buf_get_extmark_by_id (0 , M .namespace .extmark , extmark , {})
132+ return { pos [1 ] + 1 , pos [2 ] + 1 }
133+ end
134+
135+ -- Creates an extmark for the given position.
136+ --- @param pos position The position in the buffer.
137+ --- @return integer @The extmark ID.
138+ --- @nodiscard
139+ M .set_extmark = function (pos )
140+ return vim .api .nvim_buf_set_extmark (0 , M .namespace .extmark , pos [1 ] - 1 , pos [2 ] - 1 , {})
141+ end
142+
143+ -- Deletes an extmark from the buffer.
144+ --- @param extmark integer The extmark ID number.
145+ M .del_extmark = function (extmark )
146+ vim .api .nvim_buf_del_extmark (0 , M .namespace .extmark , extmark )
147+ end
148+
120149--[====================================================================================================================[
121150 Byte indexing helper functions
122151--]====================================================================================================================]
@@ -257,11 +286,10 @@ M.highlight_selection = function(selection)
257286 if not selection then
258287 return
259288 end
260- local namespace = vim .api .nvim_create_namespace (" NvimSurround" )
261289
262290 vim .highlight .range (
263291 0 ,
264- namespace ,
292+ M . namespace . highlight ,
265293 " NvimSurroundHighlight" ,
266294 { selection .first_pos [1 ] - 1 , selection .first_pos [2 ] - 1 },
267295 { selection .last_pos [1 ] - 1 , selection .last_pos [2 ] - 1 },
273301
274302-- Clears all nvim-surround highlights for the buffer.
275303M .clear_highlights = function ()
276- local namespace = vim .api .nvim_create_namespace (" NvimSurround" )
277- vim .api .nvim_buf_clear_namespace (0 , namespace , 0 , - 1 )
304+ vim .api .nvim_buf_clear_namespace (0 , M .namespace .highlight , 0 , - 1 )
278305 -- Force the screen to clear the highlight immediately
279306 vim .cmd .redraw ()
280307end
0 commit comments