Skip to content

Commit

Permalink
feat: completion list suggests constructor like & builder methods first
Browse files Browse the repository at this point in the history
  • Loading branch information
mustakimali committed Feb 12, 2024
1 parent 3770f73 commit c8f9815
Show file tree
Hide file tree
Showing 3 changed files with 477 additions and 8 deletions.
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_args: bool,
pub has_self_arg: 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_args {
// bump associated functions
fn_score += 1;
} else if asf.has_self_arg {
// downgrade methods (below Constructor)
fn_score = 1;
}
}

fn_score
})
.unwrap_or_default();

score
}

Expand Down
Loading

0 comments on commit c8f9815

Please sign in to comment.