Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve Objective-C vs Mathematica lexer disambiguation #2103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

EthanArbuckle
Copy link

@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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants