Skip to content

Conversation

@EthanArbuckle
Copy link
Contributor

@EthanArbuckle EthanArbuckle commented Jan 20, 2025

This PR fixes an issue where Rouge would incorrectly identify Objective-C code as Mathematica when encountering pointer dereference syntax in .m files.

Problem

The current lexer disambiguation logic for .m files checks for (* to identify Mathematica files. However, this pattern also appears in valid Objective-C code when using pointer dereference syntax, like:

if (*foo == 0) {

This causes Rouge to incorrectly identify such files as Mathematica, leading to incorrect syntax highlighting.

Solution

The fix improves the disambiguation logic by:

  • Making the Mathematica comment detection more specific - only matching (* when it appears at the start of a line (with optional whitespace)
  • Adding an additional Objective-C identifier for lines ending with an opening brace
  • Adding a test case to verify the fix

Related Issues

  • This issue was originally reported in GitLab: gitlab-org/gitlab#292672
  • The bug causes incorrect syntax highlighting in GitLab's UI

Screenshot 2025-01-20 at 13 14 49

fix: improve Objective-C vs Mathematica disambiguation for .m files

The previous logic would incorrectly identify Objective-C code as Mathematica
when encountering pointer dereference syntax like `if (*foo == 0)`, since it
looked similar to Mathematica's comment syntax `(* comment *)`.

This change improves the disambiguation by:
1. Only matching Mathematica comments that start at beginning of line
2. Adding an additional Objective-C identifier for braces at end of line
@jneen
Copy link
Member

jneen commented Jan 20, 2025

LGTM! Thanks for the fix! Disambiguation is always a bit of an approximate science - it's nice to see it tuned up a bit.

@EthanArbuckle
Copy link
Contributor Author

@jneen Hello! Following up on this PR - are there any other changes needed?

thanks!

@EthanArbuckle
Copy link
Contributor Author

@tancnle Hello! Apologies for the ping; I noticed you were most active on this repo as of recent.

May I ask for some eyes on this PR? It resolves a long-standing issue with Objective-C code that affects highlighting on GitLab.com. Relevant GitLab ticket: https://gitlab.com/gitlab-org/gitlab/-/issues/292672

I believe it's ready to go in current state, but i'm happy to address any feedback you have.

Thank you!

@tancnle
Copy link
Collaborator

tancnle commented Jun 12, 2025

Thanks @EthanArbuckle. The change LGTM.

@tancnle tancnle added this pull request to the merge queue Jun 12, 2025
Merged via the queue into rouge-ruby:master with commit 0332bba Jun 12, 2025
8 checks passed
xaviermignot pushed a commit to xaviermignot/rouge that referenced this pull request Jun 17, 2025
)

* fix: improve Objective-C vs Mathematica disambiguation for .m files

fix: improve Objective-C vs Mathematica disambiguation for .m files

The previous logic would incorrectly identify Objective-C code as Mathematica
when encountering pointer dereference syntax like `if (*foo == 0)`, since it
looked similar to Mathematica's comment syntax `(* comment *)`.

This change improves the disambiguation by:
1. Only matching Mathematica comments that start at beginning of line
2. Adding an additional Objective-C identifier for braces at end of line

* resolve disambiguation conflict, add more test cases
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.

3 participants