Skip to content

feat: completion list suggests constructor like & builder methods first #16117

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 3 commits into from
Feb 13, 2024
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
48 changes: 48 additions & 0 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub struct CompletionRelevance {
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
/// This is set for type inference results
pub is_definite: bool,
/// This is set for items that are function (associated or method)
pub function: Option<CompletionRelevanceFn>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -207,6 +209,24 @@ pub enum CompletionRelevancePostfixMatch {
Exact,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct CompletionRelevanceFn {
pub has_params: bool,
pub has_self_param: bool,
pub return_type: CompletionRelevanceReturnType,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CompletionRelevanceReturnType {
Other,
/// Returns the Self type of the impl/trait
DirectConstructor,
/// Returns something that indirectly constructs the `Self` type of the impl/trait e.g. `Result<Self, ()>`, `Option<Self>`
Constructor,
/// Returns a possible builder for the type
Builder,
}

impl CompletionRelevance {
/// Provides a relevance score. Higher values are more relevant.
///
Expand All @@ -231,6 +251,7 @@ impl CompletionRelevance {
postfix_match,
is_definite,
is_item_from_notable_trait,
function,
} = self;

// lower rank private things
Expand Down Expand Up @@ -275,6 +296,33 @@ impl CompletionRelevance {
if is_definite {
score += 10;
}

score += function
.map(|asf| {
let mut fn_score = match asf.return_type {
CompletionRelevanceReturnType::DirectConstructor => 15,
CompletionRelevanceReturnType::Builder => 10,
CompletionRelevanceReturnType::Constructor => 5,
CompletionRelevanceReturnType::Other => 0,
};

// When a fn is bumped due to return type:
// Bump Constructor or Builder methods with no arguments,
// over them tha with self arguments
if fn_score > 0 {
if !asf.has_params {
// bump associated functions
fn_score += 1;
} else if asf.has_self_param {
// downgrade methods (below Constructor)
fn_score = 1;
}
}

fn_score
})
.unwrap_or_default();

score
}

Expand Down
Loading