ide_db::syntax_helpers::suggest_name

Struct NameGenerator

source
pub struct NameGenerator { /* private fields */ }
Expand description

Generator for new names

The generator keeps track of existing names and suggests new names that do not conflict with existing names.

The generator will try to resolve conflicts by adding a numeric suffix to the name, e.g. a, a1, a2, …

§Examples

let mut generator = NameGenerator::new();
assert_eq!(generator.suggest_name("a"), "a");
assert_eq!(generator.suggest_name("a"), "a1");

assert_eq!(generator.suggest_name("b2"), "b2");
assert_eq!(generator.suggest_name("b"), "b3");

Implementations§

source§

impl NameGenerator

source

pub fn new() -> Self

Create a new empty generator

source

pub fn new_with_names<'a>(existing_names: impl Iterator<Item = &'a str>) -> Self

Create a new generator with existing names. When suggesting a name, it will avoid conflicts with existing names.

source

pub fn suggest_name(&mut self, name: &str) -> SmolStr

Suggest a name without conflicts. If the name conflicts with existing names, it will try to resolve the conflict by adding a numeric suffix.

source

pub fn for_type( &mut self, ty: &Type, db: &RootDatabase, edition: Edition, ) -> Option<SmolStr>

Suggest a name for given type.

The function will strip references first, and suggest name from the inner type.

  • If ty is an ADT, it will suggest the name of the ADT.
    • If ty is wrapped in Box, Option or Result, it will suggest the name from the inner type.
  • If ty is a trait, it will suggest the name of the trait.
  • If ty is an impl Trait, it will suggest the name of the first trait.

If the suggested name conflicts with reserved keywords, it will return None.

source

pub fn for_impl_trait_as_generic(&mut self, ty: &ImplTraitType) -> SmolStr

Suggest name of impl trait type

§Current implementation

In current implementation, the function tries to get the name from the first character of the name for the first type bound.

If the name conflicts with existing generic parameters, it will try to resolve the conflict with for_unique_generic_name.

Trait Implementations§

source§

impl Debug for NameGenerator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for NameGenerator

source§

fn default() -> NameGenerator

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Cast for T

§

fn cast<U>(self, interner: <U as HasInterner>::Interner) -> U
where Self: CastTo<U>, U: HasInterner,

Cast a value to type U using CastTo.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoBox<dyn Any> for T
where T: Any,

§

fn into_box(self) -> Box<dyn Any>

Convert self into the appropriate boxed form.
§

impl<T> IntoBox<dyn Any + Send> for T
where T: Any + Send,

§

fn into_box(self) -> Box<dyn Any + Send>

Convert self into the appropriate boxed form.
§

impl<T> IntoBox<dyn Any + Sync + Send> for T
where T: Any + Send + Sync,

§

fn into_box(self) -> Box<dyn Any + Sync + Send>

Convert self into the appropriate boxed form.
source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<'a, T> Captures<'a> for T
where T: ?Sized,