Skip to content

Commit 4322cf7

Browse files
committed
Remove EscapedName
1 parent 53ec791 commit 4322cf7

File tree

11 files changed

+26
-73
lines changed

11 files changed

+26
-73
lines changed

crates/hir-expand/src/mod_path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ impl ModPath {
134134
}
135135
first_segment = false;
136136
if escaped {
137-
segment.escaped().fmt(f)?
138-
} else {
139137
segment.fmt(f)?
138+
} else {
139+
segment.unescaped().fmt(f)?
140140
};
141141
}
142142
Ok(())

crates/hir-expand/src/name.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ use syntax::{ast, SmolStr, SyntaxKind};
1414
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1515
pub struct Name(Repr);
1616

17-
/// `EscapedName` will add a prefix "r#" to the wrapped `Name` when it is a raw identifier
18-
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
19-
pub struct EscapedName<'a>(&'a Name);
20-
2117
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
2218
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2319
pub struct UnescapedName<'a>(&'a Name);
@@ -42,21 +38,6 @@ fn is_raw_identifier(name: &str) -> bool {
4238
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
4339
}
4440

45-
impl<'a> fmt::Display for EscapedName<'a> {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
match &self.0 .0 {
48-
Repr::Text(text) => {
49-
if is_raw_identifier(text) {
50-
write!(f, "r#{}", &text)
51-
} else {
52-
fmt::Display::fmt(&text, f)
53-
}
54-
}
55-
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
56-
}
57-
}
58-
}
59-
6041
impl<'a> fmt::Display for UnescapedName<'a> {
6142
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6243
match &self.0 .0 {
@@ -86,31 +67,6 @@ impl<'a> UnescapedName<'a> {
8667
}
8768
}
8869

89-
impl<'a> EscapedName<'a> {
90-
pub fn is_escaped(&self) -> bool {
91-
match &self.0 .0 {
92-
Repr::Text(it) => is_raw_identifier(&it),
93-
Repr::TupleField(_) => false,
94-
}
95-
}
96-
97-
/// Returns the textual representation of this name as a [`SmolStr`].
98-
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
99-
/// the general case.
100-
pub fn to_smol_str(&self) -> SmolStr {
101-
match &self.0 .0 {
102-
Repr::Text(it) => {
103-
if is_raw_identifier(&it) {
104-
SmolStr::from_iter(["r#", &it])
105-
} else {
106-
it.clone()
107-
}
108-
}
109-
Repr::TupleField(it) => SmolStr::new(&it.to_string()),
110-
}
111-
}
112-
}
113-
11470
impl Name {
11571
/// Note: this is private to make creating name from random string hard.
11672
/// Hopefully, this should allow us to integrate hygiene cleaner in the
@@ -181,10 +137,6 @@ impl Name {
181137
}
182138
}
183139

184-
pub fn escaped(&self) -> EscapedName<'_> {
185-
EscapedName(self)
186-
}
187-
188140
pub fn unescaped(&self) -> UnescapedName<'_> {
189141
UnescapedName(self)
190142
}

