(https://github.com/ruby/irb/pull/761)
* Implement `history` command
Lists IRB input history with indices. Also aliased as `hist`.
* Add tests for `history` command
* Address feedback: `puts` with multiple arguments instead of `join`ing
* Address feedback: Handle nil from splitting an empty input string
* Refactor line truncation
* Add `-g` grep option to `history` command
* Add `history` command to README
* Remove unused `*args` parameter
* Allow spaces to be included in grep
* Allow `/` to be included in grep regex
* Handle `input` being an empty string
* Exclude "#{index}: " from matching the grep regex
* Add new line after joining
https://github.com/ruby/irb/commit/3f9eacbfa9
* Remove trailing spaces
* Migrate show_source tests to integration tests
Because show_source tests often need to define class and/or methods,
they can easily leak state to other tests. Changing them to integration
tests will ensure that they are run in a clean environment.
* Fix NoMethodError caused by SourceFinder#method_target
3c39f13397
(https://github.com/ruby/irb/pull/768)
When user enters irb:rdbg session, they don't get the same hint that the
`debug` gem provides, like
```
(rdbg) n # next command
```
This means that users may accidentally execute commands when they want to
retrieve the value of a variable.
So this commit adds a Reline output modifier to add a simiar hint:
```
irb:rdbg(main):002> n # debug command
```
It is not exactly the same as `debug`'s because in this case the importance
is to help users distinguish between value evaluation and debugger command
execution.
https://github.com/ruby/irb/commit/fdf24de851
(https://github.com/ruby/irb/pull/771)
I propose introducing the capability to set the IRB completion kinds via an environment variable, specifically `IRB_COMPLETOR=type`.
This feature aims to enhance the Rails console experience by allowing Rails users to specify their preferred completion more conveniently.
Currently, when using the Rails console, there's no straightforward way to globally set the type completion across a Rails application repository.
It's possible to configure this setting by placing a `.irbrc` file at the project root. However, using a .irbrc file is not ideal as it allows for broad configurations and can potentially affect the production environment.
My suggestion focuses on allowing users to set the completion to 'type' in a minimal.
This enhancement would be particularly beneficial for teams writing RBS in their Rails applications.
This type completer, integrated with RBS, would enhance completion accuracy, improving the Rails console experience.
https://github.com/ruby/irb/commit/032f6da25f
(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)
https://github.com/ruby/irb/commit/1048c7ed7a
refactored for future extension
(https://github.com/ruby/irb/pull/707)
* Move completion implementation to completion/regexp_completor for future extension
* Remove constant CompletionProc and PerfectMatchedProc and add a class method
* Move document display logic to InputCompletor. Each completor only need to implement `completion_caididates` and `doc_namespace`
* Move display_document logic to RelineInputMethod
* Use RegexpCompletor directly. Not through class method of InputCompletor.
* RegexpCompletor extends BaseCompletor, move back definition to completion.rb
* Move display_document test to input_method test
* Stop re-initialize completor on each completion phase
* Store completor to ReadlineInputMethod's iver
https://github.com/ruby/irb/commit/1e98521483
(https://github.com/ruby/irb/pull/684)
After this change, `RubyLex` will not interact with `Context` directly
in any way. This decoupling has a few benefits:
- It makes `RubyLex` easier to test as it no longer has a dependency on
`Context`. We can see this from the removal of `build_context` from
`test_ruby_lex.rb`.
- It will make `RubyLex` easier to understand as it will not be affected
by state changes in `Context` objects.
- It allows `RubyLex` to be used in places where `Context` is not available.
https://github.com/ruby/irb/commit/d5b262a076
(https://github.com/ruby/irb/pull/683)
* Add a test case for Ctrl-C handling
* Test symbol aliases with integration tests
There are a few places that also need to check symbol aliases before
`Irb#eval_input`. But since the current command test skip them, we
don't have test coverage on them.
* Move each_top_level_statement and readmultiline to Irb
This will save RubyLex from knowning information about commands and aliases.
https://github.com/ruby/irb/commit/69cb5b5615
(https://github.com/ruby/irb/pull/575)
* Support native integration with ruby/debug
* Prevent using multi-irb and activating debugger at the same time
Multi-irb makes a few assumptions:
- IRB will manage all threads that host sub-irb sessions
- All IRB sessions will be run on the threads created by IRB itself
However, when using the debugger these assumptions are broken:
- `debug` will freeze ALL threads when it suspends the session (e.g. when
hitting a breakpoint, or performing step-debugging).
- Since the irb-debug integration runs IRB as the debugger's interface,
it will be run on the debugger's thread, which is not managed by IRB.
So we should prevent the 2 features from being used at the same time.
To do that, we check if the other feature is already activated when
executing the commands that would activate the other feature.
https://github.com/ruby/irb/commit/d8fb3246be
(https://github.com/ruby/irb/pull/652)
1. Dynamically including `HistorySavingAbility` makes things unnecessarily
complicated and should be avoided.
2. Because both `Reline` and `Readline` use a single `HISTORY` constant
to store history data. When nesting IRB sessions, only the first IRB
session should handle history loading and saving so we can avoid
duplicating history.
3. History saving callback should NOT be stored in `IRB.conf` as it's
recreated every time `IRB.setup` is called, which would happen when
nesting IRB sessions.
https://github.com/ruby/irb/commit/0fef0ae160
(https://github.com/ruby/irb/pull/635)
* Remove unused InputMethod#initialize
The constructor takes a `file_name` argument, but it is never used. The
only input method that needs a file is `FileInputMethod`, which has its
own constructor to take a file object directly.
So the constructor in `InputMethod` is not needed and its child classes
don't need to call `super` in their constructors.
* Remove unused FileInputMethod#file_name
https://github.com/ruby/irb/commit/153b1e9d1c
(https://github.com/ruby/irb/pull/658)
* Decouple `edit` command from `show_source`
2 commands should not depend on each other. If `edit` command also needs
to find a source, the source finding logic should be extracted into a
separate class.
* Return nil if is not an actual file path
* Refactor SourceFinder
https://github.com/ruby/irb/commit/9790517a0c
(https://github.com/ruby/irb/pull/648)
IRB already has several features that rely on rdoc, such as:
- Autocompletion's document dialog
- Autocompletion's `PerfectMatchedProc`
- The `show_doc` command
- Easter egg
And we could use its pager more in the future too. So it makes sense to
declare rdoc as a dependency instead of relying on the one bundled with
Ruby.
https://github.com/ruby/irb/commit/4dffbb1dd3
(https://github.com/ruby/irb/pull/642)
* Define RelineInputMethod::HISTORY
The HistorySavingAbility module doesn't do anything if the input method
class doesn't define HISTORY.
- 3ac96be660/lib/irb/history.rb (L10)
- 3ac96be660/lib/irb/history.rb (L34)
This patch defines RelineInputMethod::HISTORY to avoid this.
* Improve history-saving's ability check
Instead of checking the existence of `input_method_class::HISTORY`, we should
make every input method class declare if it supports history saving or not.
Since the default value is `false`, it shouldn't break any custom input method
that inherits from `IRB::InputMethod`.
https://github.com/ruby/irb/commit/aec7a5b3f5
(https://github.com/ruby/irb/pull/623)
* Rename `ext/history.rb` to `ext/eval_history.rb`
To confusion with `lib/irb/history.rb`
* Add eval_history tests
* Rename eval_history's History to EvalHistory to avoid confusion
(https://github.com/ruby/irb/pull/613)
Since `IRB.conf[:SAVE_HISTORY]` is assigned with 1000 by default, history-saving
is a feature enabled by default. So it should not be treated as an extension,
which adds unnecessary complexity to the code.
(https://github.com/ruby/irb/pull/618)
* Test last value is assigned with measure enabled
* Remove unnecessary `result` variable
`Context#evaluate` always assigns the result of the evaluation to `_` so
we don't need to do it in `Irb#eval_input`.
* Move benchmarking logic into `Context#evaluate`
Current location of the benchmarking logic is too high up and includes
operations like command loading and argument transformation, which should
be excluded. So this commit moves it into `Context#evaluate` to reduce the
noise.
We don't move it further down to `Workspace#evaluate` because `Context`
is an argument of the measure block, which is not available in `Workspace`.
(https://github.com/ruby/irb/pull/615)
This makes sure `Context#evaluate` really just evaluates the input.
It will also make #575's implementation cleaner.
(https://github.com/ruby/irb/pull/515)
* Implement heredoc embdoc and string indentation with bugfix
* Fix test_ruby_lex's indentation value
* Add embdoc indent test
* Add workaround for lines==[nil] passed to auto_indent when exit IRB with CTRL+d
add new features easily
(https://github.com/ruby/irb/pull/500)
* Add nesting level parser for multiple use (indent, prompt, termination check)
* Rewrite RubyLex using NestingParser
* Add nesting parser tests, fix some existing tests
* Add description comment, rename method to NestingParser
* Add comments and tweak code to RubyLex
* Update NestingParser test
* Extract list of ltype tokens to constants
(https://github.com/ruby/irb/pull/598)
* Rename conf to irb_context
* Drop Nop#irb method because it's only used by irb/ext/loader.rb
We don't need to expose this method to all command classes, especially
when it's just an alias of `irb_context.irb`.
(https://github.com/ruby/irb/pull/599)
I received a `RegexpError` when I typed `::Array[`.
::Array[/Users/mi/ghq/github.com/ruby/irb/lib/irb/completion.rb:236:in `retrieve_completion_data': premature end of char-class: /^Array[/ (RegexpError)
(https://github.com/ruby/irb/pull/589)
* Allow `show_source` for private methods
Fix https://github.com/ruby/irb/pull/577
* Pend tests on TruffleRuby
It seems `eval(..., __LINE__ + 1)` does not work.
Other similar tests are also pended on TruffleRuby, so I think it
is acceptable.
* Use `private_method_defined?` instead of `defined?`
(https://github.com/ruby/irb/pull/576)
* Simplify each_top_level_statement, reduce instance vars
* Update lib/irb/ruby-lex.rb
Co-authored-by: Stan Lo <stan001212@gmail.com>
* Remove unused ltype from TestRubyLex#check_state response
* Remove unnecessary const path of TerminateLineInput
* Combine duplicated code state check into method
---------
https://github.com/ruby/irb/commit/172453cec4
Co-authored-by: Stan Lo <stan001212@gmail.com>
(https://github.com/ruby/irb/pull/583)
1. Make `RubyLex#set_input` simply assign the input block. This matches
the behavior of `RubyLex#set_prompt`.
2. Merge `RubyLex#set_input`'s IO configuration logic with `#set_auto_indent`
into `#configure_io`.
(https://github.com/ruby/irb/pull/574)
`MagicFile` was introduced around v0.9.6, which was like 14~15 years ago.
It was needed because back then we needed to read a file's magic comment
to determine the encoding of it, and read it with that encoding.
Commit: 3ee79e89
But now we expect files to be encoded in UTF-8 and don't specify encoding
through magic comments anymore, `MagicFile` can be retired.
(https://github.com/ruby/irb/pull/571)
* Simplify Locale#load
Instead of loading file content with `MagicFile` and then evaluting it,
we can just use `Kernel.load` to load the file.
* Remove unused optional argument
* Remove unused Locale#require and #toplevel_load
(https://github.com/ruby/irb/pull/573)
`MagicFile` was introduced around v0.9.6, which was like 14~15 years ago.
It was needed because back then we needed to read a file's magic comment
to determine the encoding of it, and read it with that encoding.
Commit: 3ee79e89ad
But now both EN and JA's help-message file are UTF-8 and have removed the
encoding comment, we don't need to open them with `MagicFile` anymore.
(https://github.com/ruby/irb/pull/569)
We don't have to load another file to define the legacy encoding aliases
map because there's only one definition of it. We can define it in
locale.rb directly.
(https://github.com/ruby/irb/pull/568)
In 3ee79e89ad,
`encoding_aliases.rb` was introduced to return the correct encoding object for
`ujis` and `euc` encodings.
However, the return value of `@@legacy_encoding_alias_map[@encoding_name]`
is always overridden by a second look up with `Encoding.find(@encoding_name)`.
So the logic didn't work as expected.
This commit fixes the problem.
(https://github.com/ruby/irb/pull/564)
The current method-redefining approach brings little benefit, makes it
harder to understand the code, and causes warnings like:
> warning: method redefined; discarding old execute
This patch simplifies it while displaying more helpful message when rdoc
couldn't be loaded.
<Class/Module>`
(https://github.com/ruby/irb/pull/562)
Instead of always printing methods inherited from Class or Module, IRB by
default should filter them out unless `<Class/Module>` is specified to be
either of those.
(https://github.com/ruby/irb/pull/559)
* Remove unnecessary command argument generation code
Now that all the supported Ruby versions handle splat args and splat kwargs,
we don't need that args generation code anymore.
* Remove unused command definition code
If we look at `@EXTEND_COMMANDS`, all commands are defined in a file, which
means the `if load_file` branch is always executed. Therefore we can drop
the else branch of that condition.
* Avoid defining unnecessary command methods
There's no need to define another command method just to call `Command.execute`.
Ruby implementations like JRuby and TruffleRuby already indicate their
compatibility target with RUBY_VERSION. We don't need to exclude
them from accepting keyword arguments as long as they target 2.7+.
https://github.com/ruby/irb/commit/bf20faa4e6
Co-authored-by: Kevin Menard <kevin@nirvdrum.com>
* Remove unused ATTR_TTY and ATTR_PLAIN constants
They were added in d7d26b51bf
But the references were removed in 1c76845cca
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
* Remove unused MethodExtender module
It was added in 6cc5d718d7
but it's not used anywhere.
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
* Remove unused IRB.irb_at_exit
It's not used after aaf4eb4e98
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
* Remove unused InputCompletor.ignored_modules
It was added in 88311ce3c8
but the reference was removed in 78c74d2425
* Remove unused TracerLoadError constant
This constant was added in cb50fa3738
but never referenced.
---------
https://github.com/ruby/irb/commit/7de0234325
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
includes word break characters
(https://github.com/ruby/irb/pull/523)
* Improve method completion for string and regexp that includes word break characters
* Remove completion-test's assert_not_include because candidates no longer include every possible methods
* Add comment about string's method completion regexp
Co-authored-by: Stan Lo <stan001212@gmail.com>
* Add comment about regexp's method completion regexp
Co-authored-by: Stan Lo <stan001212@gmail.com>
---------
https://github.com/ruby/irb/commit/aa8128c533
Co-authored-by: Stan Lo <stan001212@gmail.com>
(https://github.com/ruby/irb/pull/529)
Consider completion for this example: `foo.bar.b`
Without type information, it is hard to know the return value of the `bar`
method, so the current implementation interates through `ObjectSpace` to
get all possible candidates for the second method.
In small projects, the performance and accuracy are acceptable. But in
bigger projects, the performance is unacceptable and the accuracy is mostly
poor.
So this commit drops the support for chained methods' completion.
`IRB::Inspector#inspect_value` errors
(https://github.com/ruby/irb/pull/511)
**Before**
```
irb(main):001:0> c = Cat.new "foo"
(Object doesn't support #inspect)
=>
```
**After**
```
irb(main):001:0> c = Cat.new "foo"
An error occurred when inspecting the object: #<NoMethodError: undefined method `is_a?' for foo:Cat
if obj.is_a?(String)
^^^^^^>
Result of Kernel#inspect: #<Cat:0x0000000109090d80 @name="foo">
=>
```
Some background for this refactor:
1. Through a RubyLex instance's lifetime, the context passed to its methods
should be the same.
Given that `Context` is only initialised in `Irb#initialize`,
this should be true.
2. When `RubyLex` is initialised, the context object should be accessible.
This is also true in all 3 of `RubyLex.new`'s invocations.
With the above observations, we should be able to store the context in `RubyLex`
as an instance variable. And doing so will make `RubyLex`'s instance methods
easier to use and maintain.
https://github.com/ruby/irb/commit/5c8d3df2df
(https://github.com/ruby/irb/pull/498)
When the main object is frozen, `IRB` wraps a `SimpleDelegator` around it.
But because `SimpleDelegator` doesn't delegate private methods, methods like
`require_relative` or `const_get` would cause error, which are needed for
lazily loading commands.
This commit works around this limitation by avoiding those private method calls
when setting up command execution.
methods
(https://github.com/ruby/irb/pull/502)
* Remove unnecessary parameter defaults
These methods are always called with tokens specified. So their default
`@tokens` value is never used and is misleading.
* Remove unnecessary context default
* Require tokens for `RubyLex#check_state`
(https://github.com/ruby/irb/pull/504)
* Simplify `RubyLex#set_prompt`
It's optional argument is never used by any caller.
* Remove the optional `p` argument from `RubyLex#set_input`
The argument is only used in a test case, which can be easily replaced by
a block argument.
(https://github.com/ruby/irb/pull/489)
* Remove unnecessary Binding#source_location check
`Binding#source_location` was added in 2.6, which is the minimum supported
version now. So this check is no longer necessary.
* Remove unused IRB.delete_caller
This method was added in the earliest version of IRB:
f47808999d
But it's not currently referenced by anything. We can verify this with a
org-wide search result:
https://github.com/search?q=org%3Aruby+delete_caller&type=code
(https://github.com/ruby/irb/pull/350)
Simplify part of regex ``[_a-zA-Z0-9]`` with equivalent shorthand ``\w``.
Replace case-when with match ``$1`` or default value ``?"``, making intention more clear.
(https://github.com/ruby/irb/pull/478)
Given that `show_doc` already supports syntax like `String#gsub`, it
should be able to take it in non-string form too, like `edit` and
`show_source` do. This ensures users can have a consistent syntax on
argument between different commands.