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

process multi-line pastes as a single entity #202

Merged
merged 3 commits into from
Apr 2, 2021

Conversation

ccutrer
Copy link
Contributor

@ccutrer ccutrer commented Mar 16, 2021

this allows pasting leading-dot chained methods correctly:

class A
  def a; self; end
  def b; true; end
end

a = A.new

a
 .a
 .b

will properly return true instead of erroring on the .a line:

irb(main):001:1*     class A
irb(main):002:1*       def a; self; end
irb(main):003:0>     end
irb(main):004:0*
irb(main):005:0>     a = A.new
irb(main):006:0*
irb(main):007:0>     a
irb(main):008:0>      .a
irb(main):009:0>      .a
=> #<A:0x00007f984211fbe8>

I'd be happy to write a test, if you could give me some pointers of how to do so. The only thing I can figure to do is to shell out to irb itself, since this is dependent in Reline reading from stdin. Possibly even requiring a pty.

this allows pasting leading-dot chained methods correctly:

```ruby
class A
  def a; self; end
  def b; true; end
end

a = A.new

a
 .a
 .b
```

will properly return `true` instead of erroring on the `.a` line:

```
irb(main):001:1*     class A
irb(main):002:1*       def a; self; end
irb(main):003:0>     end
irb(main):004:0*
irb(main):005:0>     a = A.new
irb(main):006:0*
irb(main):007:0>     a
irb(main):008:0>      .a
irb(main):009:0>      .a
=> #<A:0x00007f984211fbe8>
```
@marcandre
Copy link
Member

Looks like a good addition 👍

@aycabta
Copy link
Member

aycabta commented Mar 22, 2021

I think it's a really great patch too, and a lot of people will be happy with it.

On the other hand, Reline uses "whether the buffer has received a series of keystrokes" as a criterion to determine whether it is pasting or not. So, if there is a delay in I/O due to a slow machine or over the network, it will assume that everything is being pasted. Therefore, with this implementation alone, there is a possibility that the code that should be evaluated immediately (e.g. a = 1) will be followed by a new line and will not be executed forever.

I'm currently writing an additional patch, so please wait a little longer.

@aycabta
Copy link
Member

aycabta commented Mar 25, 2021

I added a test example to this branch, but it uses yamatanooroti gem, which has crash problems in Ruby 3.0.0. I added a test example to this branch, but it uses yamatanooroti gem, which has crash problems in Ruby 3.0.0. We are investigating the cause, but for now, please use Ruby 2.7.2 and install gem install yamatanooroti vterm, then try rake test_yamatanooroti. You should see the test run. You can use this to add a test for your patch.

@aycabta
Copy link
Member

aycabta commented Mar 26, 2021

I've written a patch that evaluates top-level each statement at when multiple statements are pasted at once, d991d11.

If I paste below;

class A
  def b; self; end
  def c; true; end
end

a = A.new

a
  .b
  # aaa
  .c

(a)
  &.b()


class A def b; self; end; def c; true; end; end;
a = A.new
a
  .b
  # aaa
  .c
(a)
  &.b()

must be below;

irb(main):001:1* class A
irb(main):002:1*   def b; self; end
irb(main):003:1*   def c; true; end
irb(main):004:0> end
=> :c
irb(main):005:0>
irb(main):006:0> a = A.new
=> #<A:0x0000562a844980e0>
irb(main):007:0>
irb(main):008:0> a
irb(main):009:0>   .b
irb(main):010:0>   # aaa
irb(main):011:0>   .c
=> true
irb(main):012:0>
irb(main):013:0> (a)
irb(main):014:0>   &.b()
=> #<A:0x0000562a844980e0>
irb(main):015:0>
irb(main):016:0>
irb(main):017:0> class A def b; self; end; def c; true; end; end;
=> :c
irb(main):018:0> a = A.new
=> #<A:0x0000562a83f73d80>
irb(main):019:0> a
irb(main):020:0>   .b
irb(main):021:0>   # aaa
irb(main):022:0>   .c
=> true
irb(main):023:0> (a)
irb(main):024:0>   &.b()
=> #<A:0x0000562a83f73d80>
irb(main):025:0>

I'll merge this Pull Request if you add a test. And I'll make a another Pull Request with the additional patch later.

@aycabta
Copy link
Member

aycabta commented Mar 31, 2021

@ccutrer Could you write a test with the 279155f that I added to this branch? Then I'll merge.

I've written about how to use it before:

Ruby 2.7.2 and install gem install yamatanooroti vterm, then try rake test_yamatanooroti. You should see the test run.

@aycabta
Copy link
Member

aycabta commented Mar 31, 2021

I and @mame fixed the gems and released new versions, so you can use Ruby 3.0.0 to write a test now.

@aycabta
Copy link
Member

aycabta commented Apr 2, 2021

I'm sorry, I added a test for multiline paste due to release schedule. I'll merge now. This feature is so great for many Ruby users. Thank you so much!

@aycabta aycabta merged commit e95df06 into ruby:master Apr 2, 2021
@ccutrer
Copy link
Contributor Author

ccutrer commented Apr 2, 2021

Heh, no apologies necessary, for getting done what I didn't get around to. Thank you so much! I'm very much looking forward to this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants