@@ -343,20 +343,40 @@ impl State {
343343 } )
344344 } ) . collect :: < Result < Vec < _ > > > ( ) ?;
345345
346- let HighlightSource { buffer, source } =
347- if let Some ( highlight_source) = self . document_highlight_source {
348- highlight_source
346+ let buffer = self . call ( None , "nvim_win_get_buf" , json ! ( [ 0 ] ) ) ?;
347+
348+ let source = if let Some ( hs) = self . document_highlight_source {
349+ if hs. buffer == buffer {
350+ // If we want to highlight in the same buffer as last time, we can reuse
351+ // the previous source.
352+ Some ( hs. source )
349353 } else {
350- let buffer = self . call ( None , "nvim_win_get_buf" , json ! ( [ 0 ] ) ) ?;
354+ // Clear the highlight in the previous buffer.
355+ self . notify (
356+ None ,
357+ "nvim_buf_clear_highlight" ,
358+ json ! ( [ hs. buffer, hs. source, 0 , -1 ] ) ,
359+ ) ?;
360+
361+ None
362+ }
363+ } else {
364+ None
365+ } ;
366+
367+ let source = match source {
368+ Some ( source) => source,
369+ None => {
370+ // Create a new source.
351371 let source = self . call (
352372 None ,
353373 "nvim_buf_add_highlight" ,
354374 json ! ( [ buffer, 0 , "Error" , 1 , 1 , 1 ] ) ,
355375 ) ?;
356- let highlight_source = HighlightSource { buffer, source } ;
357- self . document_highlight_source = Some ( highlight_source ) ;
358- highlight_source
359- } ;
376+ self . document_highlight_source = Some ( HighlightSource { buffer, source } ) ;
377+ source
378+ }
379+ } ;
360380
361381 self . notify (
362382 None ,
0 commit comments