aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
blob: 7101d9aaff3a81fcbbc55046f0b6518d50ef0157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{AssistContext, AssistId, Assists};
use ra_syntax::{ast, ast::TypeParamsOwner, AstNode, SyntaxKind};
use std::collections::HashSet;

// Assist: change_lifetime_anon_to_named
//
// Change an anonymous lifetime to a named lifetime.
//
// ```
// impl Cursor<'_<|>> {
//     fn node(self) -> &SyntaxNode {
//         match self {
//             Cursor::Replace(node) | Cursor::Before(node) => node,
//         }
//     }
// }
// ```
// ->
// ```
// impl<'a> Cursor<'a> {
//     fn node(self) -> &SyntaxNode {
//         match self {
//             Cursor::Replace(node) | Cursor::Before(node) => node,
//         }
//     }
// }
// ```
// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let lifetime_token = ctx.find_token_at_offset(SyntaxKind::LIFETIME)?;
    let lifetime_arg = ast::LifetimeArg::cast(lifetime_token.parent())?;
    if lifetime_arg.syntax().text() != "'_" {
        return None;
    }
    let next_token = lifetime_token.next_token()?;
    if next_token.kind() != SyntaxKind::R_ANGLE {
        // only allow naming the last anonymous lifetime
        return None;
    }
    match lifetime_arg.syntax().ancestors().find_map(ast::ImplDef::cast) {
        Some(impl_def) => {
            // get the `impl` keyword so we know where to add the lifetime argument
            let impl_kw = impl_def.syntax().first_child_or_token()?.into_token()?;
            if impl_kw.kind() != SyntaxKind::IMPL_KW {
                return None;
            }
            let new_lifetime_param = match impl_def.type_param_list() {
                Some(type_params) => {
                    let used_lifetime_params: HashSet<_> = type_params
                        .lifetime_params()
                        .map(|p| {
                            let mut param_name = p.syntax().text().to_string();
                            param_name.remove(0);
                            param_name
                        })
                        .collect();
                    "abcdefghijklmnopqrstuvwxyz"
                        .chars()
                        .find(|c| !used_lifetime_params.contains(&c.to_string()))?
                }
                None => 'a',
            };
            acc.add(
                AssistId("change_lifetime_anon_to_named"),
                "Give anonymous lifetime a name",
                lifetime_arg.syntax().text_range(),
                |builder| {
                    match impl_def.type_param_list() {
                        Some(type_params) => {
                            builder.insert(
                                (u32::from(type_params.syntax().text_range().end()) - 1).into(),
                                format!(", '{}", new_lifetime_param),
                            );
                        }
                        None => {
                            builder.insert(
                                impl_kw.text_range().end(),
                                format!("<'{}>", new_lifetime_param),
                            );
                        }
                    }
                    builder.replace(
                        lifetime_arg.syntax().text_range(),
                        format!("'{}", new_lifetime_param),
                    );
                },
            )
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::{check_assist, check_assist_not_applicable};

    #[test]
    fn test_example_case() {
        check_assist(
            change_lifetime_anon_to_named,
            r#"impl Cursor<'_<|>> {
                fn node(self) -> &SyntaxNode {
                    match self {
                        Cursor::Replace(node) | Cursor::Before(node) => node,
                    }
                }
            }"#,
            r#"impl<'a> Cursor<'a> {
                fn node(self) -> &SyntaxNode {
                    match self {
                        Cursor::Replace(node) | Cursor::Before(node) => node,
                    }
                }
            }"#,
        );
    }

    #[test]
    fn test_example_case_simplified() {
        check_assist(
            change_lifetime_anon_to_named,
            r#"impl Cursor<'_<|>> {"#,
            r#"impl<'a> Cursor<'a> {"#,
        );
    }

    #[test]
    fn test_not_applicable() {
        check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'_><|> {"#);
        check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<|><'_> {"#);
        check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'a<|>> {"#);
    }

    #[test]
    fn test_with_type_parameter() {
        check_assist(
            change_lifetime_anon_to_named,
            r#"impl<T> Cursor<T, '_<|>>"#,
            r#"impl<T, 'a> Cursor<T, 'a>"#,
        );
    }

    #[test]
    fn test_with_existing_lifetime_name_conflict() {
        check_assist(
            change_lifetime_anon_to_named,
            r#"impl<'a, 'b> Cursor<'a, 'b, '_<|>>"#,
            r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
        );
    }
}