Skip to content

Commit

Permalink
fix(references): ignore declarations when specified (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 authored Dec 3, 2024
1 parent 649786e commit dce5db4
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/handlers/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub async fn references(
Some(value) => value,
};

let include_def = params.context.include_declaration;
let query = Query::new(&QUERY_LANGUAGE, "(capture) @cap").unwrap();
let mut cursor = QueryCursor::new();
let provider = TextProviderRope(&rope);
Expand All @@ -49,7 +50,13 @@ pub async fn references(
&provider,
&rope,
)
.map(|node| ts_node_to_lsp_location(uri, &node, &rope))
.filter_map(|node| {
if include_def || node.parent().is_some_and(|p| p.kind() == "parameters") {
Some(ts_node_to_lsp_location(uri, &node, &rope))
} else {
None
}
})
.collect(),
))
}
Expand All @@ -76,34 +83,46 @@ mod test {
#[case(
"(identifier) @variable",
Position { line: 0, character: 17 },
true,
&[((0, 13), (0, 22))]
)]
#[case(
r#"((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]*$"))"#,
Position { line: 0, character: 17 },
true,
&[((0, 14), (0, 23)), ((1, 9), (1, 18))]
)]
#[case(
r"(type_definition declarator: (type_identifier) @name) @definition.type",
Position { line: 0, character: 61 },
true,
&[((0, 54), (0, 70))]
)]
#[case(
r"(call_expression
function: (identifier) @function)",
Position { line: 0, character: 1 },
true,
&[]
)]
#[case(
&COMPLEX_FILE,
Position { line: 5, character: 25 },
true,
&[((5, 25), (5, 44)), ((11, 15), (11, 34)), ((17, 16), (17, 35))]
)]
#[case(
&COMPLEX_FILE,
Position { line: 12, character: 13 },
false,
&[((12, 12), (12, 30)), ((18, 16), (18, 34))]
)]
#[tokio::test(flavor = "current_thread")]
async fn capture_references(
#[case] input: &str,
#[case] position: Position,
#[case] include_declaration: bool,
#[case] ranges: &[Coordinate],
) {
// Arrange
Expand All @@ -119,7 +138,7 @@ function: (identifier) @function)",
.call(lsp_request_to_jsonrpc_request::<References>(
ReferenceParams {
context: ReferenceContext {
include_declaration: true,
include_declaration,
},
partial_result_params: PartialResultParams {
partial_result_token: None,
Expand Down

0 comments on commit dce5db4

Please sign in to comment.