-
Notifications
You must be signed in to change notification settings - Fork 141
Syntax Highlight using Prism #1091
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
Conversation
5e7d118 to
b6e85df
Compare
test/irb/test_irb.rb
Outdated
| def test_context_mode_ruby_box | ||
| omit if RUBY_VERSION < "4.0.0" | ||
| omit if RUBY_VERSION < '4.0.0' | ||
| pend if Prism::VERSION == '1.0.0' # Combination of ruby-4.0.0's Ruby::Box and prism-1.0.0 crashes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we require 1.1.0+ or something like that so we don't need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to 1.3.0 which is the one bundled in ruby-3.4.0.
This will also avoid this error
Could not find compatible versions
Because every version of rbs depends on prism >= 1.3.0
and Gemfile depends on prism = 1.0.0,
rbs cannot be used.
So, because Gemfile depends on rbs >= 0,
version solving has failed.
and the workaround I added in Gemfile
if RUBY_VERSION >= "3.0.0" && !is_truffleruby && ENV['PRISM_VERSION'] != '1.0.0'
b6e85df to
8570345
Compare
8570345 to
0b59d99
Compare
st0012
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. Thank you
|
Failed test (truffleruby) seems unrelated to this change. Failed in master too, maybe related to Ripper change. |
| colored = +'' | ||
| lvars_code = RubyLex.generate_local_variables_assign_code(local_variables) | ||
| code_with_lvars = lvars_code ? "#{lvars_code}\n#{code}" : code | ||
| result = Prism.parse_lex(code, scopes: [local_variables]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prism by default parses code as the latest version it currently supports. In irb you probably want to parse as the version that the user is currently running as.
You can do that by passing in version: RUBY_VERSION (or version: "current" on more recent prism for better error messages)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, I completely forgotten about version option.
In Ruby < 3.3.0, Prism.parse('code', version: :current) fails with
Prism.parse '1+2', version: 'current'
(irb):2:in `parse': invalid version: Requested to parse as `version: 'current'`; 3.2.2 is below the minimum supported syntax. (Prism::CurrentVersionError)
Is there a good way to use 'current', fallback to the nearest supported version?
(It's better to parse with the current version but it might not be such a big problem... maybe.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, prism cannot be used to parse ruby syntax below 3.3. I also added "current" only in prism 1.6.0 so I guess you need to bump the requirement in the gemspec.
Apart from bumping the minimum supported ruby version to 3.3 which might be a bit extreme, the best you can do is version: RUBY_VERSION >= "3.3.0" ? "current" : "3.3" I think
#1024
Migrate syntax highlight from Ripper to Prism
Syntax highlighting
Colorize from these three information:
Mix tokens and pseudo tokens created from error location and syntax tree which is prior than normal tokens.
Exceptional case
Symbol name can by IDENTIFIER, operator-token or keyword-token. It may not be placed after SYMBOL_BEGIN
Symbol and String colorize
Sometimes it's hard to distinguish symbol and string because both consists of same token types:
STRING_CONTENT,EMBEXPR_BEGIN,EMBEXPR_ENDandSTRING_END.Traversing syntax tree is the easiest way to colorize it correctly.
IRB colors defined method name blue. Doing this is really easy from syntax tree but hard from token.