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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
//! Tools to work with expressions present in format string literals for the `format_args!` family of macros.
//! Primarily meant for assists and completions.
/// Enum for representing extracted format string args.
/// Can either be extracted expressions (which includes identifiers),
/// or placeholders `{}`.
#[derive(Debug, PartialEq, Eq)]
pub enum Arg {
Placeholder,
Ident(String),
Expr(String),
}
/// Add placeholders like `$1` and `$2` in place of [`Arg::Placeholder`],
/// and unwraps the [`Arg::Ident`] and [`Arg::Expr`] enums.
/// ```rust
/// # use ide_db::syntax_helpers::format_string_exprs::*;
/// assert_eq!(with_placeholders(vec![Arg::Ident("ident".to_owned()), Arg::Placeholder, Arg::Expr("expr + 2".to_owned())]), vec!["ident".to_owned(), "$1".to_owned(), "expr + 2".to_owned()])
/// ```
pub fn with_placeholders(args: Vec<Arg>) -> Vec<String> {
let mut placeholder_id = 1;
args.into_iter()
.map(move |a| match a {
Arg::Expr(s) | Arg::Ident(s) => s,
Arg::Placeholder => {
let s = format!("${placeholder_id}");
placeholder_id += 1;
s
}
})
.collect()
}
// FIXME Remove this, we have this information in the HIR now
/// Parser for a format-like string. It is more allowing in terms of string contents,
/// as we expect variable placeholders to be filled with expressions.
///
/// Splits a format string that may contain expressions
/// like
/// ```rust
/// # use ide_db::syntax_helpers::format_string_exprs::*;
/// assert_eq!(parse_format_exprs("{ident} {} {expr + 42} ").unwrap(), ("{ident} {} {} ".to_owned(), vec![Arg::Placeholder, Arg::Expr("expr + 42".to_owned())]));
/// ```
pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
#[derive(Debug, Clone, Copy, PartialEq)]
enum State {
NotArg,
MaybeArg,
Expr,
Ident,
MaybeIncorrect,
FormatOpts,
}
let mut state = State::NotArg;
let mut current_expr = String::new();
let mut extracted_expressions = Vec::new();
let mut output = String::new();
// Count of open braces inside of an expression.
// We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g.
// "{MyStruct { val_a: 0, val_b: 1 }}".
let mut inexpr_open_count = 0;
let mut chars = input.chars().peekable();
while let Some(chr) = chars.next() {
match (state, chr) {
(State::NotArg, '{') => {
output.push(chr);
state = State::MaybeArg;
}
(State::NotArg, '}') => {
output.push(chr);
state = State::MaybeIncorrect;
}
(State::NotArg, _) => {
output.push(chr);
}
(State::MaybeIncorrect, '}') => {
// It's okay, we met "}}".
output.push(chr);
state = State::NotArg;
}
(State::MaybeIncorrect, _) => {
// Error in the string.
return Err(());
}
// Escaped braces `{{`
(State::MaybeArg, '{') => {
output.push(chr);
state = State::NotArg;
}
(State::MaybeArg, '}') => {
// This is an empty sequence '{}'.
output.push(chr);
extracted_expressions.push(Arg::Placeholder);
state = State::NotArg;
}
(State::MaybeArg, ':') => {
output.push(chr);
extracted_expressions.push(Arg::Placeholder);
state = State::FormatOpts;
}
(State::MaybeArg, _) => {
current_expr.push(chr);
// While Rust uses the unicode sets of XID_start and XID_continue for Identifiers
// this is probably the best we can do to avoid a false positive
if chr.is_alphabetic() || chr == '_' {
state = State::Ident;
} else {
state = State::Expr;
}
}
(State::Ident | State::Expr, ':') if matches!(chars.peek(), Some(':')) => {
// path separator
state = State::Expr;
current_expr.push_str("::");
chars.next();
}
(State::Ident | State::Expr, ':' | '}') => {
if inexpr_open_count == 0 {
let trimmed = current_expr.trim();
// if the expression consists of a single number, like "0" or "12", it can refer to
// format args in the order they are specified.
// see: https://doc.rust-lang.org/std/fmt/#positional-parameters
if trimmed.chars().fold(true, |only_num, c| c.is_ascii_digit() && only_num) {
output.push_str(trimmed);
} else if matches!(state, State::Expr) {
extracted_expressions.push(Arg::Expr(trimmed.into()));
} else if matches!(state, State::Ident) {
output.push_str(trimmed);
}
output.push(chr);
current_expr.clear();
state = if chr == ':' {
State::FormatOpts
} else if chr == '}' {
State::NotArg
} else {
unreachable!()
};
} else if chr == '}' {
// We're closing one brace met before inside of the expression.
current_expr.push(chr);
inexpr_open_count -= 1;
} else if chr == ':' {
// We're inside of braced expression, assume that it's a struct field name/value delimiter.
current_expr.push(chr);
}
}
(State::Ident | State::Expr, '{') => {
state = State::Expr;
current_expr.push(chr);
inexpr_open_count += 1;
}
(State::Ident | State::Expr, _) => {
if !(chr.is_alphanumeric() || chr == '_' || chr == '#') {
state = State::Expr;
}
current_expr.push(chr);
}
(State::FormatOpts, '}') => {
output.push(chr);
state = State::NotArg;
}
(State::FormatOpts, _) => {
output.push(chr);
}
}
}
if state != State::NotArg {
return Err(());
}
Ok((output, extracted_expressions))
}
#[cfg(test)]
mod tests {
use super::*;
use expect_test::{expect, Expect};
fn check(input: &str, expect: &Expect) {
let (output, exprs) = parse_format_exprs(input).unwrap_or(("-".to_owned(), vec![]));
let outcome_repr = if !exprs.is_empty() {
format!("{output}; {}", with_placeholders(exprs).join(", "))
} else {
output
};
expect.assert_eq(&outcome_repr);
}
#[test]
fn format_str_parser() {
let test_vector = &[
("no expressions", expect![["no expressions"]]),
(r"no expressions with \$0$1", expect![r"no expressions with \$0$1"]),
("{expr} is {2 + 2}", expect![["{expr} is {}; 2 + 2"]]),
("{expr:?}", expect![["{expr:?}"]]),
("{expr:1$}", expect![[r"{expr:1$}"]]),
("{:1$}", expect![[r"{:1$}; $1"]]),
("{:>padding$}", expect![[r"{:>padding$}; $1"]]),
("{}, {}, {0}", expect![[r"{}, {}, {0}; $1, $2"]]),
("{}, {}, {0:b}", expect![[r"{}, {}, {0:b}; $1, $2"]]),
("{$0}", expect![[r"{}; $0"]]),
("{malformed", expect![["-"]]),
("malformed}", expect![["-"]]),
("{{correct", expect![["{{correct"]]),
("correct}}", expect![["correct}}"]]),
("{correct}}}", expect![["{correct}}}"]]),
("{correct}}}}}", expect![["{correct}}}}}"]]),
("{incorrect}}", expect![["-"]]),
("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]),
("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]),
(
"{SomeStruct { val_a: 0, val_b: 1 }}",
expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]],
),
("{expr:?} is {2.32f64:.5}", expect![["{expr:?} is {:.5}; 2.32f64"]]),
(
"{SomeStruct { val_a: 0, val_b: 1 }:?}",
expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]],
),
("{ 2 + 2 }", expect![["{}; 2 + 2"]]),
("{strsim::jaro_winkle(a)}", expect![["{}; strsim::jaro_winkle(a)"]]),
("{foo::bar::baz()}", expect![["{}; foo::bar::baz()"]]),
("{foo::bar():?}", expect![["{:?}; foo::bar()"]]),
];
for (input, output) in test_vector {
check(input, output)
}
}
#[test]
fn arg_type() {
assert_eq!(
parse_format_exprs("{_ident} {r#raw_ident} {expr.obj} {name {thing: 42} } {}")
.unwrap()
.1,
vec![
Arg::Expr("expr.obj".to_owned()),
Arg::Expr("name {thing: 42}".to_owned()),
Arg::Placeholder
]
);
}
}