Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/textbringer/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
require "io/wait"

module Textbringer
Command = Struct.new(:name, :block, :doc)
class Command < Data.define(:name, :block, :doc, :source_location_proc)
def source_location
source_location_proc&.call || block.source_location
end
end

module Commands
include Utils
Expand All @@ -21,11 +25,11 @@ def self.[](name)
@command_table[name.intern]
end

def define_command(name, doc: "No documentation", &block)
def define_command(name, doc: "No documentation", source_location_proc: nil, &block)
name = name.intern
Commands.send(:define_method, name, &block)
Commands.send(:module_function, name)
Commands.command_table[name] = Command.new(name, block, doc)
Commands.command_table[name] = Command.new(name, block, doc, source_location_proc)
name
end
module_function :define_command
Expand Down
2 changes: 1 addition & 1 deletion lib/textbringer/commands/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def keymap_bindings(keymap)
end

def command_help(cmd)
file, line = *cmd.block.source_location
file, line = *cmd.source_location
s = format("%s:%d\n", file, line)
s << "-" * (Window.columns - 2) + "\n"
s << "#{cmd.name}"
Expand Down
4 changes: 3 additions & 1 deletion lib/textbringer/mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class << self

def self.define_generic_command(name, **options)
command_name = (name.to_s + "_command").intern
define_command(command_name, **options) do |*args|
define_command(command_name,
source_location_proc: -> { Buffer.current.mode.method(name).source_location rescue nil },
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

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

The bare rescue here catches all exceptions, including system-level exceptions like SystemExit and SignalException. Consider rescuing specific exceptions (e.g., rescue NoMethodError, NameError) to avoid suppressing unexpected errors that should propagate.

Suggested change
source_location_proc: -> { Buffer.current.mode.method(name).source_location rescue nil },
source_location_proc: -> { Buffer.current.mode.method(name).source_location rescue NameError, NoMethodError; nil },

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

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

The rescue modifier doesn't catch SystemExit because such a system-level exception doesn't inherit StandardError.

**options) do |*args|
begin
Buffer.current.mode.send(name, *args)
rescue NoMethodError => e
Expand Down
Loading