Repository navigation
Fix comment handling when looking for commands - #507
Merged
davidlattimore merged 1 commit intoSep 7, 2026
Merged
davidlattimore merged 1 commit into
davidlattimore merged 1 commit into
Conversation
Comment lines were only skipped while evcxr looked for commands if they started
at column zero, so an indented comment before a command ended the search and the
command was then treated as Rust code. Checking
" // An indented comment\n :an_invalid_command" reported a syntax error at
the colon rather than "Unrecognised command".
The same check was too broad in the other direction: line.starts_with("//") also
matches "///", so a doc comment at column zero was skipped and never reached the
compiler, leaving the item it documented with no documentation. check_for_doc
only passes because its "///this is my struct" is indented.
from_original_user_code now trims leading whitespace before looking for "//", and
an outer doc comment no longer ends the search on its own. It is held as the
point where the code starts if code follows it, and dropped if a command follows,
which is what happened before. "//!" and "////" stay plain comments: an inner doc
comment would document the module that evcxr generates around the cell, and
"////" is not a doc comment to rustc either.
Collaborator
|
Thanks :) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Comment lines are only skipped while evcxr looks for commands if they start at column zero, so an indented comment before a command ends the search and the command is then treated as Rust code.
ctx.check(" // An indented comment\n :an_invalid_command")reports a syntax error at the colon instead of "Unrecognised command", and the same input throughexecutefails to compile rather than running the command.The check in
evcxr/src/code_block.rsis also too broad in the other direction.line.starts_with("//")matches///too, so a doc comment at column zero is skipped and never reaches the compiler, and the item it documents ends up with no documentation.check_for_doconly passes because its///this is my structhappens to be indented; put it at column zero and:doc MyStructprints the struct with the doc text gone.from_original_user_codenow trims leading whitespace before it looks for//, and an outer doc comment no longer ends the search on its own. It is held as the point where the code starts if code follows it, and dropped if a command follows, which is what happened before.//!and////stay plain comments: an inner doc comment would document the module evcxr generates around the cell rather than any of the user's code, and////is not a doc comment to rustc either.Tests:
cargo test -p evcxr -- --test-threads 1, on macOS with rustc 1.98.0: 32 unit tests and 48 integration tests pass.indented_comment_before_commandfinds no commands,doc_comment_stays_with_its_itemloses the///line,command_after_indented_commentgetserror 2:5-2:6instead oferror 2:1-2:20, andcheck_for_doc_starting_at_column_zerogetsctx\n\nstruct MyStruct(usize)with the doc text missing.doc_comment_before_command_is_droppedandcomments_that_arent_outer_doc_comments_are_droppedpin behaviour that has not changed and pass either way.cargo fmt --all -- --checkcargo clippy -p evcxr --all-targetsNo new dependencies, so
Cargo.lockandcackle.tomlare untouched.