mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
internal: Replace Display impl for Name
This commit is contained in:
parent
2f840c2236
commit
c7ef6c25b7
108 changed files with 1045 additions and 656 deletions
|
@ -1,7 +1,7 @@
|
|||
//! A lowering for `use`-paths (more generally, paths without angle-bracketed segments).
|
||||
|
||||
use std::{
|
||||
fmt::{self, Display},
|
||||
fmt::{self, Display as _},
|
||||
iter,
|
||||
};
|
||||
|
||||
|
@ -24,6 +24,12 @@ pub struct ModPath {
|
|||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct UnescapedModPath<'a>(&'a ModPath);
|
||||
|
||||
impl<'a> UnescapedModPath<'a> {
|
||||
pub fn display(&'a self, db: &'a dyn crate::db::ExpandDatabase) -> impl fmt::Display + 'a {
|
||||
UnescapedDisplay { db, path: self }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum PathKind {
|
||||
Plain,
|
||||
|
@ -110,52 +116,30 @@ impl ModPath {
|
|||
UnescapedModPath(self)
|
||||
}
|
||||
|
||||
fn _fmt(&self, f: &mut fmt::Formatter<'_>, escaped: bool) -> fmt::Result {
|
||||
let mut first_segment = true;
|
||||
let mut add_segment = |s| -> fmt::Result {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
f.write_str(s)?;
|
||||
Ok(())
|
||||
};
|
||||
match self.kind {
|
||||
PathKind::Plain => {}
|
||||
PathKind::Super(0) => add_segment("self")?,
|
||||
PathKind::Super(n) => {
|
||||
for _ in 0..n {
|
||||
add_segment("super")?;
|
||||
}
|
||||
}
|
||||
PathKind::Crate => add_segment("crate")?,
|
||||
PathKind::Abs => add_segment("")?,
|
||||
PathKind::DollarCrate(_) => add_segment("$crate")?,
|
||||
}
|
||||
for segment in &self.segments {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
if escaped {
|
||||
segment.fmt(f)?
|
||||
} else {
|
||||
segment.unescaped().fmt(f)?
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
pub fn display<'a>(&'a self, db: &'a dyn crate::db::ExpandDatabase) -> impl fmt::Display + 'a {
|
||||
Display { db, path: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ModPath {
|
||||
struct Display<'a> {
|
||||
db: &'a dyn ExpandDatabase,
|
||||
path: &'a ModPath,
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Display<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self._fmt(f, true)
|
||||
display_fmt_path(self.db, self.path, f, true)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Display for UnescapedModPath<'a> {
|
||||
struct UnescapedDisplay<'a> {
|
||||
db: &'a dyn ExpandDatabase,
|
||||
path: &'a UnescapedModPath<'a>,
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for UnescapedDisplay<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0._fmt(f, false)
|
||||
display_fmt_path(self.db, self.path.0, f, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,6 +148,46 @@ impl From<Name> for ModPath {
|
|||
ModPath::from_segments(PathKind::Plain, iter::once(name))
|
||||
}
|
||||
}
|
||||
fn display_fmt_path(
|
||||
db: &dyn ExpandDatabase,
|
||||
path: &ModPath,
|
||||
f: &mut fmt::Formatter<'_>,
|
||||
escaped: bool,
|
||||
) -> fmt::Result {
|
||||
let mut first_segment = true;
|
||||
let mut add_segment = |s| -> fmt::Result {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
f.write_str(s)?;
|
||||
Ok(())
|
||||
};
|
||||
match path.kind {
|
||||
PathKind::Plain => {}
|
||||
PathKind::Super(0) => add_segment("self")?,
|
||||
PathKind::Super(n) => {
|
||||
for _ in 0..n {
|
||||
add_segment("super")?;
|
||||
}
|
||||
}
|
||||
PathKind::Crate => add_segment("crate")?,
|
||||
PathKind::Abs => add_segment("")?,
|
||||
PathKind::DollarCrate(_) => add_segment("$crate")?,
|
||||
}
|
||||
for segment in &path.segments {
|
||||
if !first_segment {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
first_segment = false;
|
||||
if escaped {
|
||||
segment.display(db).fmt(f)?;
|
||||
} else {
|
||||
segment.unescaped().display(db).fmt(f)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn convert_path(
|
||||
db: &dyn ExpandDatabase,
|
||||
|
|
|
@ -24,27 +24,6 @@ enum Repr {
|
|||
TupleField(usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for Name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match &self.0 {
|
||||
Repr::Text(text) => 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 {
|
||||
Repr::Text(text) => {
|
||||
let text = text.strip_prefix("r#").unwrap_or(text);
|
||||
fmt::Display::fmt(&text, f)
|
||||
}
|
||||
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> UnescapedName<'a> {
|
||||
/// 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.
|
||||
|
@ -60,6 +39,11 @@ impl<'a> UnescapedName<'a> {
|
|||
Repr::TupleField(it) => SmolStr::new(it.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display(&'a self, db: &dyn crate::db::ExpandDatabase) -> impl fmt::Display + 'a {
|
||||
_ = db;
|
||||
UnescapedDisplay { name: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl Name {
|
||||
|
@ -167,6 +151,40 @@ impl Name {
|
|||
Repr::TupleField(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display<'a>(&'a self, db: &dyn crate::db::ExpandDatabase) -> impl fmt::Display + 'a {
|
||||
_ = db;
|
||||
Display { name: self }
|
||||
}
|
||||
}
|
||||
|
||||
struct Display<'a> {
|
||||
name: &'a Name,
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Display<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match &self.name.0 {
|
||||
Repr::Text(text) => fmt::Display::fmt(&text, f),
|
||||
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct UnescapedDisplay<'a> {
|
||||
name: &'a UnescapedName<'a>,
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for UnescapedDisplay<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match &self.name.0 .0 {
|
||||
Repr::Text(text) => {
|
||||
let text = text.strip_prefix("r#").unwrap_or(text);
|
||||
fmt::Display::fmt(&text, f)
|
||||
}
|
||||
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsName {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue