-
Notifications
You must be signed in to change notification settings - Fork 25
Annotate execute request source code with code location #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5d4bc6d
Add `code_location` field to execute requests
lionel- 06454c2
Parse with line directive
lionel- 9866a76
Add execute request test for code locations
lionel- d5d1415
Add leading padding whitespace
lionel- 37bcf8d
Check that location extents match supplied code
lionel- 306eff7
Properly check span of code location
lionel- 334f718
Rename `repl_debug.rs` to `console_debug.rs`
lionel- 76ff49e
Move annotation function to own file
lionel- 32e6465
Add unit tests for `annotate_input()`
lionel- 9384dd3
Fix issue with trailing newlines in executed code
lionel- a0671ba
Make `character` a u32 for consistency
lionel- f0a62ab
Add defensive checks for code location ranges
lionel- fa06408
Frontend now sends UTF-8 byte offsets
lionel- bf3b13b
Update header
lionel- 0745db5
Fix dependencies after rebase
lionel- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // | ||
| // console_annotate.rs | ||
| // | ||
| // Copyright (C) 2025 Posit Software, PBC. All rights reserved. | ||
| // | ||
|
|
||
| use amalthea::wire::execute_request::CodeLocation; | ||
| use biome_rowan::AstNode; | ||
|
|
||
| pub(crate) fn annotate_input(code: &str, location: CodeLocation) -> String { | ||
| let node = aether_parser::parse(code, Default::default()).tree(); | ||
| let Some(first_token) = node.syntax().first_token() else { | ||
| return code.into(); | ||
| }; | ||
|
|
||
| let line_directive = format!( | ||
| "#line {line} \"{uri}\"", | ||
| line = location.start.line + 1, | ||
| uri = location.uri | ||
| ); | ||
|
|
||
| // Leading whitespace to ensure that R starts parsing expressions from | ||
| // the expected `character` offset. | ||
| let leading_padding = " ".repeat(location.start.character as usize); | ||
|
|
||
| // Collect existing leading trivia as (kind, text) tuples | ||
| let existing_trivia: Vec<_> = first_token | ||
| .leading_trivia() | ||
| .pieces() | ||
| .map(|piece| (piece.kind(), piece.text().to_string())) | ||
| .collect(); | ||
|
|
||
| // Create new trivia with line directive prepended | ||
| let new_trivia: Vec<_> = vec![ | ||
| ( | ||
| biome_rowan::TriviaPieceKind::SingleLineComment, | ||
| line_directive.to_string(), | ||
| ), | ||
| (biome_rowan::TriviaPieceKind::Newline, "\n".to_string()), | ||
| ( | ||
| biome_rowan::TriviaPieceKind::Whitespace, | ||
| leading_padding.to_string(), | ||
| ), | ||
| ] | ||
| .into_iter() | ||
| .chain(existing_trivia.into_iter()) | ||
| .collect(); | ||
|
|
||
| let new_first_token = | ||
| first_token.with_leading_trivia(new_trivia.iter().map(|(k, t)| (*k, t.as_str()))); | ||
|
|
||
| let Some(new_node) = node | ||
| .syntax() | ||
| .clone() | ||
| .replace_child(first_token.into(), new_first_token.into()) | ||
| else { | ||
| return code.into(); | ||
| }; | ||
|
|
||
| new_node.to_string() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use amalthea::wire::execute_request::CodeLocation; | ||
| use amalthea::wire::execute_request::Position; | ||
| use url::Url; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn make_location(line: u32, character: u32) -> CodeLocation { | ||
| CodeLocation { | ||
| uri: Url::parse("file:///test.R").unwrap(), | ||
| start: Position { line, character }, | ||
| end: Position { line, character }, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_basic() { | ||
| let code = "x <- 1\ny <- 2"; | ||
| let location = make_location(0, 0); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_shifted_line() { | ||
| let code = "x <- 1\ny <- 2"; | ||
| let location = make_location(10, 0); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_shifted_character() { | ||
| let code = "x <- 1\ny <- 2"; | ||
| let location = make_location(0, 5); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_shifted_line_and_character() { | ||
| let code = "x <- 1\ny <- 2"; | ||
| let location = make_location(10, 5); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_with_existing_whitespace() { | ||
| let code = " x <- 1\n y <- 2"; | ||
| let location = make_location(0, 0); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_with_existing_whitespace_shifted() { | ||
| let code = " x <- 1\n y <- 2"; | ||
| let location = make_location(0, 2); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_with_existing_comment() { | ||
| let code = "# comment\nx <- 1"; | ||
| let location = make_location(0, 0); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_annotate_input_empty_code() { | ||
| let code = ""; | ||
| let location = make_location(0, 0); | ||
| let result = annotate_input(code, location); | ||
| insta::assert_snapshot!(result); | ||
| } | ||
| } |
4 changes: 2 additions & 2 deletions
4
crates/ark/src/repl_debug.rs → crates/ark/src/console_debug.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.