crystal-libraries-needed icon indicating copy to clipboard operation
crystal-libraries-needed copied to clipboard

Pry for Crystal

Open drujensen opened this issue 7 years ago • 13 comments

drujensen avatar May 15 '17 19:05 drujensen

ICR exists. There's obviously some differences since crystal is a compiled language. I don't think it lets you use vim inside of it like pry.

elorest avatar May 15 '17 20:05 elorest

Honestly, I'd be happy with it if I could view source code or documentation for a class/module/method. Being able to "cd" into a construct (whether class or instantiated object) is incredibly helpful when debugging.

TheLonelyGhost avatar May 17 '17 04:05 TheLonelyGhost

I actually started working on a way to do this in ICR. The libreadline library actually has a lot of really useful features that would help us to achieve this. Also, somehow integrating lldb would as well. The issue with the readline is that it's lacking in crystal. I made an issue for one part https://github.com/crystal-lang/crystal/issues/4067 which is macOS related, but that prevents me from moving forward.

I'd like to tackle this again if anyone wants to jump on board with me!

jwoertink avatar May 19 '17 23:05 jwoertink

That would be cool. If I remember from the issue though they claim it's related to the library for ReadLine in C rather than Crystal

On May 19, 2017 5:43 PM, "Jeremy Woertink" [email protected] wrote:

I actually started working on a way to do this in ICR. The libreadline library actually has a lot of really useful features that would help us to achieve this. Also, somehow integrating lldb would as well. The issue with the readline is that it's lacking in crystal. I made an issue for one part crystal-lang/crystal#4067 https://github.com/crystal-lang/crystal/issues/4067 which is macOS related, but that prevents me from moving forward.

I'd like to tackle this again if anyone wants to jump on board with me!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/johnjansen/crystal-libraries-needed/issues/17#issuecomment-302834222, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHmpFAivxUM9mGPBU1xQ2492DV4wy-Sks5r7ikkgaJpZM4NblLj .

elorest avatar May 20 '17 00:05 elorest

Yeah, that's what they say; however, similar code runs in other languages. I don't think it's a readline issue, but I don't know the library well enough to be 100% sure.

jwoertink avatar May 20 '17 00:05 jwoertink

@elorest For example Haskell is compiled too and there is really pretty interactive env for that. What is different from Crystal?

schovi avatar Jun 06 '17 20:06 schovi

@schovi GHC for Haskell has two modes, compiled and interpreted. runghc and ghci use the latter.

citizen428 avatar Jun 09 '17 08:06 citizen428

@citizen428 thanks for explanation!

schovi avatar Jun 09 '17 09:06 schovi

@jwoertink You could try using fancyline instead of libreadline.

Sija avatar Jun 09 '17 09:06 Sija

AFAIK readline (or fancyline) allows you to only interrupt execution and read/evaluate an interactive command (aka repl). binding.pry in ruby is much more than just readline. Let's look at the example:

require 'pry'

class A
end

def ruby_method1
end

def ruby_method2(a, b)
  binding.pry # <--- execution stopped here
end

ruby_method2 3, "a"
[1] pry(main)> a
=> 3
[2] pry(main)> b
=> "a"
[3] pry(main)> A.new
=> #<A:0x007f93e06910d8>
[4] pry(main)> ruby_method1
=> nil
[5] pry(main)> c
NameError: undefined local variable or method `c' for main:Object
from (pry):7:in `ruby_method2'

here pry knows the execution context it is running in, so it can show you the values of method args, create an instance of available class A and call a method. Also, it knows that there is no var c, for example. This is achieved by the Binding class in ruby:

Objects of class Binding encapsulate the execution context at some particular place in the code and retain this context for future use. The variables, methods, value of self, and possibly an iterator block that can be accessed in this context are all retained. Binding objects can be created using Kernel#binding, and are made available to the callback of Kernel#set_trace_func.

If we could have something similar in Crystal, it would be really easy to implement analog of binding.pry. Take a look at the first pry's release in ruby: https://github.com/pry/pry/blob/4fcf81b97601945945f43311532c164a93b44d7c/lib/pry.rb

But creating Binding may look like a challenge for Crystal...

veelenga avatar Aug 26 '17 11:08 veelenga

I once built a proof of concept for C++ to make this possible: https://github.com/inspector-repl/inspector

Mic92 avatar Mar 27 '20 07:03 Mic92

I made a pure crystal library that doesn't rely on readline called Term::Reader if anyone would like to use that for something like this. Not sure about the rest of the pry functionality though.

watzon avatar Mar 27 '20 16:03 watzon

I made a pure crystal library that doesn't rely on readline called Term::Reader if anyone would like to use that for something like this. Not sure about the rest of the pry functionality though.

@watzon that's completely off-topic

Sija avatar Mar 27 '20 17:03 Sija