more enterprisey assists API

This commit is contained in:
Aleksey Kladov 2019-01-03 18:59:17 +03:00
parent aea2183799
commit a4635a199b
11 changed files with 287 additions and 210 deletions

View file

@ -333,19 +333,9 @@ impl db::RootDatabase {
pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> { pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
let file = self.source_file(frange.file_id); let file = self.source_file(frange.file_id);
let offset = frange.range.start(); assists::assists(&file, frange.range)
let actions = vec![
assists::flip_comma(&file, offset).map(|f| f()),
assists::add_derive(&file, offset).map(|f| f()),
assists::add_impl(&file, offset).map(|f| f()),
assists::change_visibility(&file, offset).map(|f| f()),
assists::introduce_variable(&file, frange.range).map(|f| f()),
];
actions
.into_iter() .into_iter()
.filter_map(|local_edit| { .map(|local_edit| SourceChange::from_local_edit(frange.file_id, local_edit))
Some(SourceChange::from_local_edit(frange.file_id, local_edit?))
})
.collect() .collect()
} }
@ -440,7 +430,7 @@ impl db::RootDatabase {
.map(|(file_id, text_range)| SourceFileEdit { .map(|(file_id, text_range)| SourceFileEdit {
file_id: *file_id, file_id: *file_id,
edit: { edit: {
let mut builder = ra_text_edit::TextEditBuilder::new(); let mut builder = ra_text_edit::TextEditBuilder::default();
builder.replace(*text_range, new_name.into()); builder.replace(*text_range, new_name.into());
builder.finish() builder.finish()
}, },

View file

@ -9,8 +9,13 @@ mod add_impl;
mod introduce_variable; mod introduce_variable;
mod change_visibility; mod change_visibility;
use ra_text_edit::TextEdit; use ra_text_edit::{TextEdit, TextEditBuilder};
use ra_syntax::{Direction, SyntaxNodeRef, TextUnit}; use ra_syntax::{
Direction, SyntaxNodeRef, TextUnit, TextRange,SourceFileNode, AstNode,
algo::{find_leaf_at_offset, find_covering_node, LeafAtOffset},
};
use crate::find_node_at_offset;
pub use self::{ pub use self::{
flip_comma::flip_comma, flip_comma::flip_comma,
@ -20,6 +25,21 @@ pub use self::{
change_visibility::change_visibility, change_visibility::change_visibility,
}; };
/// Return all the assists applicable at the given position.
pub fn assists(file: &SourceFileNode, range: TextRange) -> Vec<LocalEdit> {
let ctx = AssistCtx::new(file, range);
[
flip_comma,
add_derive,
add_impl,
introduce_variable,
change_visibility,
]
.iter()
.filter_map(|&assist| ctx.clone().apply(assist))
.collect()
}
#[derive(Debug)] #[derive(Debug)]
pub struct LocalEdit { pub struct LocalEdit {
pub label: String, pub label: String,
@ -32,3 +52,134 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
.skip(1) .skip(1)
.find(|node| !node.kind().is_trivia()) .find(|node| !node.kind().is_trivia())
} }
/// `AssistCtx` allows to apply an assist or check if it could be applied.
///
/// Assists use a somewhat overengeneered approach, given the current needs. The
/// assists workflow consists of two phases. In the first phase, a user asks for
/// the list of available assists. In the second phase, the user picks a
/// particular assist and it gets applied.
///
/// There are two peculiarities here:
///
/// * first, we ideally avoid computing more things then neccessary to answer
/// "is assist applicable" in the first phase.
/// * second, when we are appling assist, we don't have a gurantee that there
/// weren't any changes between the point when user asked for assists and when
/// they applied a particular assist. So, when applying assist, we need to do
/// all the checks from scratch.
///
/// To avoid repeating the same code twice for both "check" and "apply"
/// functions, we use an approach remeniscent of that of Django's function based
/// views dealing with forms. Each assist receives a runtime parameter,
/// `should_compute_edit`. It first check if an edit is applicable (potentially
/// computing info required to compute the actual edit). If it is applicable,
/// and `should_compute_edit` is `true`, it then computes the actual edit.
///
/// So, to implement the original assists workflow, we can first apply each edit
/// with `should_compute_edit = false`, and then applying the selected edit
/// again, with `should_compute_edit = true` this time.
///
/// Note, however, that we don't actually use such two-phase logic at the
/// moment, because the LSP API is pretty awkward in this place, and it's much
/// easier to just compute the edit eagarly :-)
#[derive(Debug, Clone)]
pub struct AssistCtx<'a> {
source_file: &'a SourceFileNode,
range: TextRange,
should_compute_edit: bool,
}
#[derive(Debug)]
pub enum Assist {
Applicable,
Edit(LocalEdit),
}
#[derive(Default)]
struct AssistBuilder {
edit: TextEditBuilder,
cursor_position: Option<TextUnit>,
}
impl<'a> AssistCtx<'a> {
pub fn new(source_file: &'a SourceFileNode, range: TextRange) -> AssistCtx {
AssistCtx {
source_file,
range,
should_compute_edit: false,
}
}
pub fn apply(mut self, assist: fn(AssistCtx) -> Option<Assist>) -> Option<LocalEdit> {
self.should_compute_edit = true;
match assist(self) {
None => None,
Some(Assist::Edit(e)) => Some(e),
Some(Assist::Applicable) => unreachable!(),
}
}
pub fn check(mut self, assist: fn(AssistCtx) -> Option<Assist>) -> bool {
self.should_compute_edit = false;
match assist(self) {
None => false,
Some(Assist::Edit(_)) => unreachable!(),
Some(Assist::Applicable) => true,
}
}
fn build(self, label: impl Into<String>, f: impl FnOnce(&mut AssistBuilder)) -> Option<Assist> {
if !self.should_compute_edit {
return Some(Assist::Applicable);
}
let mut edit = AssistBuilder::default();
f(&mut edit);
Some(Assist::Edit(LocalEdit {
label: label.into(),
edit: edit.edit.finish(),
cursor_position: edit.cursor_position,
}))
}
pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<SyntaxNodeRef<'a>> {
find_leaf_at_offset(self.source_file.syntax(), self.range.start())
}
pub(crate) fn node_at_offset<N: AstNode<'a>>(&self) -> Option<N> {
find_node_at_offset(self.source_file.syntax(), self.range.start())
}
pub(crate) fn covering_node(&self) -> SyntaxNodeRef<'a> {
find_covering_node(self.source_file.syntax(), self.range)
}
}
impl AssistBuilder {
fn replace(&mut self, range: TextRange, replace_with: impl Into<String>) {
self.edit.replace(range, replace_with.into())
}
#[allow(unused)]
fn delete(&mut self, range: TextRange) {
self.edit.delete(range)
}
fn insert(&mut self, offset: TextUnit, text: impl Into<String>) {
self.edit.insert(offset, text.into())
}
fn set_cursor(&mut self, offset: TextUnit) {
self.cursor_position = Some(offset)
}
}
#[cfg(test)]
fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
crate::test_utils::check_action(before, after, |file, off| {
let range = TextRange::offset_len(off, 0.into());
AssistCtx::new(file, range).apply(assist)
})
}
#[cfg(test)]
fn check_assist_range(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
crate::test_utils::check_action_range(before, after, |file, range| {
AssistCtx::new(file, range).apply(assist)
})
}

View file

@ -1,43 +1,31 @@
use ra_text_edit::TextEditBuilder;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, AttrsOwner}, ast::{self, AstNode, AttrsOwner},
SourceFileNode,
SyntaxKind::{WHITESPACE, COMMENT}, SyntaxKind::{WHITESPACE, COMMENT},
TextUnit, TextUnit,
}; };
use crate::{ use crate::assists::{AssistCtx, Assist};
find_node_at_offset,
assists::LocalEdit,
};
pub fn add_derive<'a>( pub fn add_derive(ctx: AssistCtx) -> Option<Assist> {
file: &'a SourceFileNode, let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
offset: TextUnit,
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
let node_start = derive_insertion_offset(nominal)?; let node_start = derive_insertion_offset(nominal)?;
return Some(move || { ctx.build("add `#[derive]`", |edit| {
let derive_attr = nominal let derive_attr = nominal
.attrs() .attrs()
.filter_map(|x| x.as_call()) .filter_map(|x| x.as_call())
.filter(|(name, _arg)| name == "derive") .filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg) .map(|(_name, arg)| arg)
.next(); .next();
let mut edit = TextEditBuilder::new();
let offset = match derive_attr { let offset = match derive_attr {
None => { None => {
edit.insert(node_start, "#[derive()]\n".to_string()); edit.insert(node_start, "#[derive()]\n");
node_start + TextUnit::of_str("#[derive(") node_start + TextUnit::of_str("#[derive(")
} }
Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'), Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'),
}; };
LocalEdit { edit.set_cursor(offset)
label: "add `#[derive]`".to_string(), })
edit: edit.finish(),
cursor_position: Some(offset),
} }
});
// Insert `derive` after doc comments. // Insert `derive` after doc comments.
fn derive_insertion_offset(nominal: ast::NominalDef) -> Option<TextUnit> { fn derive_insertion_offset(nominal: ast::NominalDef) -> Option<TextUnit> {
@ -47,39 +35,39 @@ pub fn add_derive<'a>(
.find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
Some(non_ws_child.range().start()) Some(non_ws_child.range().start())
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::check_action; use crate::assists::check_assist;
#[test] #[test]
fn add_derive_new() { fn add_derive_new() {
check_action( check_assist(
add_derive,
"struct Foo { a: i32, <|>}", "struct Foo { a: i32, <|>}",
"#[derive(<|>)]\nstruct Foo { a: i32, }", "#[derive(<|>)]\nstruct Foo { a: i32, }",
|file, off| add_derive(file, off).map(|f| f()),
); );
check_action( check_assist(
add_derive,
"struct Foo { <|> a: i32, }", "struct Foo { <|> a: i32, }",
"#[derive(<|>)]\nstruct Foo { a: i32, }", "#[derive(<|>)]\nstruct Foo { a: i32, }",
|file, off| add_derive(file, off).map(|f| f()),
); );
} }
#[test] #[test]
fn add_derive_existing() { fn add_derive_existing() {
check_action( check_assist(
add_derive,
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }", "#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }", "#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
|file, off| add_derive(file, off).map(|f| f()),
); );
} }
#[test] #[test]
fn add_derive_new_with_doc_comment() { fn add_derive_new_with_doc_comment() {
check_action( check_assist(
add_derive,
" "
/// `Foo` is a pretty important struct. /// `Foo` is a pretty important struct.
/// It does stuff. /// It does stuff.
@ -91,7 +79,6 @@ struct Foo { a: i32<|>, }
#[derive(<|>)] #[derive(<|>)]
struct Foo { a: i32, } struct Foo { a: i32, }
", ",
|file, off| add_derive(file, off).map(|f| f()),
); );
} }
} }

