@ -1572,47 +1572,67 @@ fn tutor(
Ok ( ( ) )
Ok ( ( ) )
}
}
fn abort_goto_line_number_preview ( cx : & mut compositor ::Context ) {
if let Some ( last_selection ) = cx . editor . last_selection . take ( ) {
let scrolloff = cx . editor . config ( ) . scrolloff ;
let ( view , doc ) = current ! ( cx . editor ) ;
doc . set_selection ( view . id , last_selection ) ;
view . ensure_cursor_in_view ( doc , scrolloff ) ;
}
}
fn update_goto_line_number_preview (
cx : & mut compositor ::Context ,
args : & [ Cow < str > ] ,
) -> anyhow ::Result < ( ) > {
cx . editor . last_selection . get_or_insert_with ( | | {
let ( view , doc ) = current ! ( cx . editor ) ;
doc . selection ( view . id ) . clone ( )
} ) ;
let scrolloff = cx . editor . config ( ) . scrolloff ;
let line = args [ 0 ] . parse ::< usize > ( ) ? ;
goto_line_without_jumplist ( cx . editor , NonZeroUsize ::new ( line ) ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
view . ensure_cursor_in_view ( doc , scrolloff ) ;
Ok ( ( ) )
}
pub ( super ) fn goto_line_number (
pub ( super ) fn goto_line_number (
cx : & mut compositor ::Context ,
cx : & mut compositor ::Context ,
args : & [ Cow < str > ] ,
args : & [ Cow < str > ] ,
event : PromptEvent ,
event : PromptEvent ,
) -> anyhow ::Result < ( ) > {
) -> anyhow ::Result < ( ) > {
match event {
match event {
PromptEvent ::Abort = > {
PromptEvent ::Abort = > abort_goto_line_number_preview ( cx ) ,
if let Some ( line_number ) = cx . editor . last_line_number {
goto_line_impl ( cx . editor , NonZeroUsize ::new ( line_number ) ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
view . ensure_cursor_in_view ( doc , line_number ) ;
cx . editor . last_line_number = None ;
}
return Ok ( ( ) ) ;
}
PromptEvent ::Validate = > {
PromptEvent ::Validate = > {
ensure ! ( ! args . is_empty ( ) , "Line number required" ) ;
ensure ! ( ! args . is_empty ( ) , "Line number required" ) ;
cx . editor . last_line_number = None ;
}
// If we are invoked directly via a keybinding, Validate is
PromptEvent ::Update = > {
// sent without any prior Update events. Ensure the cursor
if args . is_empty ( ) {
// is moved to the appropriate location.
if let Some ( line_number ) = cx . editor . last_line_number {
update_goto_line_number_preview ( cx , args ) ? ;
// When a user hits backspace and there are no numbers left,
// we can bring them back to their original line
let last_selection = cx
goto_line_impl ( cx . editor , NonZeroUsize ::new ( line_number ) ) ;
. editor
let ( view , doc ) = current ! ( cx . editor ) ;
. last_selection
view . ensure_cursor_in_view ( doc , line_number ) ;
. take ( )
cx . editor . last_line_number = None ;
. expect ( "update_goto_line_number_preview should always set last_selection" ) ;
}
return Ok ( ( ) ) ;
}
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
let text = doc . text ( ) . slice ( .. ) ;
view . jumps . push ( ( doc . id ( ) , last_selection ) ) ;
let line = doc . selection ( view . id ) . primary ( ) . cursor_line ( text ) ;
cx . editor . last_line_number . get_or_insert ( line + 1 ) ;
}
}
// When a user hits backspace and there are no numbers left,
// we can bring them back to their original selection. If they
// begin typing numbers again, we'll start a new preview session.
PromptEvent ::Update if args . is_empty ( ) = > abort_goto_line_number_preview ( cx ) ,
PromptEvent ::Update = > update_goto_line_number_preview ( cx , args ) ? ,
}
}
let line = args [ 0 ] . parse ::< usize > ( ) ? ;
goto_line_impl ( cx . editor , NonZeroUsize ::new ( line ) ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
view . ensure_cursor_in_view ( doc , line ) ;
Ok ( ( ) )
Ok ( ( ) )
}
}