Skip to content
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

サンプルコード内の class Foo; endclass ;Foo end と表示される #178

Open
ysjj opened this issue Nov 8, 2022 · 0 comments

Comments

@ysjj
Copy link
Contributor

ysjj commented Nov 8, 2022

概要

Rubyリファレンスマニュアルのサンプルコードで、class Foo; endclass ;Foo end と表示されています。

具体例: Module#private_constant
image

ソースを確認したところ、 class Foo; end (相当)で書かれていました。
image

再現確認

irb 上の次のコードで再現することが確認できました。

irb> require 'bitclust'
irb> BitClust::SyntaxHighlighter.new('class A; end').highlight
=> "<span class=\"k\">class</span> ;<span class=\"nn\"></span><span class=\"o\"></span><span class=\"nc\">A</span> <span class=\"k\">end</span>"

原因分析

この状態は on_const;on_default で処理されることが関係しているようです。

def on_const(token, data)
case
when @stack.last == :class
@name_buffer << token
when @stack.last == :module
@name_buffer << token
when @stack.last == :symbol
data << "#{token}</span>"
@stack.pop
else
on_default(:on_const, token, data)
end
data
end

クラス名(and/or モジュール名; 以下、同様)をトリガーとする on_const ではトークンが data ではなく @name_buffer に保存されます。
ですが、次の ; をトリガーとする on_default ではトークンが data に保存されます。
この時点で data にはクラス名の前に ; が保存された状態となっています。

対処案

基本的に ; は改行と同様に扱えば良いと考えています。
つまり on_nl と同等の on_semicolon を設ければ良さそうです。

def on_nl(token, data)
case
when @name_buffer.empty?
return on_default(:on_nl, token, data)
when @stack.last == :module
name = @name_buffer.join
data << "<span class=\"nn\">#{name}</span>"
@stack.pop
@name_buffer.clear
when @stack.last == :class
namespace = @name_buffer.values_at(0..-3).join
operator = @name_buffer[-2]
name = @name_buffer.last
data << "<span class=\"nn\">#{namespace}</span>"
data << "<span class=\"o\">#{operator}</span>"
data << "<span class=\"nc\">#{name}</span>"
@stack.pop
@name_buffer.clear
end
on_default(:on_nl, token, data)
end

対処コード

フォーク リポジトリにて上述の対処案で修正されることを確認しています:

b853df0...ysjj:rurema-bitclust:da05f15

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

No branches or pull requests

1 participant