Skip to content

Add support for Ruby string interpolation highlighting - #911

Open
Toshimaru (toshimaru) wants to merge 3 commits into
microsoft:mainfrom
toshimaru:ruby/interpolation-support
Open

Add support for Ruby string interpolation highlighting#911
Toshimaru (toshimaru) wants to merge 3 commits into
microsoft:mainfrom
toshimaru:ruby/interpolation-support

Conversation

@toshimaru

Copy link
Copy Markdown
Contributor

Follow-up to #849.

This pull request improves the Ruby syntax highlighting logic and its corresponding test cases. The main focus is on better handling of string interpolation and escape sequences, as well as simplifying and correcting method definition parsing.

String and interpolation handling

  • Enhanced parsing of double-quoted ("...") and backtick (`...`) strings to properly distinguish between escape sequences, interpolations (e.g., #{...}), and regular string content. Escaped interpolations (e.g., \#{...}) are now correctly recognized as plain strings, not variables.
  • Added new test cases in ruby.rb to verify correct handling of escaped interpolations and interpolations in backtick strings.

Before:

image

After:

image

Method definition parsing

  • Simplified the regular expression for parsing method definitions to handle both regular and singleton methods (e.g., def foo and def self.foo) in a single pattern, reducing code duplication.

Number literal parsing

  • Corrected the regular expression for number literals to ensure floats and integers are parsed accurately, preventing issues with constructs like 1..2 and 1.upto(2).

The two forms differed only by an optional `\w+\.` receiver prefix,
so they collapse into one regex with fewer redundant "def" prefix
tests on the common path.
The exponent suffix (?:e[+-]?[\d_]+)? was duplicated across separate
float and integer alternatives. Making the fractional part optional
instead expresses the same constraint with one shared exponent clause.
Inline the double-quote and backtick string loops so an interpolation
branch can highlight #{...} as variable, matching how shellscript.lsh
handles $var. Nested braces aren't supported, matching the same
limitation there.
@toshimaru
Toshimaru (toshimaru) marked this pull request as ready for review August 9, 2026 10:39
Copilot AI lite review requested due to automatic review settings August 9, 2026 10:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refines the Ruby LSH definition to better tokenize double-quoted and backtick strings by recognizing interpolations (#{...}) and distinguishing them from escaped interpolations (\#{...}), while also simplifying method definition matching and tightening numeric literal parsing to avoid swallowing . in ranges and method calls.

Changes:

  • Add interpolation recognition to " and ` string parsing, with correct handling of escaped interpolation (\#{...}).
  • Simplify def parsing to handle both regular and singleton methods with one regex.
  • Adjust numeric literal regex to reduce false positives around 1..2 and 1.upto(2) patterns, and extend highlighting tests accordingly.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/lsh/definitions/ruby.lsh Updates Ruby lexer rules for interpolated strings, method definitions, and numeric literals.
assets/highlighting-tests/ruby.rb Adds highlighting fixtures for escaped interpolation and interpolation in backtick strings.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants