Commit graph

221 commits

Author SHA1 Message Date
Tsutomu Katsube
f370a31578 [ruby/irb] Suppress "literal string will be frozen in the future"
warning
(https://github.com/ruby/irb/pull/1019)

* Suppress "literal string will be frozen in the future" warning

Before change:

```console
$ ruby -W -I lib -e 'require "irb"; IRB.setup(nil); IRB::Irb.new.build_statement("1 + 2")'
/Users/zzz/src/github.com/ruby/irb/lib/irb.rb:1135: warning: literal string will be frozen in the future
```

After change:

```console
$ ruby -W -I lib -e 'require "irb"; IRB.setup(nil); IRB::Irb.new.build_statement("1 + 2")'
```

* Making build_statement not modify the given argument

Because improves readability and code quality.

Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>

---------

3da04b9786

Co-authored-by: tomoya ishida <tomoyapenguin@gmail.com>
2024-10-18 17:15:25 +00:00
Gert Goet
95abf679c5 [ruby/irb] Document infinite history
(https://github.com/ruby/irb/pull/1012)

As introduced in 824473e8

15e3f50c3f
2024-10-11 16:34:59 +00:00
Gert Goet
eccfb6e60c [ruby/irb] History refactors (https://github.com/ruby/irb/pull/1013)
* Extract logic save_history in separate helper

* Extract logic history_file in helper

* Allow for readonly history

52307f9026
2024-10-11 16:34:19 +00:00
Stan Lo
87fa75c5db [ruby/irb] Move parse_command method to Context
(https://github.com/ruby/irb/pull/993)

Since Context dictates whether a line is a command or an expression,
moving the parse_command method to Context makes the relationship
more explicit.

9a4487af61
2024-08-27 12:49:23 +00:00
Stan Lo
4a4e1bf357 [ruby/irb] Group class methods under class << self
(https://github.com/ruby/irb/pull/981)

cdaa356df2
2024-07-16 15:58:15 +00:00
tomoya ishida
de2d9c8e22 [ruby/irb] Allow assigning and using local variable name conflicting
with command
(https://github.com/ruby/irb/pull/961)

00603d470f
2024-06-30 17:13:27 +00:00
Stan Lo
fb2ea7084f [ruby/irb] Use flag instead of caller for debug's binding.irb
check
(https://github.com/ruby/irb/pull/947)

4a4d7a4279
2024-05-04 03:22:17 +00:00
Stan Lo
945a99e81a Sync IRB 241e061 2024-05-02 14:33:30 -04:00
Stan Lo
1000c27db8 [ruby/irb] Support IRB.conf[:BACKTRACE_FILTER]
(https://github.com/ruby/irb/pull/917)

* Use 'irbtest-' instead if 'irb-' as prefix of test files.

Otherwise IRB would mis-recognize exceptions raised in test files as
exceptions raised in IRB itself.

* Support `IRB.conf[:BACKTRACE_FILTER]``

This config allows users to customize the backtrace of exceptions raised
and displayed in IRB sessions. This is useful for filtering out library
frames from the backtrace.

IRB expects the given value to response to `call` method and return
the filtered backtrace.

6f6e87d769
2024-05-01 14:23:09 +00:00
tomoya ishida
8fb430c1da [ruby/irb] Restore MAIN_CONTEXT correctly
(https://github.com/ruby/irb/pull/937)

c41f460a70
2024-04-30 10:29:38 +00:00
Stan Lo
f16c6ac4fd [ruby/irb] Stop using ExtendCommandBundle internally
(https://github.com/ruby/irb/pull/925)

This module was used to extend both commands and helpers when they're not
separated. Now that they are, and we have a Command module, we should move
command-related logic to the Command module and update related references.

This will make the code easier to understand and refactor in the future.

f74ec97236
2024-04-20 18:55:54 +00:00
tomoya ishida
125e1ed5f7 [ruby/irb] Remove exit command workaround, handle IRB_EXIT in
debug_readline
(https://github.com/ruby/irb/pull/923)

* Remove exit and exti! command workaround when executed outside of IRB

Command was a method. It could be executed outside of IRB.
Workaround for it is no longer needed.

* Handle IRB_EXIT in debug mode

* Add exit and exit! command in rdbg mode

0b5dd6afd0
2024-04-20 07:45:41 +00:00
tomoya ishida
ff599aea7c [ruby/irb] Fix % escape in prompt format
(https://github.com/ruby/irb/pull/927)

08eee25d28
2024-04-18 14:46:55 +00:00
Lorenzo Zabot
2e978c2cb3 [ruby/irb] Prompt specifiers documentation improvements
(https://github.com/ruby/irb/pull/926)

e8ea8f253d
2024-04-18 10:33:23 +00:00
Stan Lo
04ba96e619 [ruby/irb] Allow defining custom commands in IRB
(https://github.com/ruby/irb/pull/886)

This is a feature that has been requested for a long time. It is now
possible to define custom commands in IRB.

Example usage:

```ruby
require "irb/command"

class HelloCommand < IRB::Command::Base
  description "Prints hello world"
  category "My commands"
  help_message "It doesn't do more than printing hello world."

  def execute
    puts "Hello world"
  end
end

IRB::Command.register(:hello, HelloCommand)
```

888643467c
2024-04-14 11:01:43 +00:00
Stan Lo
f1d9e895b9 [ruby/irb] Pass statements to Context#evaluate
(https://github.com/ruby/irb/pull/920)

This has a few benefits:

- We can keep hiding the evaluation logic inside the Context level, which
  has always been the convention until #824 was merged recently.
- Although not an official API, gems like `debug` and `mission_control-jobs`
  patch `Context#evaluate` to wrap their own logic around it. This implicit
  contract was broken after #824, and this change restores it.

In addition to the refactor, I also converted some context-level evaluation
tests into integration tests, which are more robust and easier to maintain.

b32aee4068
2024-04-12 12:01:03 +00:00
Stan Lo
38e3819be6 [ruby/irb] Add a workaround to make IRB work with debug's tests
(https://github.com/ruby/irb/pull/919)

eb442c4dda
2024-04-10 23:16:32 +00:00
tomoya ishida
6a505d1b59 [ruby/irb] Command implementation not by method
(https://github.com/ruby/irb/pull/824)

* Command is not a method

* Fix command test

* Implement non-method command name completion

* Add test for ExtendCommandBundle.def_extend_command

* Add helper method install test

* Remove spaces in command input parse

* Remove command arg unquote in help command

* Simplify Statement and handle execution in IRB::Irb

* Tweak require, const name

* Always install CommandBundle module to main object

* Remove considering local variable in command or expression check

* Remove unused method, tweak

* Remove outdated comment for help command arg

Co-authored-by: Stan Lo <stan001212@gmail.com>

---------

8fb776e379

Co-authored-by: Stan Lo <stan001212@gmail.com>
2024-04-10 16:52:53 +00:00
Joshua Broughton
f87e60f1f4 [ruby/irb] Filter backtrace before format in handle_exception
(https://github.com/ruby/irb/pull/916)

handle_exception now applies the filter_backtrace to exception
backtraces prior to formatting the lines with Exception#full_message

This fixes a bug in upstream projects, notably Rails, where the
backtrace filtering logic expects the lines to be formatted as
Exception#backtrace.

805ee008f9

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2024-04-05 16:25:52 +00:00
Artur
d85c24058d [ruby/irb] Remove misleading documentation
(https://github.com/ruby/irb/pull/906)

https://github.com/ruby/irb/issues/904

89bca01bba
2024-03-22 11:34:51 +00:00
Stan Lo
137b52a881 [ruby/irb] Use markdown format for docs
(https://github.com/ruby/irb/pull/890)

* Convert irb.rb's document into markdown format

* Hide should-be-private top-level methods from docs

* Skip xmp.rb's docs

* Declare lib/irb.rb's markup do it works in ruby/ruby too

* Ignore docs folder

e9a175e06b
2024-03-16 13:51:07 +00:00
Stan Lo
57ca5960ad [ruby/irb] Restructure workspace management
(https://github.com/ruby/irb/pull/888)

* Remove dead irb_level method

* Restructure workspace management

Currently, workspace is an attribute of IRB::Context in most use cases.
But when some workspace commands are used, like `pushws` or `popws`, a
workspace will be created and used along side with the original workspace
attribute.

This complexity is not necessary and will prevent us from expanding
multi-workspace support in the future.

So this commit introduces a @workspace_stack ivar to IRB::Context so IRB
can have a more natural way to manage workspaces.

* Fix pushws without args

* Always display workspace stack after related commands are used

61560b99b3
2024-03-01 15:51:29 +00:00
tomoya ishida
f0172fc7ee [ruby/irb] Remove workaround for empty lines in dynamic_prompt
(https://github.com/ruby/irb/pull/884)

820b9e8dd6
2024-02-23 12:32:59 +00:00
tomoya ishida
598b03648f [ruby/irb] Remove remaining frozen_string_literal: false in lib/
(https://github.com/ruby/irb/pull/883)

4bfdb23ae6
2024-02-23 12:32:50 +00:00
Stan Lo
07c774e85c [ruby/irb] Revamp help command
(https://github.com/ruby/irb/pull/877)

* Make help command display help for individual commands

Usage: `help [command]`

If the command is not specified, it will display a list of all available commands.

If the command is specified, it will display the banner OR description of the command.

If the command is not found, it will display a message saying that the command is not found.

* Rename test/irb/cmd to test/irb/command

* Add banner to edit and ls commands

* Promote help command in the help message

1. Make `show_cmds` an alias of `help` so it's not displayed in the help message
2. Update description of the help command to reflect `help <command>` syntax

* Rename banner to help_message

43a2c99f3f
2024-02-18 18:21:08 +00:00
Stan Lo
f5801e2bf4 [ruby/irb] Standardize command related names
(https://github.com/ruby/irb/pull/873)

* Replace ExtendCommand with Command and standardize command related names

1. Rename lib/irb/extend-command.rb to lib/irb/command.rb
2. Rename lib/irb/cmd/*.rb to lib/irb/command/*.rb
3. Rename test/irb/test_cmd.rb to test/irb/test_command.rb
4. Rename ExtendCommand to Command

* Alias ExtendCommand to Command and deprecate it

* Rename Command::Nop to Command::Base

* Not deprecate old constants just yet

* Add lib/irb/cmd/nop.rb back

462c1284af
2024-02-16 16:47:36 +00:00
Stan Lo
b315826377 [ruby/irb] Support repeating debugger input by passing empty input
to it
(https://github.com/ruby/irb/pull/856)

* Test IRB's behaviour with empty input

* Handle empty input and pass it to debugger

Since `rdbg` accepts empty input to repeat the previous command, IRB
should take empty input in `irb:rdbg` sessions and pass them to the
debugger.

Currently, IRB simply ignores empty input and does nothing. This commit
creates `EmptyInput` to represent empty input so it can fit into the
current IRB's input processing flow in `Irb#eval_input`.

0e9db419be
2024-02-16 16:12:54 +00:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
tomoya ishida
7af97dc71f [ruby/irb] Powerup show_source by enabling RubyVM.keep_script_lines
(https://github.com/ruby/irb/pull/862)

* Powerup show_source by enabling RubyVM.keep_script_lines

* Add file_content field to avoid reading file twice while show_source

* Change path passed to eval, don't change irb_path.

* Encapsulate source coloring logic and binary file check insode class Source

* Add edit command testcase when irb_path does not exist

* Memoize irb_path existence to reduce file existence check calculating eval_path

239683a937
2024-02-12 18:38:30 +00:00
tomoya ishida
06995eb45b [ruby/irb] Fix exit! command warning and method behavior
(https://github.com/ruby/irb/pull/868)

* Fix exit! command warning and method behavior

* Remove arg(0) from Kernel.exit and Kernel.exit!

372bc59bf5
2024-02-12 11:28:54 +00:00
Stan Lo
5c4657f883 [ruby/irb] Polish the exit! command and its tests
(https://github.com/ruby/irb/pull/867)

* Remove IRB.irb_exit! method

It's not necessary to introduce a new method just for the exit! command
at this moment.

* Rename ExitForcedAction to ForceExit

* Move force exit tests to a dedicated file

* Fix nested history saving with exit! command

Because we switched to use `Kernel#exit` instead of `exit!`, the outer
session's ensure block in `Irb#run` will be run, which will save the
history. This means the separate check to save history when force exiting
is no longer necessary.

* execute_lines helper should also capture IRB setup's output

This prevents setup warnings from being printed to test output
while allowing those output to be tested.

* Update readme

899d10ade1
2024-02-11 05:17:40 +00:00
Ignacio Chiazzo Cardarello
429eeb09f2 [ruby/irb] Introduce exit! command
(https://github.com/ruby/irb/pull/851)

* Added failing test for when writing history on exit

* Save history on exit

* Exit early when calling Kernel.exit

* use status 0 for kernel.exit

* Added test for nested sessions

* Update lib/irb.rb

---------

c0a5f31679

Co-authored-by: Stan Lo <stan001212@gmail.com>
2024-02-10 22:07:53 +00:00
Nuno Silva
1236a74023 [ruby/irb] Skip re-setup when creating a child session
(https://github.com/ruby/irb/pull/850)

06b2d00dd3
2024-02-01 12:12:06 +00:00
Eddie Lebow
fdb8f08639 [ruby/irb] Reword history file documentation and fix typo
(https://github.com/ruby/irb/pull/842)

bbabf818c7
2024-01-25 10:20:47 +00:00
ydah
819ae2c2c1 [ruby/irb] assigment ==> assignment
24c7694467
2024-01-25 09:58:16 +00:00
ydah
5f9f46a24c [ruby/irb] reseting ==> resetting
6209f06c72
2024-01-25 09:58:16 +00:00
ydah
9b1cc68b77 [ruby/irb] configuation ==> configuration
a27a511777
2024-01-25 09:58:16 +00:00
Eddie Lebow
34315510d3 [ruby/irb] Fix documentation typo, niL -> nil
79086a9dda
2024-01-22 04:42:37 +00:00
Stan Lo
4bdfc9070c [ruby/irb] Refactor exit command
(https://github.com/ruby/irb/pull/835)

* Remove unnecessary code from the exit command's implementation

1. The parameters of `IRB.irb_exit` were never used. But there are some
   libraries seem to call it with arguments + it's declared on the top-level
   IRB constant. So I changed the params to anonymous splat instead of removing them.
2. `Context#exit` was completely unnecessary as `IRB.irb_exit` doesn't use
   the `@irb` instance it passes. And since it's (or should be treated as)
   a private method, I simply removed it.
3. The `exit` command doesn't use the status argument it receives at all.
   But to avoid raising errors on usages like `exit 1`, I changed the argument to
   anonymous splat instead removing it.

* Make exit an actual command

* Update readme

452b543a65
2024-01-06 17:15:16 +00:00
Burdette Lamar
5384e5dfde [ruby/irb] Remove dead doc (https://github.com/ruby/irb/pull/819)
2d5a1afdf5
2023-12-25 21:12:49 +09:00
Burdette Lamar
4ad1007fd2 [ruby/irb] [DOC] Change indexes.rdoc to indexes.md
(https://github.com/ruby/irb/pull/812)

* Change indexes.rdoc to indexes.md

* Change indexes.rdoc to indexes.md

* Change indexes.rdoc to indexes.md

b1cd53cbf7
2023-12-20 19:11:02 +09:00
Stan Lo
d1cf793adc [ruby/irb] Remove documents about deprecated/WIP features and some
slight adjustments
(https://github.com/ruby/irb/pull/811)

6a9193e88b
2023-12-12 18:46:07 +00:00
Stan Lo
28a9a4275c [ruby/irb] Remove trailing space
This is required to fix ruby/ruby's CI

3c77213209
2023-12-12 11:27:39 +00:00
Burdette Lamar
35990cb38c [ruby/irb] [DOC] RDoc for module IRB
(https://github.com/ruby/irb/pull/738)

[DOC] RDoc for module IRB

f3a0626298
2023-12-12 10:34:38 +00:00
Stan Lo
f193f96d31 [ruby/irb] Page evaluation result's output
(https://github.com/ruby/irb/pull/784)

* Page evaluation result's output

This will make it easier to work with long output that exceeds the terminal's height.

* Use consistent TERM in rendering tests

This makes sure we get consistent result on all platforms.

4fedce93d3
2023-11-30 15:22:22 +00:00
Kasumi Hanazuki
5fc71feb6c [ruby/irb] Rescue errors from main.to_s/inspect when formatting
prompt
(https://github.com/ruby/irb/pull/791)

Currently, IRB just terminates if `main.to_s` raises while IRB
constructs the prompt string. This can easily happen if the user wants
to start an IRB session in the instance scope of an uninitialized
object, for example:

```
class C
  def initialize
    binding.irb
    @values = []
  end

  def to_s = @values.join(',')  # raises if uninitialized
end

C.new
```

This patch makes IRB rescue from such an exception and displays the
class name of the exception instead of `main.to_s` to indicate some
error has occurred.

We may display more detailed information about the exception, but this
patch chooses not to do so because 1) the prompt has limited space,
2) users can evaluate `to_s` in IRB to examine the error if they want,
and 3) obtaining the details can also raise, which requires nested
exception handling and can be complicated.

412ab26067
2023-11-28 14:05:31 +00:00
tomoya ishida
f6b292b5ca [ruby/irb] Fix exception(backtrace=nil) prints nothing
(https://github.com/ruby/irb/pull/782)

fa9ecf9a5b
2023-11-25 10:16:02 +00:00
tomoya ishida
11d7c75fb3 [ruby/irb] Handle handle_exception's exception
(https://github.com/ruby/irb/pull/780)

d42138c477
2023-11-23 17:33:13 +00:00
tomoya ishida
e344010465 [ruby/irb] Type based completion using Prism and RBS
(https://github.com/ruby/irb/pull/708)

* Add completor using prism and rbs

* Add TypeCompletion test

* Switchable completors: RegexpCompletor and TypeCompletion::Completor

* Add completion info to irb_info

* Complete reserved words

* Fix [*] (*) {**} and prism's change of KeywordParameterNode

* Fix require, frozen_string_literal

* Drop prism<=0.16.0 support

* Add Completor.last_completion_error for debug report

* Retrieve `self` and `Module.nesting` in more safe way

* Support BasicObject

* Handle lvar and ivar get exception correctly

* Skip ivar reference test of non-self object in ruby < 3.2

* BaseScope to RootScope, move method objects constant under Methods

* Remove unused Splat struct

* Drop deeply nested array/hash type calculation from actual object. Now, calculation depth is 1

* Refactor loading rbs in test, change preload_in_thread not to cache Thread object

* Use new option added in prism 0.17.1 to parse code with localvars

* Add Prism version check and warn when :type completor cannot be enabled

* build_type_completor should skip truffleruby (because endless method definition is not supported)

1048c7ed7a
2023-11-08 02:46:33 +00:00
Stan Lo
745879b5ed [ruby/irb] Minor refactors around irb.rb
(https://github.com/ruby/irb/pull/736)

* Remove dead method

* Simplify IRB.version

* Move private Irb methods together

* Centralise @CONF initialization/assignment in init.rb

* Move attr_* calls above initialize method

cf23be4395
2023-10-21 18:06:00 +00:00