Remove EscapedName

This commit is contained in:
Ryo Yoshida 2022-08-01 15:19:49 +09:00
parent 53ec791dc6
commit 4322cf7f5b
No known key found for this signature in database
GPG key ID: E25698A930586171
11 changed files with 26 additions and 73 deletions

View file

@ -134,9 +134,9 @@ impl ModPath {
}
first_segment = false;
if escaped {
segment.escaped().fmt(f)?
} else {
segment.fmt(f)?
} else {
segment.unescaped().fmt(f)?
};
}
Ok(())

View file

@ -14,10 +14,6 @@ use syntax::{ast, SmolStr, SyntaxKind};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Name(Repr);
/// `EscapedName` will add a prefix "r#" to the wrapped `Name` when it is a raw identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EscapedName<'a>(&'a Name);
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UnescapedName<'a>(&'a Name);
@ -42,21 +38,6 @@ fn is_raw_identifier(name: &str) -> bool {
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
}
impl<'a> fmt::Display for EscapedName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 .0 {
Repr::Text(text) => {
if is_raw_identifier(text) {
write!(f, "r#{}", &text)
} else {
fmt::Display::fmt(&text, f)
}
}
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
}
}
}
impl<'a> fmt::Display for UnescapedName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 .0 {
@ -86,31 +67,6 @@ impl<'a> UnescapedName<'a> {
}
}
impl<'a> EscapedName<'a> {
pub fn is_escaped(&self) -> bool {
match &self.0 .0 {
Repr::Text(it) => is_raw_identifier(&it),
Repr::TupleField(_) => false,
}
}
/// Returns the textual representation of this name as a [`SmolStr`].
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
/// the general case.
pub fn to_smol_str(&self) -> SmolStr {
match &self.0 .0 {
Repr::Text(it) => {
if is_raw_identifier(&it) {
SmolStr::from_iter(["r#", &it])
} else {
it.clone()
}
}
Repr::TupleField(it) => SmolStr::new(&it.to_string()),
}
}
}
impl Name {
/// Note: this is private to make creating name from random string hard.
/// Hopefully, this should allow us to integrate hygiene cleaner in the
@ -181,10 +137,6 @@ impl Name {
}
}
pub fn escaped(&self) -> EscapedName<'_> {
EscapedName(self)
}
pub fn unescaped(&self) -> UnescapedName<'_> {
UnescapedName(self)
}

View file

