From fdd4df97ba5ce1f59abf9e945052fc6f3e077c3a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 15:26:40 +0200 Subject: Use SourceChange for assists --- crates/ra_assists/src/lib.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index f4f37614f..8cd8f89c4 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -19,9 +19,8 @@ pub mod ast_transform; use hir::Semantics; use ra_db::{FileId, FileRange}; -use ra_ide_db::RootDatabase; -use ra_syntax::{TextRange, TextSize}; -use ra_text_edit::TextEdit; +use ra_ide_db::{source_change::SourceChange, RootDatabase}; +use ra_syntax::TextRange; pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; @@ -57,21 +56,14 @@ impl AssistLabel { } } -#[derive(Debug, Clone)] -pub struct AssistAction { - pub edit: TextEdit, - pub cursor_position: Option, - pub file: AssistFile, -} - #[derive(Debug, Clone)] pub struct ResolvedAssist { pub label: AssistLabel, - pub action: AssistAction, + pub action: SourceChange, } #[derive(Debug, Clone, Copy)] -pub enum AssistFile { +enum AssistFile { CurrentFile, TargetFile(FileId), } -- cgit v1.2.3 From 0970c3454baa0164df81a9ff665430d486687331 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 16:01:47 +0200 Subject: Rename --- crates/ra_assists/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 8cd8f89c4..6e81ea8ee 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -59,7 +59,7 @@ impl AssistLabel { #[derive(Debug, Clone)] pub struct ResolvedAssist { pub label: AssistLabel, - pub action: SourceChange, + pub source_change: SourceChange, } #[derive(Debug, Clone, Copy)] -- cgit v1.2.3 From 020ca6695f4d58f651984c4fbe2227d891896bb3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 16:39:11 +0200 Subject: Simplify --- crates/ra_assists/src/lib.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 6e81ea8ee..13ea45ec7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -18,7 +18,7 @@ pub mod utils; pub mod ast_transform; use hir::Semantics; -use ra_db::{FileId, FileRange}; +use ra_db::FileRange; use ra_ide_db::{source_change::SourceChange, RootDatabase}; use ra_syntax::TextRange; @@ -62,18 +62,6 @@ pub struct ResolvedAssist { pub source_change: SourceChange, } -#[derive(Debug, Clone, Copy)] -enum AssistFile { - CurrentFile, - TargetFile(FileId), -} - -impl Default for AssistFile { - fn default() -> Self { - Self::CurrentFile - } -} - /// Return all the assists applicable at the given position. /// /// Assists are returned in the "unresolved" state, that is only labels are -- cgit v1.2.3 From 51c02ab84f6b88ba39e2d0a3ed22bea51114b05a Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Tue, 5 May 2020 19:02:45 +0200 Subject: add Ok wrapping Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_assists/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 13ea45ec7..0473fd8c2 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -129,6 +129,7 @@ mod handlers { mod replace_qualified_name_with_use; mod replace_unwrap_with_match; mod split_import; + mod change_return_type_to_result; mod add_from_impl_for_enum; mod reorder_fields; mod unwrap_block; @@ -145,6 +146,7 @@ mod handlers { add_new::add_new, apply_demorgan::apply_demorgan, auto_import::auto_import, + change_return_type_to_result::change_return_type_to_result, change_visibility::change_visibility, early_return::convert_to_guarded_return, fill_match_arms::fill_match_arms, -- cgit v1.2.3 From 4867968d22899395e6551f22641b3617e995140c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 18:45:35 +0200 Subject: Refactor assists API to be more convenient for adding new assists It now duplicates completion API in its shape. --- crates/ra_assists/src/lib.rs | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 0473fd8c2..011613762 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -10,7 +10,7 @@ macro_rules! eprintln { ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; } -mod assist_ctx; +mod assist_context; mod marks; #[cfg(test)] mod tests; @@ -22,7 +22,7 @@ use ra_db::FileRange; use ra_ide_db::{source_change::SourceChange, RootDatabase}; use ra_syntax::TextRange; -pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; +pub(crate) use crate::assist_context::{AssistContext, Assists}; /// Unique identifier of the assist, should not be shown to the user /// directly. @@ -68,13 +68,12 @@ pub struct ResolvedAssist { /// returned, without actual edits. pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { let sema = Semantics::new(db); - let ctx = AssistCtx::new(&sema, range, false); - handlers::all() - .iter() - .filter_map(|f| f(ctx.clone())) - .flat_map(|it| it.0) - .map(|a| a.label) - .collect() + let ctx = AssistContext::new(sema, range); + let mut acc = Assists::new_unresolved(&ctx); + handlers::all().iter().for_each(|handler| { + handler(&mut acc, &ctx); + }); + acc.finish_unresolved() } /// Return all the assists applicable at the given position. @@ -83,31 +82,30 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec Vec { let sema = Semantics::new(db); - let ctx = AssistCtx::new(&sema, range, true); - let mut a = handlers::all() - .iter() - .filter_map(|f| f(ctx.clone())) - .flat_map(|it| it.0) - .map(|it| it.into_resolved().unwrap()) - .collect::>(); - a.sort_by_key(|it| it.label.target.len()); - a + let ctx = AssistContext::new(sema, range); + let mut acc = Assists::new_resolved(&ctx); + handlers::all().iter().for_each(|handler| { + handler(&mut acc, &ctx); + }); + acc.finish_resolved() } mod handlers { - use crate::{Assist, AssistCtx}; + use crate::{AssistContext, Assists}; - pub(crate) type Handler = fn(AssistCtx) -> Option; + pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; mod add_custom_impl; mod add_derive; mod add_explicit_type; + mod add_from_impl_for_enum; mod add_function; mod add_impl; mod add_missing_impl_members; mod add_new; mod apply_demorgan; mod auto_import; + mod change_return_type_to_result; mod change_visibility; mod early_return; mod fill_match_arms; @@ -124,14 +122,12 @@ mod handlers { mod raw_string; mod remove_dbg; mod remove_mut; + mod reorder_fields; mod replace_if_let_with_match; mod replace_let_with_if_let; mod replace_qualified_name_with_use; mod replace_unwrap_with_match; mod split_import; - mod change_return_type_to_result; - mod add_from_impl_for_enum; - mod reorder_fields; mod unwrap_block; pub(crate) fn all() -> &'static [Handler] { -- cgit v1.2.3 From c6b81bc013b5278b917d109b723405e0df413323 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 7 May 2020 17:09:59 +0200 Subject: Rename AssitLabel -> Assist --- crates/ra_assists/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 011613762..a91975d8c 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -30,7 +30,7 @@ pub(crate) use crate::assist_context::{AssistContext, Assists}; pub struct AssistId(pub &'static str); #[derive(Debug, Clone)] -pub struct AssistLabel { +pub struct Assist { pub id: AssistId, /// Short description of the assist, as shown in the UI. pub label: String, @@ -43,22 +43,22 @@ pub struct AssistLabel { #[derive(Clone, Debug)] pub struct GroupLabel(pub String); -impl AssistLabel { +impl Assist { pub(crate) fn new( id: AssistId, label: String, group: Option, target: TextRange, - ) -> AssistLabel { + ) -> Assist { // FIXME: make fields private, so that this invariant can't be broken assert!(label.starts_with(|c: char| c.is_uppercase())); - AssistLabel { id, label, group, target } + Assist { id, label, group, target } } } #[derive(Debug, Clone)] pub struct ResolvedAssist { - pub label: AssistLabel, + pub assist: Assist, pub source_change: SourceChange, } @@ -66,7 +66,7 @@ pub struct ResolvedAssist { /// /// Assists are returned in the "unresolved" state, that is only labels are /// returned, without actual edits. -pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { +pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { let sema = Semantics::new(db); let ctx = AssistContext::new(sema, range); let mut acc = Assists::new_unresolved(&ctx); -- cgit v1.2.3 From 28fcff125a73ab2fc4aeaa100fc472af5178db20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 7 May 2020 17:29:23 +0200 Subject: Nicer API --- crates/ra_assists/src/lib.rs | 72 ++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index a91975d8c..b6dc7cb1b 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -29,6 +29,9 @@ pub(crate) use crate::assist_context::{AssistContext, Assists}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AssistId(pub &'static str); +#[derive(Clone, Debug)] +pub struct GroupLabel(pub String); + #[derive(Debug, Clone)] pub struct Assist { pub id: AssistId, @@ -40,10 +43,41 @@ pub struct Assist { pub target: TextRange, } -#[derive(Clone, Debug)] -pub struct GroupLabel(pub String); +#[derive(Debug, Clone)] +pub struct ResolvedAssist { + pub assist: Assist, + pub source_change: SourceChange, +} impl Assist { + /// Return all the assists applicable at the given position. + /// + /// Assists are returned in the "unresolved" state, that is only labels are + /// returned, without actual edits. + pub fn unresolved(db: &RootDatabase, range: FileRange) -> Vec { + let sema = Semantics::new(db); + let ctx = AssistContext::new(sema, range); + let mut acc = Assists::new_unresolved(&ctx); + handlers::all().iter().for_each(|handler| { + handler(&mut acc, &ctx); + }); + acc.finish_unresolved() + } + + /// Return all the assists applicable at the given position. + /// + /// Assists are returned in the "resolved" state, that is with edit fully + /// computed. + pub fn resolved(db: &RootDatabase, range: FileRange) -> Vec { + let sema = Semantics::new(db); + let ctx = AssistContext::new(sema, range); + let mut acc = Assists::new_resolved(&ctx); + handlers::all().iter().for_each(|handler| { + handler(&mut acc, &ctx); + }); + acc.finish_resolved() + } + pub(crate) fn new( id: AssistId, label: String, @@ -56,40 +90,6 @@ impl Assist { } } -#[derive(Debug, Clone)] -pub struct ResolvedAssist { - pub assist: Assist, - pub source_change: SourceChange, -} - -/// Return all the assists applicable at the given position. -/// -/// Assists are returned in the "unresolved" state, that is only labels are -/// returned, without actual edits. -pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec { - let sema = Semantics::new(db); - let ctx = AssistContext::new(sema, range); - let mut acc = Assists::new_unresolved(&ctx); - handlers::all().iter().for_each(|handler| { - handler(&mut acc, &ctx); - }); - acc.finish_unresolved() -} - -/// Return all the assists applicable at the given position. -/// -/// Assists are returned in the "resolved" state, that is with edit fully -/// computed. -pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec { - let sema = Semantics::new(db); - let ctx = AssistContext::new(sema, range); - let mut acc = Assists::new_resolved(&ctx); - handlers::all().iter().for_each(|handler| { - handler(&mut acc, &ctx); - }); - acc.finish_resolved() -} - mod handlers { use crate::{AssistContext, Assists}; -- cgit v1.2.3