Skip to content

fix [lsp][completion] rank results based on compatibility in context #1653#2315

Open
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:1653
Open

fix [lsp][completion] rank results based on compatibility in context #1653#2315
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:1653

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #1653

Implemented compatibility-aware completion ordering for call-argument contexts by computing the expected argument type and demoting incompatible local/attribute suggestions

Test Plan

Added an LSP interaction test that asserts compatible suggestions are ranked ahead of incompatible ones

@meta-cla meta-cla bot added the cla signed label Feb 5, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review February 5, 2026 17:20
Copilot AI review requested due to automatic review settings February 5, 2026 17:20
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements compatibility-aware completion ordering to address issue #1653, where incompatible suggestions should be ranked lower than compatible ones in call-argument contexts. The implementation computes the expected argument type at call sites and demotes local variables and attributes that are incompatible with that type.

Changes:

  • Added type compatibility checking for completion items in call-argument contexts
  • Modified completion sorting to demote incompatible suggestions using a 'z' suffix in sort_text
  • Added LSP interaction test verifying compatible suggestions rank higher than incompatible ones

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pyrefly/lib/lsp/wasm/completion.rs Implements compatibility checking logic with helper methods, integrates expected type computation into completion flows for local variables and attributes, and applies demotion via sort_text manipulation
pyrefly/lib/test/lsp/lsp_interaction/completion.rs Adds test case verifying that compatible variables are ranked before incompatible ones in function call contexts

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +287 to +295
fn expected_call_argument_type(&self, handle: &Handle, position: TextSize) -> Option<Type> {
let (callables, chosen_overload_index, active_argument, _) =
self.get_callables_from_call(handle, position)?;
let callable = callables.get(chosen_overload_index)?.clone();
let params = Self::normalize_singleton_function_type_into_params(callable)?;
let arg_index = Self::active_parameter_index(&params, &active_argument)?;
let param = params.get(arg_index)?;
Some(param.as_type().clone())
}
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method calls Self::normalize_singleton_function_type_into_params (line 291) and Self::active_parameter_index (line 292), but these methods are not defined in the Transaction impl block in completion.rs. These methods exist in signature_help.rs but are not accessible from this module. This will cause a compilation error.

The methods need to either be:

  1. Moved to a shared location accessible by both modules
  2. Re-implemented in completion.rs
  3. Imported from signature_help.rs if they are made public

Looking at the codebase, similar methods exist in signature_help.rs (lines 272-286 and 288-300) and inlay_hints.rs (lines 72-87).

Copilot uses AI. Check for mistakes.
@github-actions

This comment has been minimized.

Copy link
Contributor

@kinto0 kinto0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!!

looks pretty good for now from a few nits and red CI. a refactor at some point making it clear where and when sort_text should added would also be appreciated (how do local variable completions compete with the other types of completions?), but that is a pre-existing issue in this diff.

@github-actions

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Feb 7, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[lsp][completion] rank results based on compatibility in context

2 participants