mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-04 15:45:25 +00:00
Edits with cursors
This commit is contained in:
parent
a7d31b55a4
commit
aa0d344581
5 changed files with 85 additions and 37 deletions
|
@ -10,7 +10,17 @@ use libsyntax2::{
|
|||
},
|
||||
};
|
||||
|
||||
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> {
|
||||
pub struct ActionResult {
|
||||
pub edit: Edit,
|
||||
pub cursor_position: CursorPosition,
|
||||
}
|
||||
|
||||
pub enum CursorPosition {
|
||||
Same,
|
||||
Offset(TextUnit),
|
||||
}
|
||||
|
||||
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
|
||||
let syntax = file.syntax();
|
||||
let syntax = syntax.as_ref();
|
||||
|
||||
|
@ -21,18 +31,27 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
|
|||
let mut edit = EditBuilder::new();
|
||||
edit.replace(left.range(), right.text());
|
||||
edit.replace(right.range(), left.text());
|
||||
edit.finish()
|
||||
ActionResult {
|
||||
edit: edit.finish(),
|
||||
cursor_position: CursorPosition::Same,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> {
|
||||
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> {
|
||||
let syntax = file.syntax();
|
||||
let syntax = syntax.as_ref();
|
||||
let nominal = find_node::<ast::NominalDef<_>>(syntax, offset)?;
|
||||
Some(move || {
|
||||
let mut edit = EditBuilder::new();
|
||||
edit.insert(nominal.syntax().range().start(), "#[derive()]\n".to_string());
|
||||
edit.finish()
|
||||
let node_start = nominal.syntax().range().start();
|
||||
edit.insert(node_start, "#[derive()]\n".to_string());
|
||||
ActionResult {
|
||||
edit: edit.finish(),
|
||||
cursor_position: CursorPosition::Offset(
|
||||
node_start + TextUnit::of_str("#[derive(")
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue