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
Add method references CodeLens #5928
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eeb40db
Add method references CodeLens
vsrs b7fda5f
Make method references CodeLens off by default.
vsrs 06fbd69
Make method references CodeLens lazy.
vsrs 1895716
Do not show references CodeLens for tests.
vsrs cd5eeb9
Add tests
vsrs 91da41b
Replace "No references" with "0 references".
vsrs File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,95 @@ | ||
//! This module implements a methods and free functions search in the specified file. | ||
//! We have to skip tests, so cannot reuse file_structure module. | ||
|
||
use hir::Semantics; | ||
use ide_db::RootDatabase; | ||
use syntax::{ast, ast::NameOwner, AstNode, SyntaxNode}; | ||
|
||
use crate::{runnables::has_test_related_attribute, FileId, FileRange}; | ||
|
||
pub(crate) fn find_all_methods(db: &RootDatabase, file_id: FileId) -> Vec<FileRange> { | ||
let sema = Semantics::new(db); | ||
let source_file = sema.parse(file_id); | ||
source_file.syntax().descendants().filter_map(|it| method_range(it, file_id)).collect() | ||
} | ||
|
||
fn method_range(item: SyntaxNode, file_id: FileId) -> Option<FileRange> { | ||
ast::Fn::cast(item).and_then(|fn_def| { | ||
if has_test_related_attribute(&fn_def) { | ||
None | ||
} else { | ||
fn_def.name().map(|name| FileRange { file_id, range: name.syntax().text_range() }) | ||
} | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::mock_analysis::analysis_and_position; | ||
use crate::{FileRange, TextSize}; | ||
use std::ops::RangeInclusive; | ||
|
||
#[test] | ||
fn test_find_all_methods() { | ||
let (analysis, pos) = analysis_and_position( | ||
r#" | ||
//- /lib.rs | ||
fn private_fn() {<|>} | ||
|
||
pub fn pub_fn() {} | ||
|
||
pub fn generic_fn<T>(arg: T) {} | ||
"#, | ||
); | ||
|
||
let refs = analysis.find_all_methods(pos.file_id).unwrap(); | ||
check_result(&refs, &[3..=13, 27..=33, 47..=57]); | ||
} | ||
|
||
#[test] | ||
fn test_find_trait_methods() { | ||
let (analysis, pos) = analysis_and_position( | ||
r#" | ||
//- /lib.rs | ||
trait Foo { | ||
fn bar() {<|>} | ||
fn baz() {} | ||
} | ||
"#, | ||
); | ||
|
||
let refs = analysis.find_all_methods(pos.file_id).unwrap(); | ||
check_result(&refs, &[19..=22, 35..=38]); | ||
} | ||
|
||
#[test] | ||
fn test_skip_tests() { | ||
let (analysis, pos) = analysis_and_position( | ||
r#" | ||
//- /lib.rs | ||
#[test] | ||
fn foo() {<|>} | ||
|
||
pub fn pub_fn() {} | ||
|
||
mod tests { | ||
#[test] | ||
fn bar() {} | ||
} | ||
"#, | ||
); | ||
|
||
let refs = analysis.find_all_methods(pos.file_id).unwrap(); | ||
check_result(&refs, &[28..=34]); | ||
} | ||
|
||
fn check_result(refs: &[FileRange], expected: &[RangeInclusive<u32>]) { | ||
assert_eq!(refs.len(), expected.len()); | ||
|
||
for (i, item) in refs.iter().enumerate() { | ||
let range = &expected[i]; | ||
assert_eq!(TextSize::from(*range.start()), item.range.start()); | ||
assert_eq!(TextSize::from(*range.end()), item.range.end()); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, yeah, I am still not happy about factoring here -- way to many logic happens in the handlers.
But it also is non-trivial to properly factor the logic given the two-phase requests.
I think there's a better solution here, but I haven't looked in the details yet. I expect we want to use exactly the same pattern for code lenses as for assists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reuse existing find_all_refs method and convert the results to LSP types. And that's all.
Perhaps we can (should) utilize the fact that Code Lens Request supports partial results. But it requires a lot of work and if you do not mind I'd prefer to leave it for another PR.