ide_completion/completions/postfix/
format_like.rs

1// Feature: Format String Completion
2//
3// `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`.
4//
5// The following postfix snippets are available:
6//
7// * `format` -> `format!(...)`
8// * `panic` -> `panic!(...)`
9// * `println` -> `println!(...)`
10// * `log`:
11// ** `logd` -> `log::debug!(...)`
12// ** `logt` -> `log::trace!(...)`
13// ** `logi` -> `log::info!(...)`
14// ** `logw` -> `log::warn!(...)`
15// ** `loge` -> `log::error!(...)`
16//
17// ![Format String Completion](https://user-images.githubusercontent.com/48062697/113020656-b560f500-917a-11eb-87de-02991f61beb8.gif)
18
19use ide_db::{
20    SnippetCap,
21    syntax_helpers::format_string_exprs::{Arg, parse_format_exprs, with_placeholders},
22};
23use syntax::{AstToken, ast};
24
25use crate::{
26    Completions,
27    completions::postfix::{build_postfix_snippet_builder, escape_snippet_bits},
28    context::CompletionContext,
29};
30
31/// Mapping ("postfix completion item" => "macro to use")
32static KINDS: &[(&str, &str)] = &[
33    ("format", "format!"),
34    ("panic", "panic!"),
35    ("println", "println!"),
36    ("eprintln", "eprintln!"),
37    ("logd", "log::debug!"),
38    ("logt", "log::trace!"),
39    ("logi", "log::info!"),
40    ("logw", "log::warn!"),
41    ("loge", "log::error!"),
42];
43
44pub(crate) fn add_format_like_completions(
45    acc: &mut Completions,
46    ctx: &CompletionContext<'_>,
47    dot_receiver: &ast::Expr,
48    cap: SnippetCap,
49    receiver_text: &ast::String,
50) {
51    let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) {
52        Some(it) => it,
53        None => return,
54    };
55
56    if let Ok((mut out, mut exprs)) = parse_format_exprs(receiver_text.text()) {
57        // Escape any snippet bits in the out text and any of the exprs.
58        escape_snippet_bits(&mut out);
59        for arg in &mut exprs {
60            if let Arg::Ident(text) | Arg::Expr(text) = arg {
61                escape_snippet_bits(text)
62            }
63        }
64
65        let exprs = with_placeholders(exprs);
66        for (label, macro_name) in KINDS {
67            let snippet = if exprs.is_empty() {
68                format!(r#"{macro_name}({out})"#)
69            } else {
70                format!(r#"{}({}, {})"#, macro_name, out, exprs.join(", "))
71            };
72
73            postfix_snippet(label, macro_name, &snippet).add_to(acc, ctx.db);
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_into_suggestion() {
84        let test_vector = &[
85            ("println!", "{}", r#"println!("{}", $1)"#),
86            ("eprintln!", "{}", r#"eprintln!("{}", $1)"#),
87            (
88                "log::info!",
89                "{} {ident} {} {2 + 2}",
90                r#"log::info!("{} {ident} {} {}", $1, $2, 2 + 2)"#,
91            ),
92        ];
93
94        for (kind, input, output) in test_vector {
95            let (parsed_string, exprs) = parse_format_exprs(input).unwrap();
96            let exprs = with_placeholders(exprs);
97            let snippet = format!(r#"{kind}("{parsed_string}", {})"#, exprs.join(", "));
98            assert_eq!(&snippet, output);
99        }
100    }
101
102    #[test]
103    fn test_into_suggestion_no_epxrs() {
104        let test_vector = &[
105            ("println!", "{ident}", r#"println!("{ident}")"#),
106            ("format!", "{ident:?}", r#"format!("{ident:?}")"#),
107        ];
108
109        for (kind, input, output) in test_vector {
110            let (parsed_string, _exprs) = parse_format_exprs(input).unwrap();
111            let snippet = format!(r#"{kind}("{parsed_string}")"#);
112            assert_eq!(&snippet, output);
113        }
114    }
115}