diff options
Diffstat (limited to 'crates/ide_assists/src/lib.rs')
-rw-r--r-- | crates/ide_assists/src/lib.rs | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 88ae5c9a9..2e0c58504 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs | |||
@@ -17,6 +17,8 @@ mod tests; | |||
17 | pub mod utils; | 17 | pub mod utils; |
18 | pub mod ast_transform; | 18 | pub mod ast_transform; |
19 | 19 | ||
20 | use std::str::FromStr; | ||
21 | |||
20 | use hir::Semantics; | 22 | use hir::Semantics; |
21 | use ide_db::base_db::FileRange; | 23 | use ide_db::base_db::FileRange; |
22 | use ide_db::{label::Label, source_change::SourceChange, RootDatabase}; | 24 | use ide_db::{label::Label, source_change::SourceChange, RootDatabase}; |
@@ -56,6 +58,35 @@ impl AssistKind { | |||
56 | _ => return false, | 58 | _ => return false, |
57 | } | 59 | } |
58 | } | 60 | } |
61 | |||
62 | pub fn name(&self) -> &str { | ||
63 | match self { | ||
64 | AssistKind::None => "None", | ||
65 | AssistKind::QuickFix => "QuickFix", | ||
66 | AssistKind::Generate => "Generate", | ||
67 | AssistKind::Refactor => "Refactor", | ||
68 | AssistKind::RefactorExtract => "RefactorExtract", | ||
69 | AssistKind::RefactorInline => "RefactorInline", | ||
70 | AssistKind::RefactorRewrite => "RefactorRewrite", | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | impl FromStr for AssistKind { | ||
76 | type Err = String; | ||
77 | |||
78 | fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
79 | match s { | ||
80 | "None" => Ok(AssistKind::None), | ||
81 | "QuickFix" => Ok(AssistKind::QuickFix), | ||
82 | "Generate" => Ok(AssistKind::Generate), | ||
83 | "Refactor" => Ok(AssistKind::Refactor), | ||
84 | "RefactorExtract" => Ok(AssistKind::RefactorExtract), | ||
85 | "RefactorInline" => Ok(AssistKind::RefactorInline), | ||
86 | "RefactorRewrite" => Ok(AssistKind::RefactorRewrite), | ||
87 | unknown => Err(format!("Unknown AssistKind: '{}'", unknown)), | ||
88 | } | ||
89 | } | ||
59 | } | 90 | } |
60 | 91 | ||
61 | /// Unique identifier of the assist, should not be shown to the user | 92 | /// Unique identifier of the assist, should not be shown to the user |
@@ -63,6 +94,41 @@ impl AssistKind { | |||
63 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 94 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
64 | pub struct AssistId(pub &'static str, pub AssistKind); | 95 | pub struct AssistId(pub &'static str, pub AssistKind); |
65 | 96 | ||
97 | /// A way to control how many asssist to resolve during the assist resolution. | ||
98 | /// When an assist is resolved, its edits are calculated that might be costly to always do by default. | ||
99 | #[derive(Debug)] | ||
100 | pub enum AssistResolveStrategy { | ||
101 | /// No assists should be resolved. | ||
102 | None, | ||
103 | /// All assists should be resolved. | ||
104 | All, | ||
105 | /// Only a certain assist should be resolved. | ||
106 | Single(SingleResolve), | ||
107 | } | ||
108 | |||
109 | /// Hold the [`AssistId`] data of a certain assist to resolve. | ||
110 | /// The original id object cannot be used due to a `'static` lifetime | ||
111 | /// and the requirement to construct this struct dynamically during the resolve handling. | ||
112 | #[derive(Debug)] | ||
113 | pub struct SingleResolve { | ||
114 | /// The id of the assist. | ||
115 | pub assist_id: String, | ||
116 | // The kind of the assist. | ||
117 | pub assist_kind: AssistKind, | ||
118 | } | ||
119 | |||
120 | impl AssistResolveStrategy { | ||
121 | pub fn should_resolve(&self, id: &AssistId) -> bool { | ||
122 | match self { | ||
123 | AssistResolveStrategy::None => false, | ||
124 | AssistResolveStrategy::All => true, | ||
125 | AssistResolveStrategy::Single(single_resolve) => { | ||
126 | single_resolve.assist_id == id.0 && single_resolve.assist_kind == id.1 | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | |||
66 | #[derive(Clone, Debug)] | 132 | #[derive(Clone, Debug)] |
67 | pub struct GroupLabel(pub String); | 133 | pub struct GroupLabel(pub String); |
68 | 134 | ||
@@ -91,7 +157,7 @@ impl Assist { | |||
91 | pub fn get( | 157 | pub fn get( |
92 | db: &RootDatabase, | 158 | db: &RootDatabase, |
93 | config: &AssistConfig, | 159 | config: &AssistConfig, |
94 | resolve: bool, | 160 | resolve: AssistResolveStrategy, |
95 | range: FileRange, | 161 | range: FileRange, |
96 | ) -> Vec<Assist> { | 162 | ) -> Vec<Assist> { |
97 | let sema = Semantics::new(db); | 163 | let sema = Semantics::new(db); |