crates/ide-completion/src/completions/item_list/trait_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ fn add_type_alias_impl(
233233
type_alias: hir::TypeAlias,
234234
) {
235235
let alias_name = type_alias.name(ctx.db);
236-
let (alias_name, escaped_name) = (alias_name.to_smol_str(), alias_name.escaped().to_smol_str());
236+
let (alias_name, escaped_name) =
237+
(alias_name.unescaped().to_smol_str(), alias_name.to_smol_str());
237238

238239
let label = format!("type {} =", alias_name);
239240
let replacement = format!("type {} = ", escaped_name);

crates/ide-completion/src/render.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(crate) fn render_field(
117117
) -> CompletionItem {
118118
let is_deprecated = ctx.is_deprecated(field);
119119
let name = field.name(ctx.db());
120-
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
120+
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
121121
let mut item = CompletionItem::new(
122122
SymbolKind::Field,
123123
ctx.source_range(),
@@ -283,8 +283,8 @@ fn render_resolution_path(
283283

284284
let name = local_name.to_smol_str();
285285
let mut item = render_resolution_simple_(ctx, &local_name, import_to_add, resolution);
286-
if local_name.escaped().is_escaped() {
287-
item.insert_text(local_name.escaped().to_smol_str());
286+
if local_name.is_escaped() {
287+
item.insert_text(local_name.to_smol_str());
288288
}
289289
// Add `<>` for generic types
290290
let type_path_no_ty_args = matches!(
@@ -306,7 +306,7 @@ fn render_resolution_path(
306306
item.lookup_by(name.clone())
307307
.label(SmolStr::from_iter([&name, "<…>"]))
308308
.trigger_call_info()
309-
.insert_snippet(cap, format!("{}<$0>", local_name.escaped()));
309+
.insert_snippet(cap, format!("{}<$0>", local_name));
310310
}
311311
}
312312
}

crates/ide-completion/src/render/const_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn render_const(ctx: RenderContext<'_>, const_: hir::Const) -> Option
1313
fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem> {
1414
let db = ctx.db();
1515
let name = const_.name(db)?;
16-
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
16+
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
1717
let detail = const_.display(db).to_string();
1818

1919
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), name.clone());

crates/ide-completion/src/render/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ fn render(
5252

5353
let (call, escaped_call) = match &func_kind {
5454
FuncKind::Method(_, Some(receiver)) => (
55-
format!("{}.{}", receiver, &name).into(),
56-
format!("{}.{}", receiver.escaped(), name.escaped()).into(),
55+
format!("{}.{}", receiver.unescaped(), name.unescaped()).into(),
56+
format!("{}.{}", receiver, name).into(),
5757
),
58-
_ => (name.to_smol_str(), name.escaped().to_smol_str()),
58+
_ => (name.unescaped().to_smol_str(), name.to_smol_str()),
5959
};
6060
let mut item = CompletionItem::new(
6161
if func.self_param(db).is_some() {

crates/ide-completion/src/render/macro_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn render(
4646
ctx.source_range()
4747
};
4848

49-
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
49+
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
5050
let docs = ctx.docs(macro_);
5151
let docs_str = docs.as_ref().map(Documentation::as_str).unwrap_or_default();
5252
let is_fn_like = macro_.is_fn_like(completion.db);

crates/ide-completion/src/render/pattern.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn render_struct_pat(
3131
}
3232

3333
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
34-
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
34+
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
3535
let kind = strukt.kind(ctx.db());
3636
let label = format_literal_label(name.as_str(), kind);
3737
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
@@ -56,7 +56,7 @@ pub(crate) fn render_variant_pat(
5656
Some(path) => (path.to_string().into(), path.escaped().to_string().into()),
5757
None => {
5858
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
59-
(name.to_smol_str(), name.escaped().to_smol_str())
59+
(name.unescaped().to_smol_str(), name.to_smol_str())
6060
}
6161
};
6262

@@ -146,7 +146,7 @@ fn render_record_as_pat(
146146
format!(
147147
"{name} {{ {}{} }}",
148148
fields.enumerate().format_with(", ", |(idx, field), f| {
149-
f(&format_args!("{}${}", field.name(db).escaped(), idx + 1))
149+
f(&format_args!("{}${}", field.name(db), idx + 1))
150150
}),
151151
if fields_omitted { ", .." } else { "" },
152152
name = name
@@ -155,7 +155,7 @@ fn render_record_as_pat(
155155
None => {
156156
format!(
157157
"{name} {{ {}{} }}",
158-
fields.map(|field| field.name(db).escaped().to_smol_str()).format(", "),
158+
fields.map(|field| field.name(db).to_smol_str()).format(", "),
159159
if fields_omitted { ", .." } else { "" },
160160
name = name
161161
)

crates/ide-completion/src/render/type_alias.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ fn render(
3232
let name = type_alias.name(db);
3333
let (name, escaped_name) = if with_eq {
3434
(
35+
SmolStr::from_iter([&name.unescaped().to_smol_str(), " = "]),
3536
SmolStr::from_iter([&name.to_smol_str(), " = "]),
36-
SmolStr::from_iter([&name.escaped().to_smol_str(), " = "]),
3737
)
3838
} else {
39-
(name.to_smol_str(), name.escaped().to_smol_str())
39+
(name.unescaped().to_smol_str(), name.to_smol_str())
4040
};
4141
let detail = type_alias.display(db).to_string();
4242

crates/ide-completion/src/render/union_literal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn render_union_literal(
2222

2323
let (qualified_name, escaped_qualified_name) = match path {
2424
Some(p) => (p.to_string(), p.escaped().to_string()),
25-
None => (name.to_string(), name.escaped().to_string()),
25+
None => (name.unescaped().to_string(), name.to_string()),
2626
};
2727

2828
let mut item = CompletionItem::new(
@@ -42,15 +42,15 @@ pub(crate) fn render_union_literal(
4242
format!(
4343
"{} {{ ${{1|{}|}}: ${{2:()}} }}$0",
4444
escaped_qualified_name,
45-
fields.iter().map(|field| field.name(ctx.db()).escaped().to_smol_str()).format(",")
45+
fields.iter().map(|field| field.name(ctx.db()).to_smol_str()).format(",")
4646
)
4747
} else {
4848
format!(
4949
"{} {{ {} }}",
5050
escaped_qualified_name,
51-
fields.iter().format_with(", ", |field, f| {
52-
f(&format_args!("{}: ()", field.name(ctx.db()).escaped()))
53-
})
51+
fields
52+
.iter()
53+
.format_with(", ", |field, f| { f(&format_args!("{}: ()", field.name(ctx.db()))) })
5454
)
5555
};
5656

crates/ide-completion/src/render/variant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ pub(crate) fn render_record_lit(
2424
) -> RenderedLiteral {
2525
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
2626
if snippet_cap.is_some() {
27-
f(&format_args!("{}: ${{{}:()}}", field.name(db).escaped(), idx + 1))
27+
f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1))
2828
} else {
29-
f(&format_args!("{}: ()", field.name(db).escaped()))
29+
f(&format_args!("{}: ()", field.name(db)))
3030
}
3131
});
3232

0 commit comments

Comments
 (0)