Skip to content

feat: completion list suggests methods in the order we most likely want #16044

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

Closed
wants to merge 63 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
12b67ef
basic test
mustakimali Dec 6, 2023
983bc9a
Fix runnable cwd on Windows
lnicola Dec 5, 2023
7218995
Don't explicitly warn against `semicolon_in_expressions_from_macros`
Xanewok Dec 5, 2023
4c15158
Bump ra-ap-rustc_lexer
lnicola Dec 5, 2023
e9bc90c
Bump ra-ap-rustc_parse_format
lnicola Dec 5, 2023
41a8497
Revert "Temporarily revert delay_bug to delayed_bug change"
lnicola Dec 5, 2023
23fb593
Bump ra-ap-rustc_index and ra-ap-rustc_abi
lnicola Dec 5, 2023
1364705
Insert fn call parens only if the parens inserted around field name
dfireBird Dec 4, 2023
99e4fc4
Improve macro descension API
Veykril Dec 3, 2023
903086c
Implicit format args support
Veykril Dec 5, 2023
918ed50
Resolve implicit format args in syntax highlighting
Veykril Dec 5, 2023
42eaebe
Add test for implicit format args support through nested macro call
Veykril Dec 5, 2023
7132bf5
Ignore strings in token trees in syntax highlighting
Veykril Dec 5, 2023
28e26b5
Allow navigation targets to be duplicated when the focus range lies i…
Veykril Dec 6, 2023
ee81231
Simplify
Veykril Dec 6, 2023
6f7dfac
Fix diagnostics panicking when resolving to different files due to ma…
Veykril Dec 6, 2023
747e8a5
make ParamLoweringMode accessible
JoJoDeveloping Dec 6, 2023
597cc1f
fix: Don't print proc-macro panic backtraces in the logs
Veykril Dec 6, 2023
a2d1c9c
internal: switch to `Arc::from_iter`
davidbarsky Nov 28, 2023
f0100e3
Publish lsp-server 0.7.5
Veykril Dec 6, 2023
037c10a
internal: Bump salsa
Veykril Dec 7, 2023
44abccd
Bump and unlock some dependencies
Veykril Dec 7, 2023
dc5ea83
feat: add trait_impl_reduntant_assoc_item diagnostic
Young-Flash Nov 28, 2023
288f9c6
update: make each trait_impl_reduntant_assoc_item into individual dia…
Young-Flash Dec 1, 2023
95f9d96
fix: change default diagnostic range into impl body
Young-Flash Dec 7, 2023
578af74
fix: correct calculation for fields in WideChar for line-specific pos…
roife Dec 7, 2023
397b68a
fix: correct existing tests for WideChar in lib 'line-index' and add …
roife Dec 7, 2023
7ecc0ca
feat: suggest `new` like fn first
mustakimali Dec 7, 2023
d271682
todo
mustakimali Dec 7, 2023
5442454
fix: test
mustakimali Dec 7, 2023
1c8b95c
save
mustakimali Dec 8, 2023
7b73f60
ops new in this codebase!
mustakimali Dec 8, 2023
eb7d6b7
builder methods
mustakimali Dec 8, 2023
c754f9f
fix: bug in extract_function: should not import ControlFlow in some c…
werifu Dec 4, 2023
16d8c3f
Fix panic with closure inside array len
HKalbasi Dec 7, 2023
daaf0b3
fix: make drop inlay hint more readable
Young-Flash Dec 5, 2023
c818c39
Show placeholder while run command gets runnables from server
minestarks Nov 14, 2023
476f711
Flip binexpr works for lhs binexpr
cardoso Aug 25, 2023
d593e4b
flip binexpr works for lhs cmp
cardoso Aug 25, 2023
477f45c
Stop offering private functions in completions
petr-tik Aug 20, 2023
38b82e0
fix: Fix item tree lowering pub(self) to pub()
Veykril Dec 8, 2023
b09a052
fix: resolve Self type references in delegate method assist
rmehri01 Oct 3, 2023
7ca6ae5
fix: preserve where clause in delegate method
rmehri01 Oct 5, 2023
e1cbcf0
fix: prefer keeping Self if it is in the same impl def
rmehri01 Oct 9, 2023
ee5e1e7
Make TraitEnvironment's constructor private
Veykril Dec 8, 2023
5febc8f
fix: Fix `concat_bytes!` expansion
Veykril Dec 8, 2023
f0c9f9b
WIP - Sort suggested imports by type for data types
jmintb Sep 13, 2023
3935da3
Implement function type matching
jmintb Nov 4, 2023
303edff
Cleanup
Veykril Dec 8, 2023
61036ec
fix: Fix token downmapping being quadratic
Veykril Dec 8, 2023
2508c29
Fallback to method resolution on unresolved field access with matchin…
Veykril Dec 8, 2023
fe65636
fix: Smaller spans for unresolved field and method diagnostics
Veykril Dec 8, 2023
43878c2
fix: Fix completion failing in format_args! with invalid template
Veykril Dec 8, 2023
3283a39
fix: tests
mustakimali Dec 9, 2023
ca88056
fix: test
mustakimali Dec 9, 2023
2d58983
undo debug change
mustakimali Dec 9, 2023
b081f24
save
mustakimali Dec 9, 2023
5150422
Refactor completion relevance calculation
mustakimali Dec 13, 2023
2e6c660
Remove unnecessary code and fix formatting
mustakimali Dec 13, 2023
0eb68eb
Refactor render.rs: Rename test function to
mustakimali Dec 13, 2023
444fa3b
Fix preferred order of constructors and builder
mustakimali Dec 13, 2023
1eac62a
Refactor render.rs and function.rs
mustakimali Dec 13, 2023
27e3cff
Merge branch 'master' into mo-usefull-order
mustakimali Dec 13, 2023
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ text-size = "1.1.1"
tracing = "0.1.40"
tracing-tree = "0.3.0"
tracing-subscriber = { version = "0.3.18", default-features = false, features = [
"registry",
"fmt",
"tracing-log",
"registry",
"fmt",
"tracing-log",
] }
triomphe = { version = "0.1.10", default-features = false, features = ["std"] }
xshell = "0.2.5"
Expand Down
7 changes: 6 additions & 1 deletion crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ pub struct CompletionRelevance {
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
/// This is set for type inference results
pub is_definite: bool,
/// Any other bonuses we want to add,
/// eg. bonus for good behavior!
pub bonus_score: u32,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -228,6 +231,7 @@ impl CompletionRelevance {
is_private_editable,
postfix_match,
is_definite,
bonus_score,
} = self;

// lower rank private things
Expand Down Expand Up @@ -269,7 +273,8 @@ impl CompletionRelevance {
if is_definite {
score += 10;
}
score

score + bonus_score
}

/// Returns true when the score for this threshold is above
Expand Down
77 changes: 76 additions & 1 deletion crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ide_db::{
imports::import_assets::LocatedImport,
RootDatabase, SnippetCap, SymbolKind,
};
use syntax::{AstNode, SmolStr, SyntaxKind, TextRange};
use syntax::{AstNode, SmolStr, SyntaxKind, SyntaxToken, TextRange};
use text_edit::TextEdit;

use crate::{
Expand Down Expand Up @@ -72,6 +72,10 @@ impl<'a> RenderContext<'a> {
self.completion.db
}

fn token(&self) -> &SyntaxToken {
&self.completion.token
}

fn source_range(&self) -> TextRange {
self.completion.source_range()
}
Expand Down Expand Up @@ -1176,6 +1180,7 @@ fn main() { let _: m::Spam = S$0 }
is_private_editable: false,
postfix_match: None,
is_definite: false,
bonus_score: 0,
},
trigger_call_info: true,
},
Expand All @@ -1202,6 +1207,7 @@ fn main() { let _: m::Spam = S$0 }
is_private_editable: false,
postfix_match: None,
is_definite: false,
bonus_score: 0,
},
trigger_call_info: true,
},
Expand Down Expand Up @@ -1280,6 +1286,7 @@ fn foo() { A { the$0 } }
is_private_editable: false,
postfix_match: None,
is_definite: false,
bonus_score: 0,
},
},
]
Expand Down Expand Up @@ -2002,6 +2009,60 @@ fn main() {
);
}

