rubychan / coderay

Fast and easy syntax highlighting for selected languages, written in Ruby.
http://coderay.rubychan.de/
Other
847 stars 115 forks source link

Invalid parsing of object, containing Procs and other nested objects #215

Open Mehonoshin opened 7 years ago

Mehonoshin commented 7 years ago

Hey there! I found the following issue, it does not highlight all fields of such object.

I understand, that it can be complex problem, but maybe anyone has an idea how to solve this?

s = '<Result:false <Skill {"policy.default.eval"=>#<Proc:0x007fc73d75cfb8@/Users/mexx/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/trailblazer-2.0.7/lib/trailblazer/operation/guard.rb:14 (lambda)>, "policy.default"=>nil, "result.policy.default"=><Result:true {} >, "model.class"=>Blog::Post(id: integer, title: string, body: text, published: boolean, created_at: datetime, updated_at: datetime), "model.action"=>:new, "model"=>#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, "result.model"=><Result:true {} >, "contract.default"=>#<Blog::Post::Contracts::Create:0x007fc73aa66f18 @fields={"title"=>"test", "body"=>"123"}, @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, @mapper=#<#<Class:0x007fc73aa66450>:0x007fc73aa5cf18 @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>>, @_changes={"title"=>true, "body"=>true}, @errors=#<Reform::Contract::Errors:0x007fc73aa542c8 @errors={:body=>["size cannot be less than 9", "size cannot be less than 9"]}>>, "contract.default.params"=>{:title=>"test", :body=>"123"}, "representer.default.class"=>false, "result.contract.default"=>#<Blog::Post::Contracts::Create:0x007fc73aa66f18 @fields={"title"=>"test", "body"=>"123"}, @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, @mapper=#<#<Class:0x007fc73aa66450>:0x007fc73aa5cf18 @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>>, @_changes={"title"=>true, "body"=>true}, @errors=#<Reform::Contract::Errors:0x007fc73aa542c8 @errors={:body=>["size cannot be less than 9", "size cannot be less than 9"]}>>} {:current_user=>true, "params"=>{:blog_post=>{:title=>"test", :body=>"123"}}} {"pipetree"=>[>operation.new,>policy.default.eval,>Nested(Blog::Post::Create::Present),>contract.default.validate,>persist.save,>notify!]}> >'

irb(main):008:0> CodeRay.scan(s, :ruby).term

=> "<\e[35mResult\e[0m:\e[1;36mfalse\e[0m <\e[1;34;4mSkill\e[0m {\e[31m\e[1;31m\"\e[0m\e[31mpolicy.default.eval\e[1;31m\"\e[0m\e[31m\e[0m=>\e[1;30m#<Proc:0x007fc73d75cfb8@/Users/mexx/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/trailblazer-2.0.7/lib/trailblazer/operation/guard.rb:14 (lambda)>, \"policy.default\"=>nil, \"result.policy.default\"=><Result:true {} >, \"model.class\"=>Blog::Post(id: integer, title: string, body: text, published: boolean, created_at: datetime, updated_at: datetime), \"model.action\"=>:new, \"model\"=>#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, \"result.model\"=><Result:true {} >, \"contract.default\"=>#<Blog::Post::Contracts::Create:0x007fc73aa66f18 @fields={\"title\"=>\"test\", \"body\"=>\"123\"}, @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, @mapper=#<#<Class:0x007fc73aa66450>:0x007fc73aa5cf18 @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>>, @_changes={\"title\"=>true, \"body\"=>true}, @errors=#<Reform::Contract::Errors:0x007fc73aa542c8 @errors={:body=>[\"size cannot be less than 9\", \"size cannot be less than 9\"]}>>, \"contract.default.params\"=>{:title=>\"test\", :body=>\"123\"}, \"representer.default.class\"=>false, \"result.contract.default\"=>#<Blog::Post::Contracts::Create:0x007fc73aa66f18 @fields={\"title\"=>\"test\", \"body\"=>\"123\"}, @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>, @mapper=#<#<Class:0x007fc73aa66450>:0x007fc73aa5cf18 @model=#<Blog::Post id: nil, title: nil, body: nil, published: nil, created_at: nil, updated_at: nil>>, @_changes={\"title\"=>true, \"body\"=>true}, @errors=#<Reform::Contract::Errors:0x007fc73aa542c8 @errors={:body=>[\"size cannot be less than 9\", \"size cannot be less than 9\"]}>>} {:current_user=>true, \"params\"=>{:blog_post=>{:title=>\"test\", :body=>\"123\"}}} {\"pipetree\"=>[>operation.new,>policy.default.eval,>Nested(Blog::Post::Create::Present),>contract.default.validate,>persist.save,>notify!]}> >\e[0m"

It seems that problem is related to parsing #<Proc:0x007fc73d75cfb8@/Users/mexx/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/trailblazer-2.0.7/lib/trailblazer/operation/guard.rb:14 (lambda)> Procs and other nested objects.

korny commented 7 years ago

It looks like you're trying to highlight the output of #inspect as Ruby code. However, this isn't valid Ruby syntax – so it's not really CodeRay's fault ;)

Mehonoshin commented 7 years ago

Yeah, but I'm also trying to solve an issue with higher-level tools, like PRY. They use CodeRay for such things highlighting: https://monosnap.com/file/YQVmVscdkzgIkp4holKmq3LhmpQRPe

And this results that the output is not very pretty.

I understand that the primary goal of your tool is highlighting of sources, but probably we can implement inspect highlighting too :)