@ -233,7 +233,8 @@ fn add_type_alias_impl(
type_alias: hir::TypeAlias,
) {
let alias_name = type_alias.name(ctx.db);
let (alias_name, escaped_name) = (alias_name.to_smol_str(), alias_name.escaped().to_smol_str());
let (alias_name, escaped_name) =
(alias_name.unescaped().to_smol_str(), alias_name.to_smol_str());
let label = format!("type {} =", alias_name);
let replacement = format!("type {} = ", escaped_name);

View file

@ -117,7 +117,7 @@ pub(crate) fn render_field(
) -> CompletionItem {
let is_deprecated = ctx.is_deprecated(field);
let name = field.name(ctx.db());
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
let mut item = CompletionItem::new(
SymbolKind::Field,
ctx.source_range(),
@ -283,8 +283,8 @@ fn render_resolution_path(
let name = local_name.to_smol_str();
let mut item = render_resolution_simple_(ctx, &local_name, import_to_add, resolution);
if local_name.escaped().is_escaped() {
item.insert_text(local_name.escaped().to_smol_str());
if local_name.is_escaped() {
item.insert_text(local_name.to_smol_str());
}
// Add `<>` for generic types
let type_path_no_ty_args = matches!(
@ -306,7 +306,7 @@ fn render_resolution_path(
item.lookup_by(name.clone())
.label(SmolStr::from_iter([&name, "<…>"]))
.trigger_call_info()
.insert_snippet(cap, format!("{}<$0>", local_name.escaped()));
.insert_snippet(cap, format!("{}<$0>", local_name));
}
}
}

View file

@ -13,7 +13,7 @@ pub(crate) fn render_const(ctx: RenderContext<'_>, const_: hir::Const) -> Option
fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
let db = ctx.db();
let name = const_.name(db)?;
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
let detail = const_.display(db).to_string();
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), name.clone());

View file

@ -52,10 +52,10 @@ fn render(
let (call, escaped_call) = match &func_kind {
FuncKind::Method(_, Some(receiver)) => (
format!("{}.{}", receiver, &name).into(),
format!("{}.{}", receiver.escaped(), name.escaped()).into(),
format!("{}.{}", receiver.unescaped(), name.unescaped()).into(),
format!("{}.{}", receiver, name).into(),
),
_ => (name.to_smol_str(), name.escaped().to_smol_str()),
_ => (name.unescaped().to_smol_str(), name.to_smol_str()),
};
let mut item = CompletionItem::new(
if func.self_param(db).is_some() {

View file

@ -46,7 +46,7 @@ fn render(
ctx.source_range()
};
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
let docs = ctx.docs(macro_);
let docs_str = docs.as_ref().map(Documentation::as_str).unwrap_or_default();
let is_fn_like = macro_.is_fn_like(completion.db);

View file

@ -31,7 +31,7 @@ pub(crate) fn render_struct_pat(
}
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
let kind = strukt.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
@ -56,7 +56,7 @@ pub(crate) fn render_variant_pat(
Some(path) => (path.to_string().into(), path.escaped().to_string().into()),
None => {
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
(name.to_smol_str(), name.escaped().to_smol_str())
(name.unescaped().to_smol_str(), name.to_smol_str())
}
};
@ -146,7 +146,7 @@ fn render_record_as_pat(
format!(
"{name} {{ {}{} }}",
fields.enumerate().format_with(", ", |(idx, field), f| {
f(&format_args!("{}${}", field.name(db).escaped(), idx + 1))
f(&format_args!("{}${}", field.name(db), idx + 1))
}),
if fields_omitted { ", .." } else { "" },
name = name
@ -155,7 +155,7 @@ fn render_record_as_pat(
None => {
format!(
"{name} {{ {}{} }}",
fields.map(|field| field.name(db).escaped().to_smol_str()).format(", "),
fields.map(|field| field.name(db).to_smol_str()).format(", "),
if fields_omitted { ", .." } else { "" },
name = name
)

View file

@ -32,11 +32,11 @@ fn render(
let name = type_alias.name(db);
let (name, escaped_name) = if with_eq {
(
SmolStr::from_iter([&name.unescaped().to_smol_str(), " = "]),
SmolStr::from_iter([&name.to_smol_str(), " = "]),
SmolStr::from_iter([&name.escaped().to_smol_str(), " = "]),
)
} else {
(name.to_smol_str(), name.escaped().to_smol_str())
(name.unescaped().to_smol_str(), name.to_smol_str())
};
let detail = type_alias.display(db).to_string();

View file

@ -22,7 +22,7 @@ pub(crate) fn render_union_literal(
let (qualified_name, escaped_qualified_name) = match path {
Some(p) => (p.to_string(), p.escaped().to_string()),
None => (name.to_string(), name.escaped().to_string()),
None => (name.unescaped().to_string(), name.to_string()),
};
let mut item = CompletionItem::new(
@ -42,15 +42,15 @@ pub(crate) fn render_union_literal(
format!(
"{} {{ ${{1|{}|}}: ${{2:()}} }}$0",
escaped_qualified_name,
fields.iter().map(|field| field.name(ctx.db()).escaped().to_smol_str()).format(",")
fields.iter().map(|field| field.name(ctx.db()).to_smol_str()).format(",")
)
} else {
format!(
"{} {{ {} }}",
escaped_qualified_name,
fields.iter().format_with(", ", |field, f| {
f(&format_args!("{}: ()", field.name(ctx.db()).escaped()))
})
fields
.iter()
.format_with(", ", |field, f| { f(&format_args!("{}: ()", field.name(ctx.db()))) })
)
};

View file

@ -24,9 +24,9 @@ pub(crate) fn render_record_lit(
) -> RenderedLiteral {
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
if snippet_cap.is_some() {
f(&format_args!("{}: ${{{}:()}}", field.name(db).escaped(), idx + 1))
f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1))
} else {
f(&format_args!("{}: ()", field.name(db).escaped()))
f(&format_args!("{}: ()", field.name(db)))
}
});