Skip to content

fix: ide-assists, gen from impl for enum indent #19789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

A4-Tacks
Copy link
Contributor

mod foo {
    enum Foo {
        $0String(String),
    }
}

Old output:

mod foo {
    enum Foo {
        String(String),
    }

impl From<String> for Foo {
    fn from(v: String) -> Self {
        Self::String(v)
    }
}
}

Fixed output:

mod foo {
    enum Foo {
        String(String),
    }

    impl From<String> for Foo {
        fn from(v: String) -> Self {
            Self::String(v)
        }
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 13, 2025
let level = indent_level.0 as usize;

let indent = indent_level.to_string();
let mut buf = String::with_capacity(s.len() + level * 3 * 4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the values (3 and 4) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 4 is the number of spaces for indent
  • 3 is an approximate expected number of lines used for optimization

buf.push_str(line);
buf.push('\n');

if !rest.split_once('\n').map_or(rest, |s| s.0).is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pull out the split_once/map_or into a separate statement? it makes it a bit tricky to understand what the condition here actually is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@A4-Tacks A4-Tacks requested a review from davidbarsky May 27, 2025 23:20
@@ -250,6 +252,36 @@ pub fn add_trait_assoc_items_to_impl(
first_item.unwrap()
}

pub(crate) fn indent_string(mut s: &str, indent_level: IndentLevel) -> Cow<'_, str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong kind of API here, we already have indentation things that work on the AST. If those don't suffice this will have to wait until we have our own formatter to pretty print nodes with. Our assist output is currently best effort due to the lack of that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, many ide-assists directly generate strings. With this API, I can quickly fix nearly ten ide-assists implements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this API and continue?

@A4-Tacks A4-Tacks requested a review from Veykril May 28, 2025 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants