Repository navigation
Conversation
| def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?, local_variables: []) | ||
| return code unless colorable | ||
|
|
||
| code = code.gsub("\r\n", "\n") |
There was a problem hiding this comment.
Normalizing CRLF should be done AFTER Prism.parse_lex, not before. The original source code should be passed to the parser.
Replacing \r\n to \n will change the meaning of the code in some case, such as:
eval "%\r\r\na\r"
#=> "\na"
eval "%\r\r\na\r".gsub("\r\n", "\n")
#=> SyntaxError
eval "%\ra\r\r\n"
#=> "a"
eval "%\ra\r\r\n".gsub("\r\n", "\n")
#=> SyntaxErrorReline::Unicode.escape_for_print(code) is the one changing "\r" to "^M".
We can replace Reline::Unicode.escape_for_print(s) to a method call, calling a method defined in IRB::Color that escapes control characters except "\r\n".
|
Moved the normalization after parsing, as you suggested, and added both examples to the tests. I also fixed the failure with the latest Prism, which splits CRLF at the end of a comment across two tokens. The full suite passes locally with both released and development Prism. |
|
Hi @sjh9714! Thank you for working on this. I'm a developer and I tested this PR on my WSL (Windows Subsystem for Linux) environment to verify the fix. From: /tmp/crlf_test.rb:1 def desc^M |
|
Thanks for testing. I could only reproduce the I saved the example from #1037 with CRLF line endings and ran Could you check whether colors were on in your session, and whether IRB was loaded from this branch? If the maintainers would like the uncolored path covered as well, I can extend the change. |
Summary
Refs #1037.
CRLF still appears as
^Minside highlighted comments and multiline strings, including inshow_sourceand the source shown around a binding. Normalize CRLF before parsing colored source so those line endings render normally. Standalone carriage returns remain visible, and uncolored output is unchanged.Changes
Testing
bundle exec rake test TEST=test/irb/test_color.rbbundle exec rake test(410 tests, 0 failures/errors, 2 omissions)bundle exec rubocopbundle exec rake buildVerified on macOS with Ruby 4.0.6. The Windows/Linux and other Ruby version matrices were not run locally.