Skip to content

Commit

Permalink
Trim range when inspecting end positions
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Dec 16, 2024
1 parent 4be4b61 commit 5e8dfd8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
36 changes: 26 additions & 10 deletions crates/lsp/src/handlers_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,13 @@ fn find_deepest_enclosing_logical_lines(node: RSyntaxNode, range: TextRange) ->
let logical_lines: Vec<RSyntaxNode> = iter
.map(|expr| expr.into_syntax())
.skip_while(|node| !node.text_range().contains(range.start()))
.take_while(|node| node.text_range().start() <= range.end())
.take_while(|node| node.text_trimmed_range().start() <= range.end())
.collect();

logical_lines
}

fn find_expression_lists(
node: &RSyntaxNode,
offset: TextSize,
inclusive: bool,
) -> Vec<RSyntaxNode> {
fn find_expression_lists(node: &RSyntaxNode, offset: TextSize, end: bool) -> Vec<RSyntaxNode> {
let mut preorder = node.preorder();
let mut nodes: Vec<RSyntaxNode> = vec![];

Expand All @@ -201,11 +197,11 @@ fn find_expression_lists(
continue;
};

let node_range = node.text_range();

let is_contained = if inclusive {
node_range.contains_inclusive(offset)
let is_contained = if end {
let trimmed_node_range = node.text_trimmed_range();
trimmed_node_range.contains_inclusive(offset)
} else {
let node_range = node.text_range();
node_range.contains(offset)
};

Expand Down Expand Up @@ -485,6 +481,26 @@ mod tests {
let output4 = client.format_document_range(&doc, range).await;
insta::assert_snapshot!(output4);

#[rustfmt::skip]
let (doc, range) = Document::doodle_and_range(
"<<1+1>>
2+2
",
);

let output5 = client.format_document_range(&doc, range).await;
insta::assert_snapshot!(output5);

#[rustfmt::skip]
let (doc, range) = Document::doodle_and_range(
"1+1
<<2+2>>
",
);

let output6 = client.format_document_range(&doc, range).await;
insta::assert_snapshot!(output6);

client
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/lsp/src/handlers_format.rs
expression: output5
---
1 + 1
2+2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/lsp/src/handlers_format.rs
expression: output6
---
1+1
2 + 2

0 comments on commit 5e8dfd8

Please sign in to comment.