Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crates/ark/src/fixtures/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@ pub(crate) fn r_test_init() {
}

pub fn point_from_cursor(x: &str) -> (String, Point) {
let (text, point, _offset) = point_and_offset_from_cursor(x);
// i.e. looking for `@` in something like `fn(x = @1, y = 2)`, and it treats the
// `@` as the cursor position
let (text, point, _offset) = point_and_offset_from_cursor(x, b'@');
(text, point)
}

pub fn point_and_offset_from_cursor(x: &str) -> (String, Point, usize) {
/// Looks for `cursor` in the text and interprets it as the user's cursor position
pub fn point_and_offset_from_cursor(x: &str, cursor: u8) -> (String, Point, usize) {
let lines = x.split("\n").collect::<Vec<&str>>();

// i.e. looking for `@` in something like `fn(x = @1, y = 2)`, and it treats the
// `@` as the cursor position
let cursor = b'@';

let mut offset = 0;

let cursor_for_replace = [cursor];
let cursor_for_replace = str::from_utf8(&cursor_for_replace).unwrap();

for (line_row, line) in lines.into_iter().enumerate() {
for (char_column, char) in line.as_bytes().into_iter().enumerate() {
if char == &cursor {
let x = x.replace("@", "");
let x = x.replace(cursor_for_replace, "");
let point = Point {
row: line_row,
column: char_column,
Expand Down Expand Up @@ -122,7 +124,7 @@ mod tests {
#[test]
#[rustfmt::skip]
fn test_point_and_offset_from_cursor() {
let (text, point, offset) = point_and_offset_from_cursor("1@ + 2");
let (text, point, offset) = point_and_offset_from_cursor("1@ + 2", b'@');
assert_eq!(text, "1 + 2".to_string());
assert_eq!(point, Point::new(0, 1));
assert_eq!(offset, 1);
Expand All @@ -135,7 +137,7 @@ mod tests {
"fn(
arg = 3
)";
let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = point_and_offset_from_cursor(text, b'@');
assert_eq!(text, expect);
assert_eq!(point, Point::new(1, 7));
assert_eq!(offset, 11);
Expand Down
16 changes: 10 additions & 6 deletions crates/ark/src/lsp/code_action/roxygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ mod tests {
}
}

fn roxygen_point_and_offset_from_cursor(text: &str) -> (String, Point, usize) {
point_and_offset_from_cursor(text, b'@')
}

fn roxygen_documentation_test(text: &str, position: Position) -> String {
let mut actions = CodeActions::new();

Expand All @@ -195,7 +199,7 @@ mod tests {
.with_code_action_literal_support(true)
.with_workspace_edit_document_changes(true);

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down Expand Up @@ -301,7 +305,7 @@ outer <- function(a, b = 2) {
}
";

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down Expand Up @@ -330,7 +334,7 @@ outer <- function(a, b = 2) {
f@n <- function(a, b) {}
";

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down Expand Up @@ -359,7 +363,7 @@ f@n <- function(a, b) {}
fn@ <- function(a, b) {}
";

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down Expand Up @@ -388,7 +392,7 @@ fn@ <- function(a, b) {}
f@n <- function(a, b) {}
";

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down Expand Up @@ -417,7 +421,7 @@ f@n <- function(a, b) {}
f@n <- function(a, b) {}
";

let (text, point, offset) = point_and_offset_from_cursor(text);
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
let document = Document::new(&text, None);

roxygen_documentation(
Expand Down
4 changes: 1 addition & 3 deletions crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,7 @@ pub(crate) fn handle_statement_range(
let position = params.position;
let point = convert_position_to_point(contents, position);

let row = point.row;

statement_range(root, contents, point, row)
statement_range(root, contents, point)
}

#[tracing::instrument(level = "info", skip_all)]
Expand Down
Loading