Skip to content

Fix ruby new hash syntax #114

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

Merged
merged 6 commits into from
Feb 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ p=. _This files lists all changes in the CodeRay library since the 0.9.8 release

{{toc}}

h2. Changes in 1.0.9

* Fix Ruby scanner: Ruby 1.9 hash syntax @{ key: value }@ is highlighted correctly. [GH #106, thanks to Seth Vargo]

h2. Changes in 1.0.8

* add @:string/:char@, remove @:regexp/:function@ color from Terminal encoder [GH #29, thanks to Kyrylo Silin]
Expand Down
33 changes: 21 additions & 12 deletions lib/coderay/scanners/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,27 @@ def scan_tokens encoder, options
if !method_call_expected &&
match = scan(unicode ? /#{patterns::METHOD_NAME}/uo :
/#{patterns::METHOD_NAME}/o)
value_expected = false

kind = patterns::IDENT_KIND[match]
if kind == :ident
if match[/\A[A-Z]/] && !(match[/[!?]$/] || match?(/\(/))
kind = :constant
if kind == :ident && value_expected != :colon_expected && scan(/:(?!:)/)
value_expected = true
encoder.text_token match, :key
encoder.text_token ':', :operator
else
value_expected = false
if kind == :ident
if match[/\A[A-Z]/] && !(match[/[!?]$/] || match?(/\(/))
kind = :constant
end
elsif kind == :keyword
state = patterns::KEYWORD_NEW_STATE[match]
if patterns::KEYWORDS_EXPECTING_VALUE[match]
value_expected = match == 'when' ? :colon_expected : true
end
end
elsif kind == :keyword
state = patterns::KEYWORD_NEW_STATE[match]
value_expected = true if patterns::KEYWORDS_EXPECTING_VALUE[match]
value_expected = true if !value_expected && check(/#{patterns::VALUE_FOLLOWS}/o)
encoder.text_token match, kind
end
value_expected = true if !value_expected && check(/#{patterns::VALUE_FOLLOWS}/o)
encoder.text_token match, kind

elsif method_call_expected &&
match = scan(unicode ? /#{patterns::METHOD_AFTER_DOT}/uo :
Expand All @@ -119,9 +128,9 @@ def scan_tokens encoder, options
value_expected = check(/#{patterns::VALUE_FOLLOWS}/o)

# OPERATORS #
elsif !method_call_expected && match = scan(/ (\.(?!\.)|::) | (?: \.\.\.? | ==?=? | [,\(\[\{] )() | [\)\]\}] /x)
elsif !method_call_expected && match = scan(/ (\.(?!\.)|::) | ( \.\.\.? | ==?=? | [,\(\[\{] ) | [\)\]\}] /x)
method_call_expected = self[1]
value_expected = !method_call_expected && self[2]
value_expected = !method_call_expected && !!self[2]
if inline_block_stack
case match
when '{'
Expand Down Expand Up @@ -213,7 +222,7 @@ def scan_tokens encoder, options
encoder.text_token match, :integer

elsif match = scan(/ %=? | <(?:<|=>?)? | \? /x)
value_expected = true
value_expected = match == '?' ? :colon_expected : true
encoder.text_token match, :operator

elsif match = scan(/`/)
Expand Down