Skip to content

Commit

Permalink
fix: improve Objective-C vs Mathematica disambiguation for .m files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
EthanArbuckle committed Jan 20, 2025
1 parent bf007b7 commit b65dcb5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rouge/guessers/disambiguation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ def match?(filename)
disambiguate '*.m' do
next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/)
next ObjectiveC if contains?('@"')

next Mathematica if contains?('(*')
# Opening brace at end of line
next ObjectiveC if matches?(/\s*{\s*$/)

# Objective-C may use dereferenced pointers in statements: `if (*foo == 0)`.
# This is similar to Mathmatica's comment syntax: `(* comment *)`.
# Disambiguate by checking for any amount of whitespace (or no whitespace) followed by (*
next Mathematica if matches?(/^\s*\(\*/)
next Mathematica if contains?(':=')

next Mason if matches?(/<%(def|method|text|doc|args|flags|attr|init|once|shared|perl|cleanup|filter)([^>]*)(>)/)
Expand Down
1 change: 1 addition & 0 deletions spec/lexers/objective_c_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
it 'guesses by source' do
assert_guess :filename => 'foo.h', :source => '@"foo"'
assert_guess :filename => 'foo.h', :source => '@implementation Foo'
assert_guess :filename => 'foo.m', :source => 'if (*bar == 0) {'
end
end
end

0 comments on commit b65dcb5

Please sign in to comment.