mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Accept more than just the standard rust literal suffixes in *Number::suffix
This commit is contained in:
parent
fc0354b280
commit
931493e949
1 changed files with 18 additions and 22 deletions
|
@ -534,11 +534,6 @@ impl HasFormatSpecifier for ast::String {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::IntNumber {
|
impl ast::IntNumber {
|
||||||
const SUFFIXES: &'static [&'static str] = &[
|
|
||||||
"u8", "u16", "u32", "u64", "u128", "usize", // Unsigned.
|
|
||||||
"i8", "i16", "i32", "i64", "i128", "isize", // Signed.
|
|
||||||
];
|
|
||||||
|
|
||||||
pub fn radix(&self) -> Radix {
|
pub fn radix(&self) -> Radix {
|
||||||
match self.text().get(..2).unwrap_or_default() {
|
match self.text().get(..2).unwrap_or_default() {
|
||||||
"0b" => Radix::Binary,
|
"0b" => Radix::Binary,
|
||||||
|
@ -571,29 +566,30 @@ impl ast::IntNumber {
|
||||||
|
|
||||||
pub fn suffix(&self) -> Option<&str> {
|
pub fn suffix(&self) -> Option<&str> {
|
||||||
let text = self.text();
|
let text = self.text();
|
||||||
// FIXME: don't check a fixed set of suffixes, `1_0_1_l_o_l` is valid
|
let radix = self.radix();
|
||||||
// syntax, suffix is `l_o_l`.
|
let mut indices = text.char_indices();
|
||||||
ast::IntNumber::SUFFIXES.iter().chain(ast::FloatNumber::SUFFIXES.iter()).find_map(
|
if radix != Radix::Decimal {
|
||||||
|suffix| {
|
indices.next()?;
|
||||||
if text.ends_with(suffix) {
|
indices.next()?;
|
||||||
return Some(&text[text.len() - suffix.len()..]);
|
}
|
||||||
}
|
let is_suffix_start: fn(&(usize, char)) -> bool = match radix {
|
||||||
None
|
Radix::Hexadecimal => |(_, c)| matches!(c, 'g'..='z' | 'G'..='Z'),
|
||||||
},
|
_ => |(_, c)| c.is_ascii_alphabetic(),
|
||||||
)
|
};
|
||||||
|
let (suffix_start, _) = indices.find(is_suffix_start)?;
|
||||||
|
Some(&text[suffix_start..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::FloatNumber {
|
impl ast::FloatNumber {
|
||||||
const SUFFIXES: &'static [&'static str] = &["f32", "f64"];
|
|
||||||
pub fn suffix(&self) -> Option<&str> {
|
pub fn suffix(&self) -> Option<&str> {
|
||||||
let text = self.text();
|
let text = self.text();
|
||||||
ast::FloatNumber::SUFFIXES.iter().find_map(|suffix| {
|
let mut indices = text.char_indices();
|
||||||
if text.ends_with(suffix) {
|
let (mut suffix_start, c) = indices.by_ref().find(|(_, c)| c.is_ascii_alphabetic())?;
|
||||||
return Some(&text[text.len() - suffix.len()..]);
|
if c == 'e' || c == 'E' {
|
||||||
}
|
suffix_start = indices.find(|(_, c)| c.is_ascii_alphabetic())?.0;
|
||||||
None
|
}
|
||||||
})
|
Some(&text[suffix_start..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue