fix: replace unescape fn with the one in ra-ap-rustc_lexer

This commit is contained in:
roife 2024-04-19 16:42:07 +08:00
parent a543516ea4
commit 3e232bb78a
2 changed files with 31 additions and 34 deletions

View file

@ -8,6 +8,7 @@ use intern::Interned;
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct}; use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use span::{Span, SyntaxContextId}; use span::{Span, SyntaxContextId};
use syntax::unescape;
use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode}; use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
use triomphe::ThinArc; use triomphe::ThinArc;
@ -54,8 +55,7 @@ impl RawAttrs {
Attr { Attr {
id, id,
input: Some(Interned::new(AttrInput::Literal(tt::Literal { input: Some(Interned::new(AttrInput::Literal(tt::Literal {
// FIXME: Escape quotes from comment content text: SmolStr::new(format_smolstr!("\"{}\"", Self::escape_chars(doc))),
text: SmolStr::new(format_smolstr!("\"{doc}\"",)),
span, span,
}))), }))),
path: Interned::new(ModPath::from(crate::name!(doc))), path: Interned::new(ModPath::from(crate::name!(doc))),
@ -74,6 +74,10 @@ impl RawAttrs {
RawAttrs { entries } RawAttrs { entries }
} }
fn escape_chars(s: &str) -> String {
s.replace('\\', r#"\\"#).replace('"', r#"\""#)
}
pub fn from_attrs_owner( pub fn from_attrs_owner(
db: &dyn ExpandDatabase, db: &dyn ExpandDatabase,
owner: InFile<&dyn ast::HasAttrs>, owner: InFile<&dyn ast::HasAttrs>,
@ -303,9 +307,7 @@ impl Attr {
Some(it) => { Some(it) => {
it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed) it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed)
} }
None => { None => it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape),
it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape).map(Cow::Owned)
}
}, },
_ => None, _ => None,
} }
@ -360,37 +362,31 @@ impl Attr {
} }
} }
fn unescape(s: &str) -> Option<String> { fn unescape(s: &str) -> Option<Cow<'_, str>> {
let mut res = String::with_capacity(s.len()); let mut buf = String::new();
let mut chars = s.chars(); let mut prev_end = 0;
let mut has_error = false;
unescape::unescape_unicode(s, unescape::Mode::Str, &mut |char_range, unescaped_char| match (
unescaped_char,
buf.capacity() == 0,
) {
(Ok(c), false) => buf.push(c),
(Ok(_), true) if char_range.len() == 1 && char_range.start == prev_end => {
prev_end = char_range.end
}
(Ok(c), true) => {
buf.reserve_exact(s.len());
buf.push_str(&s[..prev_end]);
buf.push(c);
}
(Err(_), _) => has_error = true,
});
while let Some(c) = chars.next() { match (has_error, buf.capacity() == 0) {
if c == '\\' { (true, _) => None,
match chars.next()? { (false, false) => Some(Cow::Owned(buf)),
'n' => res.push('\n'), (false, true) => Some(Cow::Borrowed(s)),
'r' => res.push('\r'),
't' => res.push('\t'),
'\\' => res.push('\\'),
'\'' => res.push('\''),
'"' => res.push('"'),
'0' => res.push('\0'),
'x' => {
let hex = chars.by_ref().take(2).collect::<String>();
let c = u8::from_str_radix(&hex, 16).ok()?;
res.push(c as char);
} }
'u' => {
let hex = chars.by_ref().take(4).collect::<String>();
let c = u32::from_str_radix(&hex, 16).ok()?;
res.push(char::from_u32(c)?);
}
_ => return None,
}
} else {
res.push(c);
}
}
Some(res)
} }
pub fn collect_attrs( pub fn collect_attrs(

View file

@ -65,6 +65,7 @@ pub use rowan::{
api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize,
TokenAtOffset, WalkEvent, TokenAtOffset, WalkEvent,
}; };
pub use rustc_lexer::unescape;
pub use smol_str::{format_smolstr, SmolStr}; pub use smol_str::{format_smolstr, SmolStr};
/// `Parse` is the result of the parsing: a syntax tree and a collection of /// `Parse` is the result of the parsing: a syntax tree and a collection of