View file

@ -1,23 +1,16 @@
use join_to_string::join; use join_to_string::join;
use ra_text_edit::TextEditBuilder;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, NameOwner, TypeParamsOwner}, ast::{self, AstNode, NameOwner, TypeParamsOwner},
SourceFileNode,
TextUnit, TextUnit,
}; };
use crate::{find_node_at_offset, assists::LocalEdit}; use crate::assists::{AssistCtx, Assist};
pub fn add_impl<'a>( pub fn add_impl(ctx: AssistCtx) -> Option<Assist> {
file: &'a SourceFileNode, let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
offset: TextUnit,
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
let name = nominal.name()?; let name = nominal.name()?;
ctx.build("add impl", |edit| {
Some(move || {
let type_params = nominal.type_param_list(); let type_params = nominal.type_param_list();
let mut edit = TextEditBuilder::new();
let start_offset = nominal.syntax().range().end(); let start_offset = nominal.syntax().range().end();
let mut buf = String::new(); let mut buf = String::new();
buf.push_str("\n\nimpl"); buf.push_str("\n\nimpl");
@ -40,38 +33,33 @@ pub fn add_impl<'a>(
.to_buf(&mut buf); .to_buf(&mut buf);
} }
buf.push_str(" {\n"); buf.push_str(" {\n");
let offset = start_offset + TextUnit::of_str(&buf); edit.set_cursor(start_offset + TextUnit::of_str(&buf));
buf.push_str("\n}"); buf.push_str("\n}");
edit.insert(start_offset, buf); edit.insert(start_offset, buf);
LocalEdit {
label: "add impl".to_string(),
edit: edit.finish(),
cursor_position: Some(offset),
}
}) })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::check_action; use crate::assists::check_assist;
#[test] #[test]
fn test_add_impl() { fn test_add_impl() {
check_action( check_assist(
add_impl,
"struct Foo {<|>}\n", "struct Foo {<|>}\n",
"struct Foo {}\n\nimpl Foo {\n<|>\n}\n", "struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
|file, off| add_impl(file, off).map(|f| f()),
); );
check_action( check_assist(
add_impl,
"struct Foo<T: Clone> {<|>}", "struct Foo<T: Clone> {<|>}",
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}", "struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
|file, off| add_impl(file, off).map(|f| f()),
); );
check_action( check_assist(
add_impl,
"struct Foo<'a, T: Foo<'a>> {<|>}", "struct Foo<'a, T: Foo<'a>> {<|>}",
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}", "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}",
|file, off| add_impl(file, off).map(|f| f()),
); );
} }