#[test]
fn colon_complete_preferred_order_relevances() {
check_relevance(
r#"
struct A;
struct ABuilder;
impl A {
fn foo(&self) {}
fn new_1(input: u32) -> A { A }
fn new_2() -> Self { A }
fn aaaabuilder() -> ABuilder { A }
}

fn test() {
let a = A::$0;
}
"#,
// preference:
// fn with no param that returns itself
// builder like fn
// fn with param that returns itself
expect![[r#"
fn new_2() [type_could_unify]
fn aaaabuilder() [type_could_unify]
fn new_1(…) [type_could_unify]
me foo(…) [type_could_unify]
"#]],
);

// Generic
check_relevance(
r#"
struct A<T>{item: T}
struct ABuilder;
impl A {
fn foo(&self) {}
fn new_1<T>(input: u32, l: T) -> A<T> { A }
fn new_2<T>() -> Self { A { item: <_>::default()} }
fn aaaabuilder<T>() -> ABuilder<T> { A }
}

fn test() {
let a = A::$0;
}
"#,
expect![[r#"
fn new_2() [type_could_unify]
fn aaaabuilder() [type_could_unify]
fn new_1(…) [type_could_unify]
me foo(…) [type_could_unify]
"#]],
);
}

#[test]
fn struct_field_method_ref() {
check_kinds(
Expand Down Expand Up @@ -2095,6 +2156,7 @@ fn foo() {
is_private_editable: false,
postfix_match: None,
is_definite: false,
bonus_score: 0,
},
},
]
Expand Down Expand Up @@ -2132,6 +2194,19 @@ fn main() {
),
lookup: "foo",
detail: "fn() -> S",
relevance: CompletionRelevance {
exact_name_match: false,
type_match: None,
is_local: false,
is_item_from_trait: false,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
is_definite: false,
bonus_score: 30,
},
ref_match: "&@92",
},
]
Expand Down
29 changes: 29 additions & 0 deletions crates/ide-completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn render(
},
exact_name_match: compute_exact_name_match(completion, &call),
is_op_method,
bonus_score: calculate_bonus(&ctx, func, db),
..ctx.completion_relevance()
});

Expand Down Expand Up @@ -153,6 +154,34 @@ fn render(
item
}

/// When typing `::` of a type, the preferred order is:
/// * Constructors: new like functions to be able to create the type,
/// * Builder Methods,
/// * Constructors that take args: Any other function that creates Self
/// * Regular methods & Associated functions
///
fn calculate_bonus(ctx: &RenderContext<'_>, func: hir::Function, db: &dyn HirDatabase) -> u32 {
if ctx.token().kind() != syntax::SyntaxKind::COLON2 || func.self_param(db).is_some() {
return 0;
}

let mut bonus = 0;

let has_args = !func.assoc_fn_params(db).is_empty();
let ret_type = func.ret_type(db);
if !has_args && !ret_type.is_unit() {
// fn() -> A
bonus += 30;
} else if has_args && !ret_type.is_unit() {
// fn(..) -> A
bonus += 20;
} else if !has_args && ret_type.display(db).to_string().ends_with("Builder") {
// -> [..]Builder
bonus += 10;
}
bonus
}

pub(super) fn add_call_parens<'b>(
builder: &'b mut Builder,
ctx: &CompletionContext<'_>,
Expand Down