Class: RuVim::Dispatcher
- Inherits:
-
Object
- Object
- RuVim::Dispatcher
- Defined in:
- lib/ruvim/dispatcher.rb
Defined Under Namespace
Classes: ExCall
Instance Method Summary collapse
- #dispatch(editor, invocation) ⇒ Object
- #dispatch_ex(editor, line) ⇒ Object
-
#initialize(command_registry: CommandRegistry.instance, ex_registry: ExCommandRegistry.instance, command_host: GlobalCommands.instance) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
-
#parse_address(str, pos, editor) ⇒ Object
Parse an address at position pos in str.
- #parse_ex(line) ⇒ Object
-
#parse_range(raw, editor) ⇒ Object
Parse a range from the beginning of raw.
-
#parse_substitute(line) ⇒ Object
Parse a substitute command: s/pat/repl/flags Returns replacement:, flags_str: or nil.
Constructor Details
#initialize(command_registry: CommandRegistry.instance, ex_registry: ExCommandRegistry.instance, command_host: GlobalCommands.instance) ⇒ Dispatcher
Returns a new instance of Dispatcher.
9 10 11 12 13 |
# File 'lib/ruvim/dispatcher.rb', line 9 def initialize(command_registry: CommandRegistry.instance, ex_registry: ExCommandRegistry.instance, command_host: GlobalCommands.instance) @command_registry = command_registry @ex_registry = ex_registry @command_host = command_host end |
Instance Method Details
#dispatch(editor, invocation) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/ruvim/dispatcher.rb', line 15 def dispatch(editor, invocation) spec = @command_registry.fetch(invocation.id) ctx = Context.new(editor:, invocation:) @command_host.call(spec.call, ctx, argv: invocation.argv, kwargs: invocation.kwargs, bang: invocation.bang, count: invocation.count) rescue StandardError => e editor.echo_error("Error: #{e.message}") end |
#dispatch_ex(editor, line) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruvim/dispatcher.rb', line 23 def dispatch_ex(editor, line) raw = line.strip if raw.start_with?("!") command = raw[1..].strip invocation = CommandInvocation.new(id: "__shell__", argv: [command]) ctx = Context.new(editor:, invocation:) @command_host.ex_shell(ctx, command:) editor.enter_normal_mode return end # Parse range prefix range_result = parse_range(raw, editor) rest = range_result ? range_result[:rest] : raw # Try substitute on rest if (sub = parse_substitute(rest)) kwargs = sub.merge( range_start: range_result&.dig(:range_start), range_end: range_result&.dig(:range_end) ) invocation = CommandInvocation.new(id: "__substitute__", kwargs:) ctx = Context.new(editor:, invocation:) @command_host.ex_substitute(ctx, **kwargs) editor.enter_normal_mode return end parsed = parse_ex(rest) return if parsed.nil? spec = @ex_registry.fetch(parsed.name) argv = parsed.argv if spec.raw_args # Re-extract raw text after command name (preserving shell quoting) raw_rest = rest.strip.sub(/\A\S+\s*/, "") argv = raw_rest.empty? ? [] : [raw_rest] end validate_ex_args!(spec, argv, parsed.bang) invocation = CommandInvocation.new(id: spec.name, argv: argv, bang: parsed.bang) ctx = Context.new(editor:, invocation:) range_kwargs = {} if range_result range_kwargs[:range_start] = range_result[:range_start] range_kwargs[:range_end] = range_result[:range_end] end @command_host.call(spec.call, ctx, argv: argv, bang: parsed.bang, count: 1, kwargs: range_kwargs) rescue StandardError => e editor.echo_error("Error: #{e.message}") ensure editor.leave_command_line if editor.mode == :command_line end |
#parse_address(str, pos, editor) ⇒ Object
Parse an address at position pos in str. Returns [resolved_line_number, new_pos] or nil.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ruvim/dispatcher.rb', line 117 def parse_address(str, pos, editor) return nil if pos >= str.length ch = str[pos] base = nil new_pos = pos case ch when /\d/ # Numeric address m = str[pos..].match(/\A(\d+)/) return nil unless m base = m[1].to_i - 1 # convert 1-based to 0-based new_pos = pos + m[0].length when "." base = editor.current_window.cursor_y new_pos = pos + 1 when "$" base = editor.current_buffer.line_count - 1 new_pos = pos + 1 when "'" # Mark address mark_ch = str[pos + 1] return nil unless mark_ch if mark_ch == "<" || mark_ch == ">" sel = editor.visual_selection if sel base = mark_ch == "<" ? sel[:start_row] : sel[:end_row] else return nil end else loc = editor.mark_location(mark_ch) return nil unless loc base = loc[:row] end new_pos = pos + 2 when "+", "-" # Relative offset with implicit current line base = editor.current_window.cursor_y # Don't advance new_pos — the offset parsing below will handle +/- else return nil end # Parse trailing +N / -N offsets while new_pos < str.length offset_ch = str[new_pos] if offset_ch == "+" m = str[new_pos + 1..].to_s.match(/\A(\d+)/) if m base += m[1].to_i new_pos += 1 + m[0].length else base += 1 new_pos += 1 end elsif offset_ch == "-" m = str[new_pos + 1..].to_s.match(/\A(\d+)/) if m base -= m[1].to_i new_pos += 1 + m[0].length else base -= 1 new_pos += 1 end else break end end # Clamp to valid range max_line = editor.current_buffer.line_count - 1 base = [[base, 0].max, max_line].min [base, new_pos] end |
#parse_ex(line) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruvim/dispatcher.rb', line 76 def parse_ex(line) raw = line.strip return nil if raw.empty? tokens = Shellwords.shellsplit(raw) return nil if tokens.empty? head = tokens.shift bang = head.end_with?("!") name = bang ? head[0...-1] : head ExCall.new(name:, argv: tokens, bang:) rescue ArgumentError => e raise RuVim::CommandError, "Parse error: #{e.message}" end |
#parse_range(raw, editor) ⇒ Object
Parse a range from the beginning of raw. Returns range_end:, rest: or nil.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ruvim/dispatcher.rb', line 197 def parse_range(raw, editor) str = raw return nil if str.empty? # % = whole file if str[0] == "%" max_line = editor.current_buffer.line_count - 1 rest = str[1..].to_s return { range_start: 0, range_end: max_line, rest: rest } end # Try first address addr1 = parse_address(str, 0, editor) return nil unless addr1 line1, pos = addr1 if pos < str.length && str[pos] == "," # addr,addr range addr2 = parse_address(str, pos + 1, editor) if addr2 line2, pos2 = addr2 return { range_start: line1, range_end: line2, rest: str[pos2..].to_s } end end # Single address { range_start: line1, range_end: line1, rest: str[pos..].to_s } end |
#parse_substitute(line) ⇒ Object
Parse a substitute command: s/pat/repl/flags Returns replacement:, flags_str: or nil
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruvim/dispatcher.rb', line 93 def parse_substitute(line) raw = line.strip return nil unless raw.match?(/\As[^a-zA-Z]/) return nil if raw.length < 2 delim = raw[1] return nil if delim.nil? || delim =~ /\s/ i = 2 pat, i = parse_delimited_segment(raw, i, delim) return nil unless pat rep, i = parse_delimited_segment(raw, i, delim) return nil unless rep flags_str = raw[i..].to_s { pattern: pat, replacement: rep, flags_str: flags_str } rescue StandardError nil end |