1 //! This module defines the `Assist` data structure. The actual assist live in
2 //! the `ide_assists` downstream crate. We want to define the data structures in
3 //! this low-level crate though, because `ide_diagnostics` also need them
4 //! (fixits for diagnostics and assists are the same thing under the hood). We
5 //! want to compile `ide_assists` and `ide_diagnostics` in parallel though, so
6 //! we pull the common definitions upstream, to this crate.
10 use syntax::TextRange;
12 use crate::{label::Label, source_change::SourceChange};
14 #[derive(Debug, Clone)]
17 /// Short description of the assist, as shown in the UI.
19 pub group: Option<GroupLabel>,
20 /// Target ranges are used to sort assists: the smaller the target range,
21 /// the more specific assist is, and so it should be sorted first.
22 pub target: TextRange,
23 /// Computing source change sometimes is much more costly then computing the
24 /// other fields. Additionally, the actual change is not required to show
25 /// the lightbulb UI, it only is needed when the user tries to apply an
26 /// assist. So, we compute it lazily: the API allow requesting assists with
27 /// or without source change. We could (and in fact, used to) distinguish
28 /// between resolved and unresolved assists at the type level, but this is
29 /// cumbersome, especially if you want to embed an assist into another data
30 /// structure, such as a diagnostic.
31 pub source_change: Option<SourceChange>,
32 pub trigger_signature_help: bool,
35 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
37 // FIXME: does the None variant make sense? Probably not.
49 pub fn contains(self, other: AssistKind) -> bool {
55 AssistKind::None | AssistKind::Generate => true,
56 AssistKind::Refactor => matches!(
58 AssistKind::RefactorExtract
59 | AssistKind::RefactorInline
60 | AssistKind::RefactorRewrite
66 pub fn name(&self) -> &str {
68 AssistKind::None => "None",
69 AssistKind::QuickFix => "QuickFix",
70 AssistKind::Generate => "Generate",
71 AssistKind::Refactor => "Refactor",
72 AssistKind::RefactorExtract => "RefactorExtract",
73 AssistKind::RefactorInline => "RefactorInline",
74 AssistKind::RefactorRewrite => "RefactorRewrite",
79 impl FromStr for AssistKind {
82 fn from_str(s: &str) -> Result<Self, Self::Err> {
84 "None" => Ok(AssistKind::None),
85 "QuickFix" => Ok(AssistKind::QuickFix),
86 "Generate" => Ok(AssistKind::Generate),
87 "Refactor" => Ok(AssistKind::Refactor),
88 "RefactorExtract" => Ok(AssistKind::RefactorExtract),
89 "RefactorInline" => Ok(AssistKind::RefactorInline),
90 "RefactorRewrite" => Ok(AssistKind::RefactorRewrite),
91 unknown => Err(format!("Unknown AssistKind: '{}'", unknown)),
96 /// Unique identifier of the assist, should not be shown to the user
98 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
99 pub struct AssistId(pub &'static str, pub AssistKind);
101 /// A way to control how many asssist to resolve during the assist resolution.
102 /// When an assist is resolved, its edits are calculated that might be costly to always do by default.
104 pub enum AssistResolveStrategy {
105 /// No assists should be resolved.
107 /// All assists should be resolved.
109 /// Only a certain assist should be resolved.
110 Single(SingleResolve),
113 /// Hold the [`AssistId`] data of a certain assist to resolve.
114 /// The original id object cannot be used due to a `'static` lifetime
115 /// and the requirement to construct this struct dynamically during the resolve handling.
117 pub struct SingleResolve {
118 /// The id of the assist.
119 pub assist_id: String,
120 // The kind of the assist.
121 pub assist_kind: AssistKind,
124 impl AssistResolveStrategy {
125 pub fn should_resolve(&self, id: &AssistId) -> bool {
127 AssistResolveStrategy::None => false,
128 AssistResolveStrategy::All => true,
129 AssistResolveStrategy::Single(single_resolve) => {
130 single_resolve.assist_id == id.0 && single_resolve.assist_kind == id.1
136 #[derive(Clone, Debug)]
137 pub struct GroupLabel(pub String);