Skip to content

Commit 1895716

Browse files
committed
Do not show references CodeLens for tests.
1 parent 06fbd69 commit 1895716

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

crates/ide/src/fn_references.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use hir::Semantics;
2+
use ide_db::RootDatabase;
3+
use syntax::{ast, ast::NameOwner, AstNode, SyntaxNode};
4+
5+
use crate::{runnables::has_test_related_attribute, FileId, FileRange};
6+
7+
pub(crate) fn find_all_methods(db: &RootDatabase, file_id: FileId) -> Vec<FileRange> {
8+
let sema = Semantics::new(db);
9+
let source_file = sema.parse(file_id);
10+
source_file.syntax().descendants().filter_map(|it| method_range(it, file_id)).collect()
11+
}
12+
13+
pub(crate) fn method_range(item: SyntaxNode, file_id: FileId) -> Option<FileRange> {
14+
ast::Fn::cast(item).and_then(|fn_def|{
15+
if has_test_related_attribute(&fn_def) {
16+
None
17+
} else {
18+
fn_def.name().map(|name| FileRange{ file_id, range: name.syntax().text_range() })
19+
}
20+
})
21+
}

crates/ide/src/lib.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mod join_lines;
3838
mod matching_brace;
3939
mod parent_module;
4040
mod references;
41+
mod fn_references;
4142
mod runnables;
4243
mod status;
4344
mod syntax_highlighting;
@@ -56,7 +57,7 @@ use ide_db::{
5657
symbol_index::{self, FileSymbol},
5758
LineIndexDatabase,
5859
};
59-
use syntax::{SourceFile, SyntaxKind, TextRange, TextSize};
60+
use syntax::{SourceFile, TextRange, TextSize};
6061

6162
use crate::display::ToNav;
6263

@@ -369,19 +370,9 @@ impl Analysis {
369370
})
370371
}
371372

372-
/// Finds all methods and free functions for the file.
373+
/// Finds all methods and free functions for the file. Does not return tests!
373374
pub fn find_all_methods(&self, file_id: FileId) -> Cancelable<Vec<FileRange>> {
374-
let res = self
375-
.file_structure(file_id)?
376-
.into_iter()
377-
.filter(|it| match it.kind {
378-
SyntaxKind::FN => true,
379-
_ => false,
380-
})
381-
.filter_map(|it| Some(FileRange { file_id, range: it.navigation_range }))
382-
.collect();
383-
384-
Ok(res)
375+
self.with_db(|db| fn_references::find_all_methods(db, file_id))
385376
}
386377

387378
/// Returns a short text describing element at position.

crates/ide/src/runnables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl TestAttr {
203203
///
204204
/// It may produce false positives, for example, `#[wasm_bindgen_test]` requires a different command to run the test,
205205
/// but it's better than not to have the runnables for the tests at all.
206-
fn has_test_related_attribute(fn_def: &ast::Fn) -> bool {
206+
pub(crate) fn has_test_related_attribute(fn_def: &ast::Fn) -> bool {
207207
fn_def
208208
.attrs()
209209
.filter_map(|attr| attr.path())

0 commit comments

Comments
 (0)