@@ -58,6 +58,12 @@ use std::{
5858 time:: { Duration , Instant } ,
5959} ;
6060
61+ #[ derive( PartialEq ) ]
62+ pub enum Direction {
63+ Next ,
64+ Previous ,
65+ }
66+
6167impl LanguageClient {
6268 pub fn get_client ( & self , language_id : & LanguageId ) -> Result < Arc < RpcClient > > {
6369 self . get ( |state| state. clients . get ( language_id) . cloned ( ) ) ?
@@ -607,6 +613,45 @@ impl LanguageClient {
607613 Ok ( position)
608614 }
609615
616+ // moves the cursor to the next or previous diagnostic, depending on the value of direction.
617+ pub fn cycle_diagnostics ( & self , params : & Value , direction : Direction ) -> Result < ( ) > {
618+ let filename = self . vim ( ) ?. get_filename ( params) ?;
619+ let pos = self . vim ( ) ?. get_position ( params) ?;
620+ let mut diagnostics = self . get ( |state| state. diagnostics . clone ( ) ) ?;
621+ if let Some ( diagnostics) = diagnostics. get_mut ( & filename) {
622+ if direction == Direction :: Next {
623+ diagnostics. sort_by_key ( |edit| ( edit. range . start . line , edit. range . start . character ) ) ;
624+ } else {
625+ diagnostics. sort_by_key ( |edit| {
626+ (
627+ -( edit. range . start . line as i64 ) ,
628+ -( edit. range . start . character as i64 ) ,
629+ )
630+ } ) ;
631+ }
632+
633+ let ( line, col) = ( pos. line , pos. character ) ;
634+ if let Some ( ( _, diagnostic) ) = diagnostics. iter_mut ( ) . find_position ( |it| {
635+ let start = it. range . start ;
636+ if direction == Direction :: Next {
637+ start. line > line || ( start. line == line && start. character > col)
638+ } else {
639+ start. line < line || ( start. line == line && start. character < col)
640+ }
641+ } ) {
642+ let line = diagnostic. range . start . line + 1 ;
643+ let col = diagnostic. range . start . character + 1 ;
644+ let _: String = self . vim ( ) ?. rpcclient . call ( "cursor" , json ! ( [ line, col] ) ) ?;
645+ } else {
646+ self . vim ( ) ?. echomsg ( "No diagnostics found" ) ?;
647+ }
648+ } else {
649+ self . vim ( ) ?. echomsg ( "No diagnostics found" ) ?;
650+ }
651+
652+ Ok ( ( ) )
653+ }
654+
610655 fn update_quickfixlist ( & self ) -> Result < ( ) > {
611656 let diagnostics = self . get ( |state| state. diagnostics . clone ( ) ) ?;
612657 let qflist: Vec < _ > = diagnostics
0 commit comments