Skip to content

Add introspection utilities for Reline keybindings and IRB command to…#4

Open
Syati wants to merge 1 commit into
mainfrom
keybindgs
Open

Add introspection utilities for Reline keybindings and IRB command to…#4
Syati wants to merge 1 commit into
mainfrom
keybindgs

Conversation

@Syati
Copy link
Copy Markdown
Owner

@Syati Syati commented Jan 17, 2026

… display them

Copilot AI review requested due to automatic review settings January 17, 2026 13:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds introspection utilities for Reline keybindings and an IRB command to display them. The PR introduces functionality to inspect and format Reline keybindings in a human-readable way, along with an interactive IRB command interface.

Changes:

  • Added introspection module to query and format Reline keybindings with special key symbols
  • Created an IRB command to display keybindings in a formatted table
  • Added comprehensive test coverage for the introspection functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
lib/reline_pac/introspection.rb Implements keymap inspection and key sequence formatting with support for control keys, arrow keys, and special characters
lib/reline_pac/irb_commands.rb Defines an IRB command class to display keybindings in a formatted output
spec/reline_pac/introspection_spec.rb Provides test coverage for the introspection module's keymap functionality
lib/reline_pac.rb Integrates the new introspection module and conditionally loads IRB commands

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

case byte
when 27
'⎋'
when 0..31 # Control キー
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 78 contains Japanese text ('Control キー' meaning 'Control key'). For consistency with the rest of the codebase where English is used, this comment should be written in English.

Suggested change
when 0..31 # Control キー
when 0..31 # Control key

Copilot uses AI. Check for mistakes.
Comment on lines +22 to +25
it 'includes known keybindings' do
result = described_class.keymap
# Control-A should map to ed_move_to_beg
expect(result).to include('⌃A' => :ed_move_to_beg)
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test expects Control-A (⌃A) to always map to ed_move_to_beg, but this assumption depends on the default Reline configuration and may vary across different environments or Reline versions. This could make the test brittle. Consider either mocking the keymap data or testing the formatting logic independently of specific keybinding values.

Suggested change
it 'includes known keybindings' do
result = described_class.keymap
# Control-A should map to ed_move_to_beg
expect(result).to include('⌃A' => :ed_move_to_beg)
it 'includes and formats movement keybindings when present' do
result = described_class.keymap
# If ed_move_to_beg is present, its key should be formatted as a Control key
key_for_move_to_beg = result.key(:ed_move_to_beg)
if key_for_move_to_beg
expect(key_for_move_to_beg).to include('⌃')
end

Copilot uses AI. Check for mistakes.
Comment thread lib/reline_pac.rb
Comment on lines +8 to +9
# Load commands only if IRB is available
require_relative 'reline_pac/irb_commands' if defined?(IRB)
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional require 'require_relative 'reline_pac/irb_commands' if defined?(IRB)' may not work as intended in all scenarios. The file itself requires 'irb' at the top (line 3 of irb_commands.rb), which means if IRB is not available when this line executes, it will raise a LoadError. The conditional should either wrap the require statement in the irb_commands.rb file or use a rescue block here to handle cases where IRB is not installed.

Suggested change
# Load commands only if IRB is available
require_relative 'reline_pac/irb_commands' if defined?(IRB)
# Load commands only if IRB is available, and fail gracefully if IRB cannot be loaded
begin
require_relative 'reline_pac/irb_commands' if defined?(IRB)
rescue LoadError
# IRB is not installed or could not be loaded; skip IRB-specific commands
end

Copilot uses AI. Check for mistakes.

def execute(_arg)
bindings = RelinePac::Introspection.keymap
max_key_length = bindings.keys.map(&:length).max
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execute method doesn't handle the case where bindings is empty or max_key_length is nil. If keymap returns an empty hash, bindings.keys.map(&:length).max will return nil, and calling ljust on a string with nil as an argument will raise a TypeError.

Suggested change
max_key_length = bindings.keys.map(&:length).max
max_key_length = bindings.keys.map(&:length).max || 0

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +20
class Keybinds < ::IRB::Command::Base
category 'Context'
description 'Show Reline keybindings'

def execute(_arg)
bindings = RelinePac::Introspection.keymap
max_key_length = bindings.keys.map(&:length).max

bindings.each do |key, method|
puts "#{key.ljust(max_key_length + 2)} #{method}"
end
end
end
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases don't cover the IrbCommands::Keybinds class, which means the execute method and command registration logic lacks test coverage. Since other modules in the repo have corresponding test files, this command should also have tests.

Copilot uses AI. Check for mistakes.
config = Reline.core.config
bindings = {}

config.keymap.instance_variable_get(:@key_bindings).each do |key, method|
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses instance_variable_get to access Reline's internal @key_bindings, which is a brittle approach that depends on internal implementation details. If Reline changes its internal structure in future versions, this code will break. Consider checking if Reline provides a public API for accessing keybindings, or add error handling for when the instance variable doesn't exist.

Suggested change
config.keymap.instance_variable_get(:@key_bindings).each do |key, method|
keymap = config.keymap
key_bindings =
if keymap.respond_to?(:key_bindings)
keymap.key_bindings
elsif keymap.instance_variable_defined?(:@key_bindings)
keymap.instance_variable_get(:@key_bindings)
else
{}
end
key_bindings.each do |key, method|

Copilot uses AI. Check for mistakes.
Comment on lines +40 to +43
when "\e[F", "\eOF" then 'End'
when "\e[H", "\eOH" then 'Home'
when "\e[1~", "\e[7~" then 'Home' # rubocop:disable Lint/DuplicateBranch
when "\e[4~", "\e[8~" then 'End' # rubocop:disable Lint/DuplicateBranch
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rubocop:disable comments for Lint/DuplicateBranch on lines 42 and 43 are suppressing legitimate warnings. These case branches do have duplicate 'Home' and 'End' return values. The duplicate branches could be combined (e.g., when "\e[F", "\eOF", "\e[4~", "\e[8~" then 'End') to eliminate the need for these suppressions and improve maintainability.

Suggested change
when "\e[F", "\eOF" then 'End'
when "\e[H", "\eOH" then 'Home'
when "\e[1~", "\e[7~" then 'Home' # rubocop:disable Lint/DuplicateBranch
when "\e[4~", "\e[8~" then 'End' # rubocop:disable Lint/DuplicateBranch
when "\e[F", "\eOF", "\e[4~", "\e[8~" then 'End'
when "\e[H", "\eOH", "\e[1~", "\e[7~" then 'Home'

Copilot uses AI. Check for mistakes.
end
end

# IRB が読み込まれている場合のみ登録
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 24 is in Japanese while all other code and comments in the file are in English. For consistency and to ensure all team members can understand the codebase, comments should be written in English.

Suggested change
# IRB が読み込まれている場合のみ登録
# Register only if IRB has been loaded

Copilot uses AI. Check for mistakes.
Base automatically changed from custom_package to main January 19, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants