Skip to content

fix: Insert whitespace into trait-impl completions when coming from macros #12376

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

Merged
merged 2 commits into from
May 24, 2022
Merged
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 @@ -939,7 +939,6 @@ struct Foo(usize);

impl FooB for Foo {
$0fn foo< 'lt>(& 'lt self){}

}
"#,
)
Expand Down
1 change: 0 additions & 1 deletion crates/ide-completion/src/completions/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|| ctx.is_path_disallowed()
|| ctx.expects_item()
|| ctx.expects_assoc_item()
|| ctx.expects_variant()
{
return None;
}
Expand Down
108 changes: 91 additions & 17 deletions crates/ide-completion/src/completions/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
//! ```

use hir::{self, HasAttrs};
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
use ide_db::{
path_transform::PathTransform, syntax_helpers::insert_whitespace_into_node,
traits::get_missing_assoc_items, SymbolKind,
};
use syntax::{
ast::{self, edit_in_place::AttrsOwnerEdit},
display::function_declaration,
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, T,
};
use text_edit::TextEdit;
Expand Down Expand Up @@ -179,7 +181,7 @@ fn add_function_impl(
_ => unreachable!(),
};

let function_decl = function_declaration(&transformed_fn);
let function_decl = function_declaration(&transformed_fn, source.file_id.is_macro());
match ctx.config.snippet_cap {
Some(cap) => {
let snippet = format!("{} {{\n $0\n}}", function_decl);
Expand Down Expand Up @@ -260,7 +262,7 @@ fn add_const_impl(
_ => unreachable!(),
};

let label = make_const_compl_syntax(&transformed_const);
let label = make_const_compl_syntax(&transformed_const, source.file_id.is_macro());
let replacement = format!("{} ", label);

let mut item = CompletionItem::new(SymbolKind::Const, replacement_range, label);
Expand All @@ -283,29 +285,55 @@ fn add_const_impl(
}
}

fn make_const_compl_syntax(const_: &ast::Const) -> String {
fn make_const_compl_syntax(const_: &ast::Const, needs_whitespace: bool) -> String {
const_.remove_attrs_and_docs();
let const_ = if needs_whitespace {
insert_whitespace_into_node::insert_ws_into(const_.syntax().clone())
} else {
const_.syntax().clone()
};

let const_start = const_.syntax().text_range().start();
let const_end = const_.syntax().text_range().end();

let start =
const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start());
let start = const_.text_range().start();
let const_end = const_.text_range().end();

let end = const_
.syntax()
.children_with_tokens()
.find(|s| s.kind() == T![;] || s.kind() == T![=])
.map_or(const_end, |f| f.text_range().start());

let len = end - start;
let range = TextRange::new(0.into(), len);

let syntax = const_.syntax().text().slice(range).to_string();
let syntax = const_.text().slice(range).to_string();

format!("{} =", syntax.trim_end())
}

fn function_declaration(node: &ast::Fn, needs_whitespace: bool) -> String {
node.remove_attrs_and_docs();

let node = if needs_whitespace {
insert_whitespace_into_node::insert_ws_into(node.syntax().clone())
} else {
node.syntax().clone()
};

let start = node.text_range().start();
let end = node.text_range().end();

let end = node
.last_child_or_token()
.filter(|s| s.kind() == T![;] || s.kind() == SyntaxKind::BLOCK_EXPR)
.map_or(end, |f| f.text_range().start());

let len = end - start;
let range = TextRange::new(0.into(), len);

let syntax = node.text().slice(range).to_string();

syntax.trim_end().to_owned()
}

fn replacement_range(ctx: &CompletionContext, item: &SyntaxNode) -> TextRange {
let first_child = item
.children_with_tokens()
Expand Down Expand Up @@ -655,8 +683,7 @@ trait Test {
struct T;

impl Test for T {
fn foo<T>()
where T: Into<String> {
fn foo<T>() where T: Into<String> {
$0
}
}
Expand Down Expand Up @@ -992,7 +1019,7 @@ trait SomeTrait<T> {}

trait Foo<T> {
fn function()
where Self: SomeTrait<T>;
where Self: SomeTrait<T>;
}
struct Bar;

Expand All @@ -1005,13 +1032,13 @@ trait SomeTrait<T> {}

trait Foo<T> {
fn function()
where Self: SomeTrait<T>;
where Self: SomeTrait<T>;
}
struct Bar;

impl Foo<u32> for Bar {
fn function()
where Self: SomeTrait<u32> {
where Self: SomeTrait<u32> {
$0
}
}
Expand Down Expand Up @@ -1052,4 +1079,51 @@ impl Tr for () {
"#]],
);
}

#[test]
fn fixes_up_macro_generated() {
check_edit(
"fn foo",
r#"
macro_rules! noop {
($($item: item)*) => {
$($item)*
}
}

noop! {
trait Foo {
fn foo(&mut self, bar: i64, baz: &mut u32) -> Result<(), u32>;
}
}

struct Test;

impl Foo for Test {
$0
}
"#,
r#"
macro_rules! noop {
($($item: item)*) => {
$($item)*
}
}

noop! {
trait Foo {
fn foo(&mut self, bar: i64, baz: &mut u32) -> Result<(), u32>;
}
}

struct Test;

impl Foo for Test {
fn foo(&mut self,bar:i64,baz: &mut u32) -> Result<(),u32> {
$0
}
}
"#,
);
}
}
9 changes: 1 addition & 8 deletions crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,6 @@ impl<'a> CompletionContext<'a> {
matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
}

pub(crate) fn expects_variant(&self) -> bool {
matches!(self.name_ctx(), Some(NameContext { kind: NameKind::Variant, .. }))
}

pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Impl))
}
Expand Down Expand Up @@ -379,10 +375,7 @@ impl<'a> CompletionContext<'a> {
pub(crate) fn is_path_disallowed(&self) -> bool {
self.previous_token_is(T![unsafe])
|| matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility))
|| matches!(
self.name_ctx(),
Some(NameContext { kind: NameKind::Module(_) | NameKind::Rename, .. })
)
|| (matches!(self.name_ctx(), Some(NameContext { .. })) && self.pattern_ctx.is_none())
|| matches!(self.pattern_ctx, Some(PatternContext { record_pat: Some(_), .. }))
|| matches!(
self.nameref_ctx(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
ted::insert(pos, insert);
}

if let Some(it) = syn.last_token().filter(|it| it.kind() == SyntaxKind::WHITESPACE) {
ted::remove(it);
}

syn
}

Expand Down
17 changes: 4 additions & 13 deletions crates/ide/src/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ fn main() {
"#,
expect![[r#"
bar
for _ in 0..42{}
"#]],
for _ in 0..42{}"#]],
);
}

Expand All @@ -273,7 +272,6 @@ f$0oo!();
expect![[r#"
foo
fn b(){}

"#]],
);
}
Expand All @@ -297,8 +295,7 @@ f$0oo!();
fn some_thing() -> u32 {
let a = 0;
a+10
}
"#]],
}"#]],
);
}

Expand Down Expand Up @@ -359,8 +356,7 @@ fn main() {
"#,
expect![[r#"
match_ast
{}
"#]],
{}"#]],
);
}

Expand Down Expand Up @@ -421,8 +417,7 @@ fn main() {
"#,
expect![[r#"
foo
fn f<T>(_: &dyn ::std::marker::Copy){}
"#]],
fn f<T>(_: &dyn ::std::marker::Copy){}"#]],
);
}

Expand All @@ -440,7 +435,6 @@ struct Foo {}
expect![[r#"
Clone
impl < >core::clone::Clone for Foo< >{}

"#]],
);
}
Expand All @@ -458,7 +452,6 @@ struct Foo {}
expect![[r#"
Copy
impl < >core::marker::Copy for Foo< >{}

"#]],
);
}
Expand All @@ -475,7 +468,6 @@ struct Foo {}
expect![[r#"
Copy
impl < >core::marker::Copy for Foo< >{}

"#]],
);
check(
Expand All @@ -488,7 +480,6 @@ struct Foo {}
expect![[r#"
Clone
impl < >core::clone::Clone for Foo< >{}

"#]],
);
}
Expand Down
51 changes: 0 additions & 51 deletions crates/syntax/src/display.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ mod token_text;
#[cfg(test)]
mod tests;

pub mod display;
pub mod algo;
pub mod ast;
#[doc(hidden)]
Expand Down