View file

@ -1,90 +1,74 @@
use ra_text_edit::TextEditBuilder;
use ra_syntax::{ use ra_syntax::{
SourceFileNode,
algo::find_leaf_at_offset,
SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF}, SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF},
TextUnit,
}; };
use crate::assists::LocalEdit; use crate::assists::{AssistCtx, Assist};
pub fn change_visibility<'a>( pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> {
file: &'a SourceFileNode, let keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() {
offset: TextUnit,
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
let syntax = file.syntax();
let keyword = find_leaf_at_offset(syntax, offset).find(|leaf| match leaf.kind() {
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true,
_ => false, _ => false,
})?; })?;
let parent = keyword.parent()?; let parent = keyword.parent()?;
let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF];
// Parent is not a definition, can't add visibility
if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) {
return None;
}
// Already have visibility, do nothing
if parent.children().any(|child| child.kind() == VISIBILITY) {
return None;
}
let node_start = parent.range().start(); let node_start = parent.range().start();
Some(move || { ctx.build("make pub crate", |edit| {
let mut edit = TextEditBuilder::new(); edit.insert(node_start, "pub(crate) ");
edit.set_cursor(node_start);
if !def_kws.iter().any(|&def_kw| def_kw == parent.kind())
|| parent.children().any(|child| child.kind() == VISIBILITY)
{
return LocalEdit {
label: "make pub crate".to_string(),
edit: edit.finish(),
cursor_position: Some(offset),
};
}
edit.insert(node_start, "pub(crate) ".to_string());
LocalEdit {
label: "make pub crate".to_string(),
edit: edit.finish(),
cursor_position: Some(node_start),
}
}) })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::check_action; use crate::assists::check_assist;
#[test] #[test]
fn test_change_visibility() { fn test_change_visibility() {
check_action( check_assist(
change_visibility,
"<|>fn foo() {}", "<|>fn foo() {}",
"<|>pub(crate) fn foo() {}", "<|>pub(crate) fn foo() {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
check_action( check_assist(
change_visibility,
"f<|>n foo() {}", "f<|>n foo() {}",
"<|>pub(crate) fn foo() {}", "<|>pub(crate) fn foo() {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
check_action( check_assist(
change_visibility,
"<|>struct Foo {}", "<|>struct Foo {}",
"<|>pub(crate) struct Foo {}", "<|>pub(crate) struct Foo {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
check_action("<|>mod foo {}", "<|>pub(crate) mod foo {}", |file, off| { check_assist(
change_visibility(file, off).map(|f| f()) change_visibility,
}); "<|>mod foo {}",
check_action( "<|>pub(crate) mod foo {}",
);
check_assist(
change_visibility,
"<|>trait Foo {}", "<|>trait Foo {}",
"<|>pub(crate) trait Foo {}", "<|>pub(crate) trait Foo {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
check_action("m<|>od {}", "<|>pub(crate) mod {}", |file, off| { check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}");
change_visibility(file, off).map(|f| f()) check_assist(
}); change_visibility,
check_action(
"pub(crate) f<|>n foo() {}", "pub(crate) f<|>n foo() {}",
"pub(crate) f<|>n foo() {}", "pub(crate) f<|>n foo() {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
check_action( check_assist(
change_visibility,
"unsafe f<|>n foo() {}", "unsafe f<|>n foo() {}",
"<|>pub(crate) unsafe fn foo() {}", "<|>pub(crate) unsafe fn foo() {}",
|file, off| change_visibility(file, off).map(|f| f()),
); );
} }
} }

View file

@ -1,45 +1,31 @@
use ra_text_edit::TextEditBuilder;
use ra_syntax::{ use ra_syntax::{
algo::find_leaf_at_offset, Direction,
Direction, SourceFileNode,
SyntaxKind::COMMA, SyntaxKind::COMMA,
TextUnit,
}; };
use crate::assists::{LocalEdit, non_trivia_sibling}; use crate::assists::{non_trivia_sibling, AssistCtx, Assist};
pub fn flip_comma<'a>( pub fn flip_comma(ctx: AssistCtx) -> Option<Assist> {
file: &'a SourceFileNode, let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;
offset: TextUnit,
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
let syntax = file.syntax();
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
let prev = non_trivia_sibling(comma, Direction::Prev)?; let prev = non_trivia_sibling(comma, Direction::Prev)?;
let next = non_trivia_sibling(comma, Direction::Next)?; let next = non_trivia_sibling(comma, Direction::Next)?;
Some(move || { ctx.build("flip comma", |edit| {
let mut edit = TextEditBuilder::new(); edit.replace(prev.range(), next.text());
edit.replace(prev.range(), next.text().to_string()); edit.replace(next.range(), prev.text());
edit.replace(next.range(), prev.text().to_string());
LocalEdit {
label: "flip comma".to_string(),
edit: edit.finish(),
cursor_position: None,
}
}) })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::check_action; use crate::assists::check_assist;
#[test] #[test]
fn test_swap_comma() { fn flip_comma_works_for_function_parameters() {
check_action( check_assist(
flip_comma,
"fn foo(x: i32,<|> y: Result<(), ()>) {}", "fn foo(x: i32,<|> y: Result<(), ()>) {}",
"fn foo(y: Result<(), ()>,<|> x: i32) {}", "fn foo(y: Result<(), ()>,<|> x: i32) {}",
|file, off| flip_comma(file, off).map(|f| f()),
) )
} }
} }

View file

@ -1,19 +1,13 @@
use ra_text_edit::TextEditBuilder;
use ra_syntax::{ use ra_syntax::{
algo::{find_covering_node},
ast::{self, AstNode}, ast::{self, AstNode},
SourceFileNode, SyntaxKind::WHITESPACE,
SyntaxKind::{WHITESPACE}, SyntaxNodeRef, TextUnit,
SyntaxNodeRef, TextRange, TextUnit,
}; };
use crate::assists::LocalEdit; use crate::assists::{AssistCtx, Assist};
pub fn introduce_variable<'a>( pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> {
file: &'a SourceFileNode, let node = ctx.covering_node();
range: TextRange,
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
let node = find_covering_node(file.syntax(), range);
let expr = node.ancestors().filter_map(ast::Expr::cast).next()?; let expr = node.ancestors().filter_map(ast::Expr::cast).next()?;
let anchor_stmt = anchor_stmt(expr)?; let anchor_stmt = anchor_stmt(expr)?;
@ -21,9 +15,8 @@ pub fn introduce_variable<'a>(
if indent.kind() != WHITESPACE { if indent.kind() != WHITESPACE {
return None; return None;
} }
return Some(move || { ctx.build("introduce variable", move |edit| {
let mut buf = String::new(); let mut buf = String::new();
let mut edit = TextEditBuilder::new();
buf.push_str("let var_name = "); buf.push_str("let var_name = ");
expr.syntax().text().push_to(&mut buf); expr.syntax().text().push_to(&mut buf);
@ -40,13 +33,9 @@ pub fn introduce_variable<'a>(
edit.replace(expr.syntax().range(), "var_name".to_string()); edit.replace(expr.syntax().range(), "var_name".to_string());
edit.insert(anchor_stmt.range().start(), buf); edit.insert(anchor_stmt.range().start(), buf);
} }
let cursor_position = anchor_stmt.range().start() + TextUnit::of_str("let "); edit.set_cursor(anchor_stmt.range().start() + TextUnit::of_str("let "));
LocalEdit { })
label: "introduce variable".to_string(),
edit: edit.finish(),
cursor_position: Some(cursor_position),
} }
});
/// Statement or last in the block expression, which will follow /// Statement or last in the block expression, which will follow
/// the freshly introduced var. /// the freshly introduced var.
@ -67,16 +56,16 @@ pub fn introduce_variable<'a>(
false false
}) })
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::check_action_range; use crate::assists::check_assist_range;
#[test] #[test]
fn test_introduce_var_simple() { fn test_introduce_var_simple() {
check_action_range( check_assist_range(
introduce_variable,
" "
fn foo() { fn foo() {
foo(<|>1 + 1<|>); foo(<|>1 + 1<|>);
@ -86,13 +75,13 @@ fn foo() {
let <|>var_name = 1 + 1; let <|>var_name = 1 + 1;
foo(var_name); foo(var_name);
}", }",
|file, range| introduce_variable(file, range).map(|f| f()),
); );
} }
#[test] #[test]
fn test_introduce_var_expr_stmt() { fn test_introduce_var_expr_stmt() {
check_action_range( check_assist_range(
introduce_variable,
" "
fn foo() { fn foo() {
<|>1 + 1<|>; <|>1 + 1<|>;
@ -101,13 +90,13 @@ fn foo() {
fn foo() { fn foo() {
let <|>var_name = 1 + 1; let <|>var_name = 1 + 1;
}", }",
|file, range| introduce_variable(file, range).map(|f| f()),
); );
} }
#[test] #[test]
fn test_introduce_var_part_of_expr_stmt() { fn test_introduce_var_part_of_expr_stmt() {
check_action_range( check_assist_range(
introduce_variable,
" "
fn foo() { fn foo() {
<|>1<|> + 1; <|>1<|> + 1;
@ -117,13 +106,13 @@ fn foo() {
let <|>var_name = 1; let <|>var_name = 1;
var_name + 1; var_name + 1;
}", }",
|file, range| introduce_variable(file, range).map(|f| f()),
); );
} }
#[test] #[test]
fn test_introduce_var_last_expr() { fn test_introduce_var_last_expr() {
check_action_range( check_assist_range(
introduce_variable,
" "
fn foo() { fn foo() {
bar(<|>1 + 1<|>) bar(<|>1 + 1<|>)
@ -133,13 +122,13 @@ fn foo() {
let <|>var_name = 1 + 1; let <|>var_name = 1 + 1;
bar(var_name) bar(var_name)
}", }",
|file, range| introduce_variable(file, range).map(|f| f()),
); );
} }
#[test] #[test]
fn test_introduce_var_last_full_expr() { fn test_introduce_var_last_full_expr() {
check_action_range( check_assist_range(
introduce_variable,
" "
fn foo() { fn foo() {
<|>bar(1 + 1)<|> <|>bar(1 + 1)<|>
@ -149,7 +138,6 @@ fn foo() {
let <|>var_name = bar(1 + 1); let <|>var_name = bar(1 + 1);
var_name var_name
}", }",
|file, range| introduce_variable(file, range).map(|f| f()),
); );
} }

View file

@ -57,7 +57,7 @@ fn check_unnecessary_braces_in_use_statement(
text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(single_use_tree) text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(single_use_tree)
.unwrap_or_else(|| { .unwrap_or_else(|| {
let to_replace = single_use_tree.syntax().text().to_string(); let to_replace = single_use_tree.syntax().text().to_string();
let mut edit_builder = TextEditBuilder::new(); let mut edit_builder = TextEditBuilder::default();
edit_builder.delete(range); edit_builder.delete(range);
edit_builder.insert(range.start(), to_replace); edit_builder.insert(range.start(), to_replace);
edit_builder.finish() edit_builder.finish()
@ -93,7 +93,7 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
let start = use_tree_list_node.prev_sibling()?.range().start(); let start = use_tree_list_node.prev_sibling()?.range().start();
let end = use_tree_list_node.range().end(); let end = use_tree_list_node.range().end();
let range = TextRange::from_to(start, end); let range = TextRange::from_to(start, end);
let mut edit_builder = TextEditBuilder::new(); let mut edit_builder = TextEditBuilder::default();
edit_builder.delete(range); edit_builder.delete(range);
return Some(edit_builder.finish()); return Some(edit_builder.finish());
} }
@ -111,7 +111,7 @@ fn check_struct_shorthand_initialization(
let field_name = name_ref.syntax().text().to_string(); let field_name = name_ref.syntax().text().to_string();
let field_expr = expr.syntax().text().to_string(); let field_expr = expr.syntax().text().to_string();
if field_name == field_expr { if field_name == field_expr {
let mut edit_builder = TextEditBuilder::new(); let mut edit_builder = TextEditBuilder::default();
edit_builder.delete(named_field.syntax().range()); edit_builder.delete(named_field.syntax().range());
edit_builder.insert(named_field.syntax().range().start(), field_name); edit_builder.insert(named_field.syntax().range().start(), field_name);
let edit = edit_builder.finish(); let edit = edit_builder.finish();

View file

@ -21,7 +21,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
None => { None => {
return LocalEdit { return LocalEdit {
label: "join lines".to_string(), label: "join lines".to_string(),
edit: TextEditBuilder::new().finish(), edit: TextEditBuilder::default().finish(),
cursor_position: None, cursor_position: None,
}; };
} }
@ -33,7 +33,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
}; };
let node = find_covering_node(file.syntax(), range); let node = find_covering_node(file.syntax(), range);
let mut edit = TextEditBuilder::new(); let mut edit = TextEditBuilder::default();
for node in node.descendants() { for node in node.descendants() {
let text = match node.leaf_text() { let text = match node.leaf_text() {
Some(text) => text, Some(text) => text,
@ -76,7 +76,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
let indent = node_indent(file, comment.syntax())?; let indent = node_indent(file, comment.syntax())?;
let inserted = format!("\n{}{} ", indent, prefix); let inserted = format!("\n{}{} ", indent, prefix);
let cursor_position = offset + TextUnit::of_str(&inserted); let cursor_position = offset + TextUnit::of_str(&inserted);
let mut edit = TextEditBuilder::new(); let mut edit = TextEditBuilder::default();
edit.insert(offset, inserted); edit.insert(offset, inserted);
Some(LocalEdit { Some(LocalEdit {
label: "on enter".to_string(), label: "on enter".to_string(),
@ -127,7 +127,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit>
return None; return None;
} }
let offset = let_stmt.syntax().range().end(); let offset = let_stmt.syntax().range().end();
let mut edit = TextEditBuilder::new(); let mut edit = TextEditBuilder::default();
edit.insert(offset, ";".to_string()); edit.insert(offset, ";".to_string());
Some(LocalEdit { Some(LocalEdit {
label: "add semicolon".to_string(), label: "add semicolon".to_string(),

View file

@ -119,3 +119,9 @@ impl SyntaxTextSlice for ops::Range<TextUnit> {
TextRange::from_to(self.start, self.end).restrict(range) TextRange::from_to(self.start, self.end).restrict(range)
} }
} }
impl From<SyntaxText<'_>> for String {
fn from(text: SyntaxText) -> String {
text.to_string()
}
}

View file

@ -7,15 +7,12 @@ pub struct TextEdit {
atoms: Vec<AtomTextEdit>, atoms: Vec<AtomTextEdit>,
} }
#[derive(Debug)] #[derive(Debug, Default)]
pub struct TextEditBuilder { pub struct TextEditBuilder {
atoms: Vec<AtomTextEdit>, atoms: Vec<AtomTextEdit>,
} }
impl TextEditBuilder { impl TextEditBuilder {
pub fn new() -> TextEditBuilder {
TextEditBuilder { atoms: Vec::new() }
}
pub fn replace(&mut self, range: TextRange, replace_with: String) { pub fn replace(&mut self, range: TextRange, replace_with: String) {
self.atoms.push(AtomTextEdit::replace(range, replace_with)) self.atoms.push(AtomTextEdit::replace(range, replace_with))
} }