Class: RuVim::CompletionManager
- Inherits:
-
Object
- Object
- RuVim::CompletionManager
- Defined in:
- lib/ruvim/completion_manager.rb
Instance Method Summary collapse
- #cancel_incsearch_preview_if_any ⇒ Object
- #clear_command_line_completion ⇒ Object
- #clear_incsearch_preview_state(apply:) ⇒ Object
-
#clear_insert_completion ⇒ Object
--- Insert completion ---.
-
#command_line_complete ⇒ Object
--- Command-line completion ---.
- #history_move(delta) ⇒ Object
-
#incsearch_enabled? ⇒ Boolean
--- Incremental search preview ---.
-
#initialize(editor:, terminal:, verbose_logger: nil) ⇒ CompletionManager
constructor
A new instance of CompletionManager.
- #insert_complete(direction) ⇒ Object
- #load_history! ⇒ Object
-
#push_history(prefix, line) ⇒ Object
--- Command-line history ---.
- #reset_history_index! ⇒ Object
- #save_history! ⇒ Object
-
#trailing_keyword_fragment(prefix_text, window, buffer) ⇒ Object
--- Keyword helpers ---.
- #update_incsearch_preview_if_needed ⇒ Object
Constructor Details
#initialize(editor:, terminal:, verbose_logger: nil) ⇒ CompletionManager
Returns a new instance of CompletionManager.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruvim/completion_manager.rb', line 9 def initialize(editor:, terminal:, verbose_logger: nil) @editor = editor @terminal = terminal @verbose_logger = verbose_logger @cmdline_history = Hash.new { |h, k| h[k] = [] } @cmdline_history_index = nil @cmdline_completion = nil @insert_completion = nil @incsearch_preview = nil end |
Instance Method Details
#cancel_incsearch_preview_if_any ⇒ Object
227 228 229 |
# File 'lib/ruvim/completion_manager.rb', line 227 def cancel_incsearch_preview_if_any clear_incsearch_preview_state(apply: false) end |
#clear_command_line_completion ⇒ Object
128 129 130 |
# File 'lib/ruvim/completion_manager.rb', line 128 def clear_command_line_completion @cmdline_completion = nil end |
#clear_incsearch_preview_state(apply:) ⇒ Object
231 232 233 234 235 236 237 238 |
# File 'lib/ruvim/completion_manager.rb', line 231 def clear_incsearch_preview_state(apply:) return unless @incsearch_preview if !apply && @incsearch_preview[:origin] @editor.jump_to_location(@incsearch_preview[:origin]) end @incsearch_preview = nil end |
#clear_insert_completion ⇒ Object
--- Insert completion ---
134 135 136 |
# File 'lib/ruvim/completion_manager.rb', line 134 def clear_insert_completion @insert_completion = nil end |
#command_line_complete ⇒ Object
--- Command-line completion ---
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruvim/completion_manager.rb', line 107 def command_line_complete cmd = @editor.command_line return unless cmd.prefix == ":" ctx = ex_completion_context(cmd) return unless ctx matches = reusable_command_line_completion_matches(cmd, ctx) || ex_completion_candidates(ctx) case matches.length when 0 clear_command_line_completion @editor.echo("No completion") when 1 clear_command_line_completion cmd.replace_span(ctx[:token_start], ctx[:token_end], matches.first) else apply_wildmode_completion(cmd, ctx, matches) end update_incsearch_preview_if_needed end |
#history_move(delta) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ruvim/completion_manager.rb', line 84 def history_move(delta) cmd = @editor.command_line hist = @cmdline_history[cmd.prefix] return if hist.empty? @cmdline_history_index = if @cmdline_history_index.nil? delta.negative? ? hist.length - 1 : hist.length else @cmdline_history_index + delta end @cmdline_history_index = [[@cmdline_history_index, 0].max, hist.length].min if @cmdline_history_index == hist.length cmd.replace_text("") else cmd.replace_text(hist[@cmdline_history_index]) end update_incsearch_preview_if_needed end |
#incsearch_enabled? ⇒ Boolean
--- Incremental search preview ---
191 192 193 194 195 196 |
# File 'lib/ruvim/completion_manager.rb', line 191 def incsearch_enabled? return false unless @editor.command_line_active? return false unless ["/", "?"].include?(@editor.command_line.prefix) !!@editor.effective_option("incsearch") end |
#insert_complete(direction) ⇒ Object
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 |
# File 'lib/ruvim/completion_manager.rb', line 138 def insert_complete(direction) state = ensure_insert_completion_state return unless state matches = state[:matches] if matches.empty? @editor.echo("No completion") return end if state[:index].nil? && insert_completion_noselect? && matches.length > 1 (matches, selected: nil) state[:index] = :pending_select return end if state[:index].nil? && insert_completion_noinsert? preview_idx = direction.positive? ? 0 : matches.length - 1 state[:index] = :pending_insert state[:pending_index] = preview_idx (matches, selected: preview_idx, current: matches[preview_idx]) return end idx = state[:index] idx = nil if idx == :pending_select if idx == :pending_insert idx = state.delete(:pending_index) || (direction.positive? ? 0 : matches.length - 1) else idx = idx.nil? ? (direction.positive? ? 0 : matches.length - 1) : (idx + direction) % matches.length end replacement = matches[idx] end_col = state[:current_end_col] start_col = state[:start_col] @editor.current_buffer.delete_span(state[:row], start_col, state[:row], end_col) _y, new_x = @editor.current_buffer.insert_text(state[:row], start_col, replacement) @editor.current_window.cursor_y = state[:row] @editor.current_window.cursor_x = new_x state[:index] = idx state[:current_end_col] = start_col + replacement.length if matches.length == 1 @editor.echo(replacement) else (matches, selected: idx, current: replacement) end rescue StandardError => e @editor.echo_error("Completion error: #{e.message}") clear_insert_completion end |
#load_history! ⇒ Object
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 |
# File 'lib/ruvim/completion_manager.rb', line 38 def load_history! path = history_file_path return unless path return unless File.file?(path) raw = File.read(path) data = JSON.parse(raw) return unless data.is_a?(Hash) loaded = Hash.new { |h, k| h[k] = [] } data.each do |prefix, items| next unless [":", "/", "?"].include?(prefix) next unless items.is_a?(Array) hist = loaded[prefix] items.each do |item| next if item.empty? hist.delete(item) hist << item end hist.shift while hist.length > 100 end @cmdline_history = loaded rescue StandardError => e verbose_log(1, "history load error: #{e.message}") end |
#push_history(prefix, line) ⇒ Object
--- Command-line history ---
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ruvim/completion_manager.rb', line 22 def push_history(prefix, line) return if line.empty? text = line hist = @cmdline_history[prefix] hist.delete(text) hist << text hist.shift while hist.length > 100 @cmdline_history_index = nil end |
#reset_history_index! ⇒ Object
34 35 36 |
# File 'lib/ruvim/completion_manager.rb', line 34 def reset_history_index! @cmdline_history_index = nil end |
#save_history! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ruvim/completion_manager.rb', line 66 def save_history! path = history_file_path return unless path payload = { ":" => Array(@cmdline_history[":"]).map(&:to_s).last(100), "/" => Array(@cmdline_history["/"]).map(&:to_s).last(100), "?" => Array(@cmdline_history["?"]).map(&:to_s).last(100) } FileUtils.mkdir_p(File.dirname(path)) tmp = "#{path}.tmp" File.write(tmp, JSON.pretty_generate(payload) + "\n") File.rename(tmp, path) rescue StandardError => e verbose_log(1, "history save error: #{e.message}") end |
#trailing_keyword_fragment(prefix_text, window, buffer) ⇒ Object
--- Keyword helpers ---
242 243 244 245 246 247 |
# File 'lib/ruvim/completion_manager.rb', line 242 def trailing_keyword_fragment(prefix_text, window, buffer) cls = keyword_char_class(window, buffer) prefix_text.to_s[/[#{cls}]+\z/] rescue RegexpError prefix_text.to_s[/[[:alnum:]_]+\z/] end |
#update_incsearch_preview_if_needed ⇒ Object
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/completion_manager.rb', line 198 def update_incsearch_preview_if_needed return unless incsearch_enabled? cmd = @editor.command_line ensure_incsearch_preview_origin!(direction: (cmd.prefix == "/" ? :forward : :backward)) pattern = cmd.text if pattern.empty? clear_incsearch_preview_state(apply: false) return end buf = @editor.current_buffer win = @editor.current_window origin = @incsearch_preview[:origin] tmp_window = RuVim::Window.new(id: -1, buffer_id: buf.id) tmp_window.cursor_y = origin[:row] tmp_window.cursor_x = origin[:col] regex = GlobalCommands.instance.send(:compile_search_regex, pattern, editor: @editor, window: win, buffer: buf) match = GlobalCommands.instance.send(:find_next_match, buf, tmp_window, regex, direction: @incsearch_preview[:direction]) if match win.cursor_y = match[:row] win.cursor_x = match[:col] win.clamp_to_buffer(buf) end @incsearch_preview[:active] = true rescue RuVim::CommandError, RegexpError # Keep editing command-line without forcing an error flash on every keystroke. end |