mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Use CompactString
for Identifier
(#12101)
This commit is contained in:
parent
db6ee74cbe
commit
5109b50bb3
474 changed files with 4953 additions and 4776 deletions
|
@ -13,6 +13,8 @@ license = { workspace = true }
|
|||
[lib]
|
||||
|
||||
[dependencies]
|
||||
ruff_cache = { workspace = true, optional = true }
|
||||
ruff_macros = { workspace = true, optional = true }
|
||||
ruff_python_trivia = { workspace = true }
|
||||
ruff_source_file = { workspace = true }
|
||||
ruff_text_size = { workspace = true }
|
||||
|
@ -23,10 +25,16 @@ is-macro = { workspace = true }
|
|||
itertools = { workspace = true }
|
||||
once_cell = { workspace = true }
|
||||
rustc-hash = { workspace = true }
|
||||
schemars = { workspace = true, optional = true }
|
||||
serde = { workspace = true, optional = true }
|
||||
compact_str = { workspace = true }
|
||||
|
||||
[features]
|
||||
serde = ["dep:serde", "ruff_text_size/serde"]
|
||||
serde = ["dep:serde", "ruff_text_size/serde", "dep:ruff_cache", "compact_str/serde", "dep:ruff_macros", "dep:schemars"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[package.metadata.cargo-shear]
|
||||
# Used via `CacheKey` macro expansion.
|
||||
ignored = ["ruff_cache"]
|
||||
|
|
|
@ -7,7 +7,7 @@ use ruff_python_trivia::{indentation_at_offset, CommentRanges, SimpleTokenKind,
|
|||
use ruff_source_file::Locator;
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||
|
||||
use crate::name::{QualifiedName, QualifiedNameBuilder};
|
||||
use crate::name::{Name, QualifiedName, QualifiedNameBuilder};
|
||||
use crate::parenthesize::parenthesized_range;
|
||||
use crate::statement_visitor::StatementVisitor;
|
||||
use crate::visitor::Visitor;
|
||||
|
@ -1403,7 +1403,7 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr {
|
|||
}
|
||||
|
||||
/// Format the expression as a `typing.Optional`-style optional.
|
||||
pub fn typing_optional(elt: Expr, binding: String) -> Expr {
|
||||
pub fn typing_optional(elt: Expr, binding: Name) -> Expr {
|
||||
Expr::Subscript(ast::ExprSubscript {
|
||||
value: Box::new(Expr::Name(ast::ExprName {
|
||||
id: binding,
|
||||
|
@ -1417,8 +1417,8 @@ pub fn typing_optional(elt: Expr, binding: String) -> Expr {
|
|||
}
|
||||
|
||||
/// Format the expressions as a `typing.Union`-style union.
|
||||
pub fn typing_union(elts: &[Expr], binding: String) -> Expr {
|
||||
fn tuple(elts: &[Expr], binding: String) -> Expr {
|
||||
pub fn typing_union(elts: &[Expr], binding: Name) -> Expr {
|
||||
fn tuple(elts: &[Expr], binding: Name) -> Expr {
|
||||
match elts {
|
||||
[] => Expr::Tuple(ast::ExprTuple {
|
||||
elts: vec![],
|
||||
|
|
|
@ -1,9 +1,213 @@
|
|||
use std::borrow::{Borrow, Cow};
|
||||
use std::fmt::{Debug, Display, Formatter, Write};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{nodes, Expr};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize, ruff_macros::CacheKey)
|
||||
)]
|
||||
pub struct Name(compact_str::CompactString);
|
||||
|
||||
impl Name {
|
||||
#[inline]
|
||||
pub fn empty() -> Self {
|
||||
Self(compact_str::CompactString::default())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new(name: impl AsRef<str>) -> Self {
|
||||
Self(compact_str::CompactString::new(name))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_static(name: &'static str) -> Self {
|
||||
// TODO(Micha): Use CompactString::const_new once we upgrade to 0.8 https://github.com/ParkMyCar/compact_str/pull/336
|
||||
Self(compact_str::CompactString::from(name))
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
self.0.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Name {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Name({:?})", self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Name {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Name {
|
||||
type Target = str;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Borrow<str> for Name {
|
||||
#[inline]
|
||||
fn borrow(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Name {
|
||||
#[inline]
|
||||
fn from(s: &'a str) -> Self {
|
||||
Name(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Name {
|
||||
#[inline]
|
||||
fn from(s: String) -> Self {
|
||||
Name(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a String> for Name {
|
||||
#[inline]
|
||||
fn from(s: &'a String) -> Self {
|
||||
Name(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Cow<'a, str>> for Name {
|
||||
#[inline]
|
||||
fn from(cow: Cow<'a, str>) -> Self {
|
||||
Name(cow.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<str>> for Name {
|
||||
#[inline]
|
||||
fn from(b: Box<str>) -> Self {
|
||||
Name(b.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<compact_str::CompactString> for Name {
|
||||
#[inline]
|
||||
fn from(value: compact_str::CompactString) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Name> for compact_str::CompactString {
|
||||
#[inline]
|
||||
fn from(name: Name) -> Self {
|
||||
name.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<char> for Name {
|
||||
fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
|
||||
Self(iter.into_iter().collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Name {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for Name {
|
||||
#[inline]
|
||||
fn eq(&self, other: &str) -> bool {
|
||||
self.as_str() == other
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Name> for str {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Name) -> bool {
|
||||
other == self
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<&str> for Name {
|
||||
#[inline]
|
||||
fn eq(&self, other: &&str) -> bool {
|
||||
self.as_str() == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Name> for &str {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Name) -> bool {
|
||||
other == self
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<String> for Name {
|
||||
fn eq(&self, other: &String) -> bool {
|
||||
self == other.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Name> for String {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Name) -> bool {
|
||||
other == self
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<&String> for Name {
|
||||
#[inline]
|
||||
fn eq(&self, other: &&String) -> bool {
|
||||
self.as_str() == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Name> for &String {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Name) -> bool {
|
||||
other == self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl schemars::JsonSchema for Name {
|
||||
fn is_referenceable() -> bool {
|
||||
String::is_referenceable()
|
||||
}
|
||||
|
||||
fn schema_name() -> String {
|
||||
String::schema_name()
|
||||
}
|
||||
|
||||
fn schema_id() -> std::borrow::Cow<'static, str> {
|
||||
String::schema_id()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||
String::json_schema(gen)
|
||||
}
|
||||
|
||||
fn _schemars_private_non_optional_json_schema(
|
||||
gen: &mut schemars::gen::SchemaGenerator,
|
||||
) -> schemars::schema::Schema {
|
||||
String::_schemars_private_non_optional_json_schema(gen)
|
||||
}
|
||||
|
||||
fn _schemars_private_is_option() -> bool {
|
||||
String::_schemars_private_is_option()
|
||||
}
|
||||
}
|
||||
|
||||
/// A representation of a qualified name, like `typing.List`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct QualifiedName<'a>(SegmentsVec<'a>);
|
||||
|
|
|
@ -12,6 +12,7 @@ use itertools::Itertools;
|
|||
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||
|
||||
use crate::name::Name;
|
||||
use crate::{
|
||||
int,
|
||||
str::Quote,
|
||||
|
@ -1762,12 +1763,6 @@ impl PartialEq<str> for StringLiteralValue {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialEq<String> for StringLiteralValue {
|
||||
fn eq(&self, other: &String) -> bool {
|
||||
self == other.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StringLiteralValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.to_str())
|
||||
|
@ -2740,10 +2735,16 @@ impl From<ExprStarred> for Expr {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ExprName {
|
||||
pub range: TextRange,
|
||||
pub id: String,
|
||||
pub id: Name,
|
||||
pub ctx: ExprContext,
|
||||
}
|
||||
|
||||
impl ExprName {
|
||||
pub fn id(&self) -> &Name {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ExprName> for Expr {
|
||||
fn from(payload: ExprName) -> Self {
|
||||
Expr::Name(payload)
|
||||
|
@ -3763,19 +3764,23 @@ impl IpyEscapeKind {
|
|||
/// ```
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Identifier {
|
||||
pub id: String,
|
||||
pub id: Name,
|
||||
pub range: TextRange,
|
||||
}
|
||||
|
||||
impl Identifier {
|
||||
#[inline]
|
||||
pub fn new(id: impl Into<String>, range: TextRange) -> Self {
|
||||
pub fn new(id: impl Into<Name>, range: TextRange) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
range,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &Name {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
!self.id.is_empty()
|
||||
}
|
||||
|
@ -3798,7 +3803,7 @@ impl PartialEq<str> for Identifier {
|
|||
impl PartialEq<String> for Identifier {
|
||||
#[inline]
|
||||
fn eq(&self, other: &String) -> bool {
|
||||
&self.id == other
|
||||
self.id == other
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3817,22 +3822,15 @@ impl AsRef<str> for Identifier {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsRef<String> for Identifier {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &String {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Identifier {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(&self.id, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Identifier> for String {
|
||||
impl From<Identifier> for Name {
|
||||
#[inline]
|
||||
fn from(identifier: Identifier) -> String {
|
||||
fn from(identifier: Identifier) -> Name {
|
||||
identifier.id
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue