Repository navigation
fix Reference Counting #2194 #2907
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
+570
−104
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| //! Server orchestration for runnable and reference-count CodeLens providers. | ||
|
|
||
| mod reference_count; | ||
| mod runnable; | ||
|
|
||
| use lsp_server::ErrorCode; | ||
| use lsp_server::RequestId; | ||
| use lsp_types::CodeLens; | ||
| use lsp_types::CodeLensParams; | ||
| use lsp_types::CodeLensRequest; | ||
| use lsp_types::Request as _; | ||
| use pyrefly_python::module::TextRangeWithModule; | ||
| use pyrefly_util::telemetry::ActivityKey; | ||
| use pyrefly_util::telemetry::EmptyResponseReason; | ||
|
|
||
| use super::Server; | ||
| use crate::commands::lsp::IndexingMode; | ||
| use crate::lsp::non_wasm::protocol::Message; | ||
| use crate::lsp::non_wasm::protocol::Response; | ||
| use crate::state::lsp::FindDefinitionItemWithDocstring; | ||
| use crate::state::lsp::FindPreference; | ||
| use crate::state::lsp::ImportBehavior; | ||
| use crate::state::lsp::ReferenceOptions; | ||
| use crate::state::state::Transaction; | ||
|
|
||
| impl Server { | ||
| /// Return eager runnable lenses and unresolved reference-count lenses for a Python file. | ||
| pub(super) fn code_lens( | ||
| &self, | ||
| transaction: &Transaction<'_>, | ||
| params: CodeLensParams, | ||
| ) -> Result<Option<Vec<CodeLens>>, EmptyResponseReason> { | ||
| let uri = ¶ms.text_document.uri; | ||
| if self.open_notebook_cells.read().contains_key(uri) { | ||
| return Ok(Some(Vec::new())); | ||
| } | ||
|
|
||
| let handle = self.make_handle_if_enabled(uri, Some(CodeLensRequest::METHOD.as_str()))?; | ||
| let mut lenses = runnable::code_lenses(self, transaction, &handle, uri)?; | ||
| if self.indexing_mode != IndexingMode::None { | ||
| lenses.extend(reference_count::unresolved_code_lenses( | ||
| transaction, | ||
| &handle, | ||
| uri, | ||
| )?); | ||
| } | ||
| Ok(Some(lenses)) | ||
| } | ||
|
|
||
| /// Resolve one visible reference-count lens on the references queue. | ||
| pub(super) fn resolve_code_lens<'a>( | ||
| &'a self, | ||
| request_id: RequestId, | ||
| transaction: &Transaction<'a>, | ||
| lens: &CodeLens, | ||
| activity_key: Option<ActivityKey>, | ||
| ) -> Result<(), EmptyResponseReason> { | ||
| let data = match reference_count::resolve_data(lens) { | ||
| Ok(data) => data, | ||
| Err(message) => { | ||
| self.connection.send(Message::Response(Response::new_err( | ||
| request_id, | ||
| ErrorCode::InvalidParams as i32, | ||
| message, | ||
| ))); | ||
| return Ok(()); | ||
| } | ||
| }; | ||
| if self.open_notebook_cells.read().contains_key(&data.uri) { | ||
| return Err(EmptyResponseReason::NotebookNotSupported); | ||
| } | ||
|
|
||
| let handle = | ||
| self.make_handle_if_enabled(&data.uri, Some(CodeLensRequest::METHOD.as_str()))?; | ||
| let path_remapper = self.path_remapper.clone(); | ||
| let source_uri = data.uri; | ||
| let source_uri_for_response = source_uri.clone(); | ||
| let lens = lens.clone(); | ||
| self.async_find_from_definition_helper( | ||
| request_id, | ||
| transaction, | ||
| handle, | ||
| &source_uri, | ||
| lens.range.start, | ||
| FindPreference { | ||
| import_behavior: ImportBehavior::StopAtRenamedImports, | ||
| ..Default::default() | ||
| }, | ||
| activity_key, | ||
| move |transaction, handle, definition, _telemetry, _telemetry_event| { | ||
| let FindDefinitionItemWithDocstring { | ||
| metadata, | ||
| definition_range, | ||
| module, | ||
| .. | ||
| } = definition; | ||
| Ok(transaction.find_global_references_from_definition( | ||
| *handle.sys_info(), | ||
| metadata, | ||
| TextRangeWithModule::new(module, definition_range), | ||
| ReferenceOptions::all(false), | ||
| )?) | ||
| }, | ||
| move |results| { | ||
| reference_count::resolve_code_lens( | ||
| lens, | ||
| &source_uri_for_response, | ||
| results, | ||
| path_remapper.as_ref(), | ||
| ) | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.