Skip to content

fix: ide-assists, some generate_\* indentations #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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions crates/ide-assists/src/handlers/generate_from_impl_for_enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use ide_db::{RootDatabase, famous_defs::FamousDefs};
use syntax::ast::{self, AstNode, HasName};
use syntax::ast::{self, AstNode, HasName, edit::AstNodeEdit};

use crate::{AssistContext, AssistId, Assists, utils::generate_trait_impl_text_intransitive};
use crate::{
AssistContext, AssistId, Assists,
utils::{generate_trait_impl_text_intransitive, indent_string},
};

// Assist: generate_from_impl_for_enum
//
Expand Down Expand Up @@ -71,7 +74,7 @@ pub(crate) fn generate_from_impl_for_enum(
)
};
let from_impl = generate_trait_impl_text_intransitive(&enum_, &from_trait, &impl_code);
edit.insert(start_offset, from_impl);
edit.insert(start_offset, indent_string(&from_impl, enum_.indent_level()));
},
)
}
Expand Down Expand Up @@ -295,6 +298,54 @@ impl<'a> From<&'a i32> for Generic<'a> {
Self::One(v)
}
}
"#,
);
}

#[test]
fn test_non_zero_indent() {
check_assist(
generate_from_impl_for_enum,
r#"
//- minicore: from
mod foo {
enum Generic<'a> { $0One(&'a i32) }
}
"#,
r#"
mod foo {
enum Generic<'a> { One(&'a i32) }

impl<'a> From<&'a i32> for Generic<'a> {
fn from(v: &'a i32) -> Self {
Self::One(v)
}
}
}
"#,
);
check_assist(
generate_from_impl_for_enum,
r#"
//- minicore: from
mod foo {
mod bar {
enum Generic<'a> { $0One(&'a i32) }
}
}
"#,
r#"
mod foo {
mod bar {
enum Generic<'a> { One(&'a i32) }

impl<'a> From<&'a i32> for Generic<'a> {
fn from(v: &'a i32) -> Self {
Self::One(v)
}
}
}
}
"#,
);
}
Expand Down
31 changes: 31 additions & 0 deletions crates/ide-assists/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Assorted functions shared by several assists.

use std::borrow::Cow;

pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
use hir::{
DisplayTarget, HasAttrs as HirHasAttrs, HirDisplay, InFile, ModuleDef, PathResolution,
Expand Down Expand Up @@ -250,6 +252,35 @@ 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?

Copy link
Member

@Veykril Veykril Jun 2, 2025

Choose a reason for hiding this comment

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

The problem with this is that it makes us even more reliant on the text based editing, while we want to generate nodes proper and format them with a proper formatter. Until we have a formatter we only try to fix up indentation on a best effort basis.

(on a related note, all the assist PRs you have opened are text based which is also not ideal for the same reason)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(on a related note, all the assist PRs you have opened are text based which is also not ideal for the same reason)

Should I use ted?

Copy link
Member

@Veykril Veykril Jun 2, 2025

Choose a reason for hiding this comment

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

Ideally any new assists should be using the SyntaxEditor infra, see #15710 and #18285 for context

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with this is that it makes us even more reliant on the text based editing, while we want to generate nodes proper and format them with a proper formatter. Until we have a formatter we only try to fix up indentation on a best effort basis.

Can this PR be merged? It doesn't seem to make things worse

if indent_level.is_zero() || s.is_empty() {
return s.into();
}
let level = indent_level.0 as usize;

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

if !s.starts_with('\n') {
buf.push_str(&indent);
}

while let Some((line, rest)) = s.split_once('\n') {
buf.push_str(line);
buf.push('\n');

if !rest.split_once('\n').map_or(rest, |s| s.0).is_empty() {
buf.push_str(&indent);
}

s = rest;
}

buf.push_str(s);

buf.into()
}

pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
node.children_with_tokens()
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
Expand Down