rust/crates/ra_hir_expand/src/name.rs

231 lines
5.2 KiB
Rust
Raw Normal View History

2019-10-30 08:12:55 -05:00
//! FIXME: write short doc here
use std::fmt;
use ra_syntax::{ast, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygiene info, but we are
/// not there yet!
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Name(Repr);
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Repr {
Text(SmolStr),
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 Name {
/// Note: this is private to make creating name from random string hard.
/// Hopefully, this should allow us to integrate hygiene cleaner in the
/// future, and to switch to interned representation of names.
const fn new_text(text: SmolStr) -> Name {
Name(Repr::Text(text))
}
pub fn new_tuple_field(idx: usize) -> Name {
Name(Repr::TupleField(idx))
}
2020-05-31 03:59:40 -05:00
pub fn new_lifetime(lt: &ra_syntax::SyntaxToken) -> Name {
assert!(lt.kind() == ra_syntax::SyntaxKind::LIFETIME);
Name(Repr::Text(lt.text().clone()))
}
2019-10-30 08:12:55 -05:00
/// Shortcut to create inline plain text name
const fn new_inline_ascii(text: &[u8]) -> Name {
Name::new_text(SmolStr::new_inline_from_ascii(text.len(), text))
2019-10-30 08:12:55 -05:00
}
/// Resolve a name from the text of token.
fn resolve(raw_text: &SmolStr) -> Name {
let raw_start = "r#";
if raw_text.as_str().starts_with(raw_start) {
Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
} else {
Name::new_text(raw_text.clone())
}
}
pub fn missing() -> Name {
Name::new_text("[missing name]".into())
}
pub fn as_tuple_index(&self) -> Option<usize> {
match self.0 {
Repr::TupleField(idx) => Some(idx),
_ => None,
}
}
}
pub trait AsName {
fn as_name(&self) -> Name;
}
impl AsName for ast::NameRef {
fn as_name(&self) -> Name {
match self.as_tuple_field() {
Some(idx) => Name::new_tuple_field(idx),
None => Name::resolve(self.text()),
}
}
}
impl AsName for ast::Name {
fn as_name(&self) -> Name {
Name::resolve(self.text())
}
}
impl AsName for ast::NameOrNameRef {
fn as_name(&self) -> Name {
match self {
ast::NameOrNameRef::Name(it) => it.as_name(),
ast::NameOrNameRef::NameRef(it) => it.as_name(),
}
}
}
impl AsName for tt::Ident {
fn as_name(&self) -> Name {
Name::resolve(&self.text)
}
}
2019-10-30 08:12:55 -05:00
impl AsName for ast::FieldKind {
fn as_name(&self) -> Name {
match self {
ast::FieldKind::Name(nr) => nr.as_name(),
2020-01-07 07:49:42 -06:00
ast::FieldKind::Index(idx) => {
let idx = idx.text().parse::<usize>().unwrap_or(0);
Name::new_tuple_field(idx)
}
2019-10-30 08:12:55 -05:00
}
}
}
impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name {
2020-07-01 02:53:53 -05:00
Name::new_text(SmolStr::new(&*self.name))
2019-10-30 08:12:55 -05:00
}
}
2019-12-13 14:43:53 -06:00
pub mod known {
macro_rules! known_names {
($($ident:ident),* $(,)?) => {
$(
#[allow(bad_style)]
pub const $ident: super::Name =
super::Name::new_inline_ascii(stringify!($ident).as_bytes());
)*
};
}
known_names!(
// Primitives
isize,
i8,
i16,
i32,
i64,
i128,
usize,
u8,
u16,
u32,
u64,
u128,
f32,
f64,
bool,
char,
str,
// Special names
macro_rules,
doc,
2019-12-13 14:43:53 -06:00
// Components of known path (value or mod name)
std,
2020-01-27 16:09:56 -06:00
core,
alloc,
2019-12-13 14:43:53 -06:00
iter,
ops,
future,
result,
boxed,
// Components of known path (type name)
IntoIterator,
Item,
Try,
Ok,
Future,
Result,
Output,
Target,
Box,
RangeFrom,
RangeFull,
RangeInclusive,
RangeToInclusive,
RangeTo,
Range,
Neg,
Not,
Index,
2019-12-13 14:43:53 -06:00
// Builtin macros
file,
column,
compile_error,
line,
2020-03-11 10:08:12 -05:00
assert,
2019-12-13 14:43:53 -06:00
stringify,
2020-03-02 00:05:15 -06:00
concat,
2020-03-06 08:58:45 -06:00
include,
2020-06-27 13:02:47 -05:00
include_bytes,
2020-06-27 07:31:19 -05:00
include_str,
2019-12-13 14:43:53 -06:00
format_args,
format_args_nl,
env,
option_env,
2019-12-13 14:43:53 -06:00
// Builtin derives
Copy,
Clone,
Default,
Debug,
Hash,
Ord,
PartialOrd,
Eq,
PartialEq,
);
// self/Self cannot be used as an identifier
pub const SELF_PARAM: super::Name = super::Name::new_inline_ascii(b"self");
pub const SELF_TYPE: super::Name = super::Name::new_inline_ascii(b"Self");
#[macro_export]
2019-12-13 15:01:06 -06:00
macro_rules! name {
2019-12-13 14:43:53 -06:00
(self) => {
$crate::name::known::SELF_PARAM
};
(Self) => {
$crate::name::known::SELF_TYPE
};
($ident:ident) => {
$crate::name::known::$ident
};
}
}
2019-12-13 15:01:06 -06:00
pub use crate::name;