Skip to content

Commit

Permalink
Merge pull request #160 from krororo/support-symbol-list
Browse files Browse the repository at this point in the history
Support syntax highlighting `%i`, `%I`
  • Loading branch information
znz authored Feb 3, 2023
2 parents 426d54e + 1b6a2b8 commit 1211686
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/bitclust/syntax_highlighter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CompileError < Error
nl: nil, # \n
op: "o", # operator
period: "p", # .
qsymbols_beg: "ss", # %i(
qwords_beg: "sx", # %w(
rbrace: "p", # }
rbracket: "p", # ]
Expand All @@ -65,6 +66,7 @@ class CompileError < Error
semicolon: nil, # ;
sp: nil, # space
symbeg: "ss", # :
symbols_beg: "ss", # %I(
tlambda: "o", # ->
tlambeg: "p", # (->) {
tstring_beg: nil, # " (string")
Expand Down Expand Up @@ -344,7 +346,7 @@ def on_tstring_end(token, data)
case
when token == "'"
data << "#{token}</span>"
when [:qwords, :words].include?(@stack.last)
when %i[qwords words qsymbols symbols].include?(@stack.last)
@stack.pop
data << "#{token}</span>"
else
Expand All @@ -368,6 +370,20 @@ def on_words_beg(token, data)
data
end

def on_qsymbols_beg(token, data)
@stack.push(:qsymbols)
style = COLORS[:qsymbols_beg]
data << "<span class=\"#{style}\">#{token}"
data
end

def on_symbols_beg(token, data)
@stack.push(:symbols)
style = COLORS[:symbols_beg]
data << "<span class=\"#{style}\">#{token}"
data
end

def on_heredoc_beg(token, data)
@stack.push(:heredoc)
on_default(:on_heredoc_beg, token, data)
Expand Down
18 changes: 18 additions & 0 deletions test/test_syntax_highlighter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ def to_hash
assert_equal(expected, highlight(source))
end

test 'symbol list' do
source = <<~END
%i[hellow world].each {|s| p s }
END
expected = <<~END
<span class="ss">%i[hellow world]</span><span class="p">.</span><span class="nf">each</span> <span class="p">{</span><span class="o">|</span>s<span class="o">|</span> <span class="nb">p</span> s <span class="p">}</span>
END
assert_equal(expected, highlight(source))

source = <<~END
%I[hellow world].each {|s| p s }
END
expected = <<~END
<span class="ss">%I[hellow world]</span><span class="p">.</span><span class="nf">each</span> <span class="p">{</span><span class="o">|</span>s<span class="o">|</span> <span class="nb">p</span> s <span class="p">}</span>
END
assert_equal(expected, highlight(source))
end

test 'one liner module' do
source = <<~END
module Foo; end
Expand Down

0 comments on commit 1211686

Please sign in to comment.