Skip to content

Commit 63be641

Browse files
authored
Add full statement range support to roxygen @examples and @examplesIf (#965)
* Allow for customizing the `cursor` token * Add full statement range support to roxygen examples * Mention that this is a bit hand wavy * Add note about multiline strings * Add test related to multiple spaces before next tag
1 parent 2e92989 commit 63be641

File tree

4 files changed

+646
-168
lines changed

4 files changed

+646
-168
lines changed

crates/ark/src/fixtures/utils.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,25 @@ pub(crate) fn r_test_init() {
3737
}
3838

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

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

47-
// i.e. looking for `@` in something like `fn(x = @1, y = 2)`, and it treats the
48-
// `@` as the cursor position
49-
let cursor = b'@';
50-
5150
let mut offset = 0;
5251

52+
let cursor_for_replace = [cursor];
53+
let cursor_for_replace = str::from_utf8(&cursor_for_replace).unwrap();
54+
5355
for (line_row, line) in lines.into_iter().enumerate() {
5456
for (char_column, char) in line.as_bytes().into_iter().enumerate() {
5557
if char == &cursor {
56-
let x = x.replace("@", "");
58+
let x = x.replace(cursor_for_replace, "");
5759
let point = Point {
5860
row: line_row,
5961
column: char_column,
@@ -122,7 +124,7 @@ mod tests {
122124
#[test]
123125
#[rustfmt::skip]
124126
fn test_point_and_offset_from_cursor() {
125-
let (text, point, offset) = point_and_offset_from_cursor("1@ + 2");
127+
let (text, point, offset) = point_and_offset_from_cursor("1@ + 2", b'@');
126128
assert_eq!(text, "1 + 2".to_string());
127129
assert_eq!(point, Point::new(0, 1));
128130
assert_eq!(offset, 1);
@@ -135,7 +137,7 @@ mod tests {
135137
"fn(
136138
arg = 3
137139
)";
138-
let (text, point, offset) = point_and_offset_from_cursor(text);
140+
let (text, point, offset) = point_and_offset_from_cursor(text, b'@');
139141
assert_eq!(text, expect);
140142
assert_eq!(point, Point::new(1, 7));
141143
assert_eq!(offset, 11);

crates/ark/src/lsp/code_action/roxygen.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ mod tests {
186186
}
187187
}
188188

189+
fn roxygen_point_and_offset_from_cursor(text: &str) -> (String, Point, usize) {
190+
point_and_offset_from_cursor(text, b'@')
191+
}
192+
189193
fn roxygen_documentation_test(text: &str, position: Position) -> String {
190194
let mut actions = CodeActions::new();
191195

@@ -195,7 +199,7 @@ mod tests {
195199
.with_code_action_literal_support(true)
196200
.with_workspace_edit_document_changes(true);
197201

198-
let (text, point, offset) = point_and_offset_from_cursor(text);
202+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
199203
let document = Document::new(&text, None);
200204

201205
roxygen_documentation(
@@ -301,7 +305,7 @@ outer <- function(a, b = 2) {
301305
}
302306
";
303307

304-
let (text, point, offset) = point_and_offset_from_cursor(text);
308+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
305309
let document = Document::new(&text, None);
306310

307311
roxygen_documentation(
@@ -330,7 +334,7 @@ outer <- function(a, b = 2) {
330334
f@n <- function(a, b) {}
331335
";
332336

333-
let (text, point, offset) = point_and_offset_from_cursor(text);
337+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
334338
let document = Document::new(&text, None);
335339

336340
roxygen_documentation(
@@ -359,7 +363,7 @@ f@n <- function(a, b) {}
359363
fn@ <- function(a, b) {}
360364
";
361365

362-
let (text, point, offset) = point_and_offset_from_cursor(text);
366+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
363367
let document = Document::new(&text, None);
364368

365369
roxygen_documentation(
@@ -388,7 +392,7 @@ fn@ <- function(a, b) {}
388392
f@n <- function(a, b) {}
389393
";
390394

391-
let (text, point, offset) = point_and_offset_from_cursor(text);
395+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
392396
let document = Document::new(&text, None);
393397

394398
roxygen_documentation(
@@ -417,7 +421,7 @@ f@n <- function(a, b) {}
417421
f@n <- function(a, b) {}
418422
";
419423

420-
let (text, point, offset) = point_and_offset_from_cursor(text);
424+
let (text, point, offset) = roxygen_point_and_offset_from_cursor(text);
421425
let document = Document::new(&text, None);
422426

423427
roxygen_documentation(

crates/ark/src/lsp/handlers.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ pub(crate) fn handle_statement_range(
361361
let position = params.position;
362362
let point = convert_position_to_point(contents, position);
363363

364-
let row = point.row;
365-
366-
statement_range(root, contents, point, row)
364+
statement_range(root, contents, point)
367365
}
368366

369367
#[tracing::instrument(level = "info", skip_all)]

0 commit comments

Comments
 (0)