@@ -101,17 +101,16 @@ local colorize_ls_long = function(bufnr, data, sections)
101101 local section = sections [lnum ]
102102 for i = 1 , section [1 ].end_index - 1 do -- Highlight permissions
103103 local c = line :sub (i , i )
104- vim . api . nvim_buf_add_highlight (bufnr , ns_previewer , color_hash [c ], lnum - 1 , i - 1 , i )
104+ utils . highlight (bufnr , ns_previewer , color_hash [c ], { lnum - 1 , i - 1 }, { lnum - 1 , i } )
105105 end
106106 for i = 2 , # section do -- highlights size, (user, group), date and name
107107 local hl_group = color_hash [i + (i ~= 2 and windows_add or 0 )]
108- vim . api . nvim_buf_add_highlight (
108+ utils . highlight (
109109 bufnr ,
110110 ns_previewer ,
111111 type (hl_group ) == " function" and hl_group (line ) or hl_group ,
112- lnum - 1 ,
113- section [i ].start_index - 1 ,
114- section [i ].end_index - 1
112+ { lnum - 1 , section [i ].start_index - 1 },
113+ { lnum - 1 , section [i ].end_index - 1 }
115114 )
116115 end
117116 end
@@ -133,7 +132,7 @@ local handle_directory_preview = function(filepath, bufnr, opts)
133132 vim .api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , paths )
134133 for i , path in ipairs (paths ) do
135134 local hl = color_hash [6 ](data [i ])
136- vim . api . nvim_buf_add_highlight (bufnr , ns_previewer , hl , i - 1 , 0 , # path )
135+ utils . highlight (bufnr , ns_previewer , hl , { i - 1 , 0 }, { i - 1 , # path } )
137136 end
138137 end
139138 else
@@ -531,13 +530,12 @@ previewers.vimgrep = defaulter(function(opts)
531530
532531 for i = lnum , lnend do
533532 pcall (
534- vim . api . nvim_buf_add_highlight ,
533+ utils . highlight ,
535534 bufnr ,
536535 ns_previewer ,
537536 " TelescopePreviewLine" ,
538- i ,
539- i == lnum and col or 0 ,
540- i == lnend and colend or - 1
537+ { i , i == lnum and col or 0 },
538+ { i , i == lnend and colend or - 1 }
541539 )
542540 end
543541
@@ -623,7 +621,14 @@ previewers.ctags = defaulter(function(opts)
623621 if self .state .last_set_bufnr then
624622 pcall (vim .api .nvim_buf_clear_namespace , self .state .last_set_bufnr , ns_previewer , 0 , - 1 )
625623 end
626- pcall (vim .api .nvim_buf_add_highlight , bufnr , ns_previewer , " TelescopePreviewMatch" , entry .lnum - 1 , 0 , - 1 )
624+ pcall (
625+ utils .highlight ,
626+ bufnr ,
627+ ns_previewer ,
628+ " TelescopePreviewMatch" ,
629+ { entry .lnum - 1 , 0 },
630+ { entry .lnum - 1 , - 1 }
631+ )
627632 pcall (vim .api .nvim_win_set_cursor , self .state .winid , { entry .lnum , 0 })
628633 self .state .last_set_bufnr = bufnr
629634 end
@@ -751,13 +756,12 @@ previewers.git_branch_log = defaulter(function(opts)
751756 if hstart then
752757 if hend < # line then
753758 pcall (
754- vim . api . nvim_buf_add_highlight ,
759+ utils . highlight ,
755760 bufnr ,
756761 ns_previewer ,
757762 " TelescopeResultsIdentifier" ,
758- i - 1 ,
759- hstart - 1 ,
760- hend
763+ { i - 1 , hstart - 1 },
764+ { i - 1 , hend }
761765 )
762766 end
763767 end
@@ -766,27 +770,18 @@ previewers.git_branch_log = defaulter(function(opts)
766770 local cend = string.find (line , " %) " )
767771 if cend then
768772 pcall (
769- vim . api . nvim_buf_add_highlight ,
773+ utils . highlight ,
770774 bufnr ,
771775 ns_previewer ,
772776 " TelescopeResultsConstant" ,
773- i - 1 ,
774- cstart - 1 ,
775- cend
777+ { i - 1 , cstart - 1 },
778+ { i - 1 , cend }
776779 )
777780 end
778781 end
779782 local dstart , _ = line :find " %(%d"
780783 if dstart then
781- pcall (
782- vim .api .nvim_buf_add_highlight ,
783- bufnr ,
784- ns_previewer ,
785- " TelescopeResultsSpecialComment" ,
786- i - 1 ,
787- dstart ,
788- # line
789- )
784+ pcall (utils .highlight , bufnr , ns_previewer , " TelescopeResultsSpecialComment" , { i - 1 , dstart }, { # line })
790785 end
791786 end
792787 end
@@ -965,7 +960,7 @@ previewers.git_commit_message = defaulter(function(opts)
965960 for k , v in ipairs (hl_map ) do
966961 local _ , s = content [k ]:find " %s"
967962 if s then
968- vim . api . nvim_buf_add_highlight (bufnr , ns_previewer , v , k - 1 , s , # content [k ])
963+ utils . highlight (bufnr , ns_previewer , v , { k - 1 , s }, { k - 1 , # content [k ] } )
969964 end
970965 end
971966 end ,
@@ -1054,7 +1049,7 @@ previewers.autocommands = defaulter(function(_)
10541049
10551050 vim .api .nvim_buf_set_option (self .state .bufnr , " filetype" , " vim" )
10561051 vim .api .nvim_buf_set_lines (self .state .bufnr , 0 , - 1 , false , display )
1057- vim . api . nvim_buf_add_highlight (self .state .bufnr , 0 , " TelescopeBorder" , 1 , 0 , - 1 )
1052+ utils . highlight (self .state .bufnr , 0 , " TelescopeBorder" , { 1 , 0 }, { 1 , - 1 } )
10581053 else
10591054 for idx , item in ipairs (results ) do
10601055 if item == entry then
@@ -1064,7 +1059,13 @@ previewers.autocommands = defaulter(function(_)
10641059 end
10651060 end
10661061
1067- vim .api .nvim_buf_add_highlight (self .state .bufnr , ns_previewer , " TelescopePreviewLine" , selected_row + 1 , 0 , - 1 )
1062+ utils .highlight (
1063+ self .state .bufnr ,
1064+ ns_previewer ,
1065+ " TelescopePreviewLine" ,
1066+ { selected_row + 1 , 0 },
1067+ { selected_row + 1 , - 1 }
1068+ )
10681069 -- set the cursor position after self.state.bufnr is connected to the
10691070 -- preview window (which is scheduled in new_buffer_previewer)
10701071 vim .schedule (function ()
@@ -1109,7 +1110,7 @@ previewers.highlights = defaulter(function(_)
11091110 local startPos = string.find (v , " xxx" , 1 , true ) - 1
11101111 local endPos = startPos + 3
11111112 local hlgroup = string.match (v , " ([^ ]*)%s+.*" )
1112- pcall (vim . api . nvim_buf_add_highlight , self .state .bufnr , 0 , hlgroup , k - 1 , startPos , endPos )
1113+ pcall (utils . highlight , self .state .bufnr , 0 , hlgroup , { k - 1 , startPos }, { k - 1 , endPos } )
11131114 end
11141115 end
11151116
@@ -1120,13 +1121,12 @@ previewers.highlights = defaulter(function(_)
11201121 local lnum = vim .api .nvim_win_get_cursor (self .state .winid )[1 ]
11211122 -- That one is actually a match but its better to use it like that then matchadd
11221123 pcall (vim .api .nvim_buf_clear_namespace , self .state .bufnr , ns_previewer , 0 , - 1 )
1123- vim . api . nvim_buf_add_highlight (
1124+ utils . highlight (
11241125 self .state .bufnr ,
11251126 ns_previewer ,
11261127 " TelescopePreviewMatch" ,
1127- lnum - 1 ,
1128- 0 ,
1129- # entry .value
1128+ { lnum - 1 , 0 },
1129+ { lnum - 1 , # entry .value }
11301130 )
11311131 -- we need to zz after the highlighting otherwise highlighting doesnt work
11321132 vim .cmd " norm! zz"
@@ -1194,24 +1194,22 @@ previewers.pickers = defaulter(function(_)
11941194
11951195 if display_highlight ~= nil then
11961196 for _ , hl_block in ipairs (display_highlight ) do
1197- vim . api . nvim_buf_add_highlight (
1197+ utils . highlight (
11981198 self .state .bufnr ,
11991199 ns_telescope_entry ,
12001200 hl_block [2 ],
1201- row ,
1202- hl_block [1 ][1 ],
1203- hl_block [1 ][2 ]
1201+ { row , hl_block [1 ][1 ] },
1202+ { row , hl_block [1 ][2 ] }
12041203 )
12051204 end
12061205 end
12071206 if picker ._multi :is_selected (e ) then
1208- vim . api . nvim_buf_add_highlight (
1207+ utils . highlight (
12091208 self .state .bufnr ,
12101209 ns_telescope_multiselection ,
12111210 " TelescopeMultiSelection" ,
1212- row ,
1213- 0 ,
1214- - 1
1211+ { row , 0 },
1212+ { row , - 1 }
12151213 )
12161214 end
12171215 end
0 commit comments