Skip to content

fix Reference Counting #2194#2907

Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2194
Open

fix Reference Counting #2194#2907
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2194

Conversation

@asukaminato0721
Copy link
Contributor

@asukaminato0721 asukaminato0721 commented Mar 26, 2026

Summary

Fixes #2194

The server now advertises textDocument/codeLens when indexing is enabled and returns CodeLens entries for class/function/method definitions using the existing global references engine.

Each lens shows N reference(s) and wires to VS Code’s built-in editor.action.showReferences, so clicking the count opens the references UI.

Test Plan

add LSP interaction test

@meta-cla meta-cla bot added the cla signed label Mar 26, 2026
@asukaminato0721 asukaminato0721 force-pushed the 2194 branch 2 times, most recently from 011421e to c711129 Compare March 26, 2026 12:02
@asukaminato0721 asukaminato0721 marked this pull request as ready for review March 26, 2026 12:04
Copilot AI review requested due to automatic review settings March 26, 2026 12:04
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

Adds CodeLens-based reference counts for Python symbols in the Pyrefly LSP when indexing is enabled, backed by the existing global references engine, and introduces an LSP interaction test to validate the behavior.

Changes:

  • Advertise textDocument/codeLens capability when indexing is enabled and handle CodeLensRequest on the server.
  • Generate CodeLens entries for class/function/method definitions with editor.action.showReferences wired up.
  • Add LSP interaction tests and test fixtures validating CodeLens reference counts.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pyrefly/lib/lsp/non_wasm/server.rs Advertises CodeLens capability and implements textDocument/codeLens request handling to return reference-count lenses.
pyrefly/lib/lsp/non_wasm/workspace.rs Adds code_lens flag to disabled-language-services gating for textDocument/codeLens.
pyrefly/lib/test/lsp/lsp_interaction/object_model.rs Adds a test client helper to send CodeLensRequest.
pyrefly/lib/test/lsp/lsp_interaction/mod.rs Registers new code_lens LSP interaction test module.
pyrefly/lib/test/lsp/lsp_interaction/code_lens.rs Adds tests verifying capability advertisement and reference-count CodeLens results.
pyrefly/lib/test/lsp/lsp_interaction/test_files/code_lens_references/{symbols.py,usage.py} Adds small fixtures used by the new CodeLens tests.

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

Comment on lines +4622 to +4625
self.find_reference_queue.queue_task(
TelemetryEventKind::FindFromDefinition,
Box::new(move |server, _telemetry, telemetry_event| {
telemetry_event.set_activity_key(activity_key);
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

find_reference_queue.queue_task expects a closure with signature FnOnce(&Server, &dyn Telemetry, &mut TelemetryEvent), but this new task closure takes 5 parameters (|server, _telemetry, telemetry_event, _, _|). This will not compile; update the closure to accept only the three expected arguments (and adjust captures accordingly).

Copilot uses AI. Check for mistakes.
Comment on lines +4662 to +4669
for (info, ranges) in local_results {
if let Some(uri) = module_info_to_uri(&info, path_remapper.as_ref()) {
for range in ranges {
locations.push(Location {
uri: uri.clone(),
range: info.to_lsp_range(range),
});
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

When building locations for the editor.action.showReferences command, notebook cell references aren’t remapped to cell URIs (unlike the existing references implementation, which maps file URIs to notebook cell URIs via snapshot_open_notebooks / to_cell_for_lsp). This will cause CodeLens “show references” to point at the notebook file path instead of the correct cell, or fail to navigate in notebook scenarios. Consider reusing the same notebook remapping logic used in async_find_references_helper when translating ModuleInfo + TextRange into LSP Locations.

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

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.

Reference Counting

2 participants