Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ide_db::{
source_change::SourceChangeBuilder,
};
use syntax::ToSmolStr;
use syntax::ast::edit::AstNodeEdit;

use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

Expand All @@ -23,33 +24,32 @@ pub(crate) fn trait_impl_redundant_assoc_item(

let default_range = d.impl_.syntax_node_ptr().text_range();
let trait_name = d.trait_.name(db).display_no_db(ctx.edition).to_smolstr();
let indent_level = d.trait_.source(db).map_or(0, |it| it.value.indent_level().0) + 1;

let (redundant_item_name, diagnostic_range, redundant_item_def) = match assoc_item {
hir::AssocItem::Function(id) => {
let function = id;
(
format!("`fn {redundant_assoc_item_name}`"),
function.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
format!("\n {};", function.display(db, ctx.display_target)),
format!("\n{};", function.display(db, ctx.display_target)),
)
}
hir::AssocItem::Const(id) => {
let constant = id;
(
format!("`const {redundant_assoc_item_name}`"),
constant.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
format!("\n {};", constant.display(db, ctx.display_target)),
format!("\n{};", constant.display(db, ctx.display_target)),
)
}
hir::AssocItem::TypeAlias(id) => {
let type_alias = id;
(
format!("`type {redundant_assoc_item_name}`"),
type_alias.source(db).map(|it| it.syntax().text_range()).unwrap_or(default_range),
format!(
"\n type {};",
type_alias.name(ctx.sema.db).display_no_db(ctx.edition).to_smolstr()
),
// FIXME cannot generate generic parameter and bounds
format!("\ntype {};", type_alias.name(ctx.sema.db).display_no_db(ctx.edition)),
)
}
};
Expand All @@ -65,7 +65,7 @@ pub(crate) fn trait_impl_redundant_assoc_item(
.with_fixes(quickfix_for_redundant_assoc_item(
ctx,
d,
redundant_item_def,
stdx::indent_string(redundant_item_def, indent_level).into(),
diagnostic_range,
))
}
Expand Down Expand Up @@ -191,6 +191,89 @@ impl Marker for Foo {
)
}

#[test]
fn quickfix_indentations() {
check_fix(
r#"
mod indent {
trait Marker {
fn boo();
}
struct Foo;
impl Marker for Foo {
fn$0 bar<T: Copy>(_a: i32, _b: T) -> String {}
fn boo() {}
}
}
"#,
r#"
mod indent {
trait Marker {
fn bar<T>(_a: i32, _b: T) -> String
where
T: Copy,;
fn boo();
}
struct Foo;
impl Marker for Foo {
fn bar<T: Copy>(_a: i32, _b: T) -> String {}
fn boo() {}
}
}
"#,
);

check_fix(
r#"
mod indent {
trait Marker {
fn foo () {}
}
struct Foo;
impl Marker for Foo {
const FLAG: bool$0 = false;
}
}
"#,
r#"
mod indent {
trait Marker {
const FLAG: bool;
fn foo () {}
}
struct Foo;
impl Marker for Foo {
const FLAG: bool = false;
}
}
"#,
);

check_fix(
r#"
mod indent {
trait Marker {
}
struct Foo;
impl Marker for Foo {
type T = i32;$0
}
}
"#,
r#"
mod indent {
trait Marker {
type T;
}
struct Foo;
impl Marker for Foo {
type T = i32;
}
}
"#,
);
}

#[test]
fn quickfix_dont_work() {
check_no_fix(
Expand Down
24 changes: 24 additions & 0 deletions crates/stdx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Missing batteries for standard libraries.
use std::borrow::Cow;
use std::io as sio;
use std::process::Command;
use std::{cmp::Ordering, ops, time::Instant};
Expand Down Expand Up @@ -214,6 +215,29 @@ pub fn trim_indent(mut text: &str) -> String {
.collect()
}

/// Indent non empty lines, including the first line
#[must_use]
pub fn indent_string<'a, S>(s: S, indent_level: u8) -> Cow<'a, str>
where
Cow<'a, str>: From<S>,
{
let s = Cow::from(s);
if indent_level == 0 || s.is_empty() {
return s;
}
let indent_str = " ".repeat(indent_level as usize);
s.split_inclusive("\n")
.map(|line| {
if line.trim_end().is_empty() {
Cow::Borrowed(line)
} else {
format!("{indent_str}{line}").into()
}
})
.collect::<String>()
.into()
}

pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
where
F: FnMut(&T) -> Ordering,
Expand Down
Loading