From e5cdcb8b124f5b7d59950429787e760e46388f72 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 17:08:09 +0300 Subject: Add a way to resolve certain assists --- crates/ide_assists/src/lib.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'crates/ide_assists/src/lib.rs') diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 88ae5c9a9..397d2a3d0 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -26,7 +26,7 @@ pub(crate) use crate::assist_context::{AssistContext, Assists}; pub use assist_config::AssistConfig; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum AssistKind { // FIXME: does the None variant make sense? Probably not. None, @@ -60,9 +60,27 @@ impl AssistKind { /// Unique identifier of the assist, should not be shown to the user /// directly. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct AssistId(pub &'static str, pub AssistKind); +// TODO kb docs +#[derive(Debug, Clone, Copy)] +pub enum AssistResolveStrategy { + None, + All, + Single(AssistId), +} + +impl AssistResolveStrategy { + pub fn should_resolve(&self, id: &AssistId) -> bool { + match self { + AssistResolveStrategy::None => false, + AssistResolveStrategy::All => true, + AssistResolveStrategy::Single(id_to_resolve) => id_to_resolve == id, + } + } +} + #[derive(Clone, Debug)] pub struct GroupLabel(pub String); @@ -91,7 +109,7 @@ impl Assist { pub fn get( db: &RootDatabase, config: &AssistConfig, - resolve: bool, + resolve: AssistResolveStrategy, range: FileRange, ) -> Vec { let sema = Semantics::new(db); -- cgit v1.2.3 From 1679a376f30c5ad8971c0f855074a3f489fee5fa Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 18:03:28 +0300 Subject: Resolve single assist only --- crates/ide_assists/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'crates/ide_assists/src/lib.rs') diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 397d2a3d0..01addffe9 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -17,6 +17,8 @@ mod tests; pub mod utils; pub mod ast_transform; +use std::str::FromStr; + use hir::Semantics; use ide_db::base_db::FileRange; use ide_db::{label::Label, source_change::SourceChange, RootDatabase}; @@ -56,6 +58,35 @@ impl AssistKind { _ => return false, } } + + pub fn name(&self) -> &str { + match self { + AssistKind::None => "None", + AssistKind::QuickFix => "QuickFix", + AssistKind::Generate => "Generate", + AssistKind::Refactor => "Refactor", + AssistKind::RefactorExtract => "RefactorExtract", + AssistKind::RefactorInline => "RefactorInline", + AssistKind::RefactorRewrite => "RefactorRewrite", + } + } +} + +impl FromStr for AssistKind { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "None" => Ok(AssistKind::None), + "QuickFix" => Ok(AssistKind::QuickFix), + "Generate" => Ok(AssistKind::Generate), + "Refactor" => Ok(AssistKind::Refactor), + "RefactorExtract" => Ok(AssistKind::RefactorExtract), + "RefactorInline" => Ok(AssistKind::RefactorInline), + "RefactorRewrite" => Ok(AssistKind::RefactorRewrite), + unknown => Err(format!("Unknown AssistKind: '{}'", unknown)), + } + } } /// Unique identifier of the assist, should not be shown to the user @@ -64,11 +95,11 @@ impl AssistKind { pub struct AssistId(pub &'static str, pub AssistKind); // TODO kb docs -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub enum AssistResolveStrategy { None, All, - Single(AssistId), + Single(String, AssistKind), } impl AssistResolveStrategy { @@ -76,7 +107,9 @@ impl AssistResolveStrategy { match self { AssistResolveStrategy::None => false, AssistResolveStrategy::All => true, - AssistResolveStrategy::Single(id_to_resolve) => id_to_resolve == id, + AssistResolveStrategy::Single(id_to_resolve, kind) => { + id_to_resolve == id.0 && kind == &id.1 + } } } } -- cgit v1.2.3 From 28293d370ffc4270bb6244579166f0df18962951 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 18:16:35 +0300 Subject: Add docs and use better naming --- crates/ide_assists/src/lib.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'crates/ide_assists/src/lib.rs') diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 01addffe9..5a0047f03 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -94,12 +94,27 @@ impl FromStr for AssistKind { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct AssistId(pub &'static str, pub AssistKind); -// TODO kb docs -#[derive(Debug, Clone)] +/// A way to control how many asssist to resolve during the assist resolution. +/// When an assist is resolved, its edits are calculated that might be costly to always do by default. +#[derive(Debug)] pub enum AssistResolveStrategy { + /// No assists should be resolved. None, + /// All assists should be resolved. All, - Single(String, AssistKind), + /// Only a certain assists should be resolved. + Single(SingleResolve), +} + +/// Hold the [`AssistId`] data of a certain assist to resolve. +/// The original id object cannot be used due to a `'static` lifetime +/// and the requirement to construct this struct dynamically during the resolve handling. +#[derive(Debug)] +pub struct SingleResolve { + /// The id of the assist. + pub assist_id: String, + // The kind of the assist. + pub assist_kind: AssistKind, } impl AssistResolveStrategy { @@ -107,8 +122,8 @@ impl AssistResolveStrategy { match self { AssistResolveStrategy::None => false, AssistResolveStrategy::All => true, - AssistResolveStrategy::Single(id_to_resolve, kind) => { - id_to_resolve == id.0 && kind == &id.1 + AssistResolveStrategy::Single(single_resolve) => { + single_resolve.assist_id == id.0 && single_resolve.assist_kind == id.1 } } } -- cgit v1.2.3 From 53a73de3d10e20a13153c94e050a8ad9230169eb Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 18:44:58 +0300 Subject: Small fixes --- crates/ide_assists/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ide_assists/src/lib.rs') diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 5a0047f03..723531078 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -28,7 +28,7 @@ pub(crate) use crate::assist_context::{AssistContext, Assists}; pub use assist_config::AssistConfig; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AssistKind { // FIXME: does the None variant make sense? Probably not. None, @@ -91,7 +91,7 @@ impl FromStr for AssistKind { /// Unique identifier of the assist, should not be shown to the user /// directly. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AssistId(pub &'static str, pub AssistKind); /// A way to control how many asssist to resolve during the assist resolution. -- cgit v1.2.3 From 90fc32937785b3f17899f14d8cb2f7b3738a9850 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 3 May 2021 19:35:44 +0300 Subject: Index retrieval fix --- crates/ide_assists/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ide_assists/src/lib.rs') diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 723531078..2e0c58504 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -102,7 +102,7 @@ pub enum AssistResolveStrategy { None, /// All assists should be resolved. All, - /// Only a certain assists should be resolved. + /// Only a certain assist should be resolved. Single(SingleResolve), } -- cgit v1.2.3