Class: CommandT::Controller

Inherits:
Object
  • Object
show all
Includes:
PathUtilities, SCMUtilities
Defined in:
lib/command-t/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/command-t/controller.rb', line 27

def initialize
  encoding = VIM::get_string('g:CommandTEncoding')
  if encoding
    begin
      encoding = Encoding.find(encoding)
      Encoding.default_external = encoding
      Encoding.default_internal = encoding
    rescue
    end
  end
  @nowait = '<nowait>' if ::VIM::evaluate('v:version') >= 704
end

Class Method Details

.guard(method) ⇒ Object

Wraps ‘method` in a `rescue` clause that attempts to print some useful information to the screen before re-raising any exception. Without this, most of the useful output is unhelpfully swallowed by Vim.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/command-t/controller.rb', line 12

def self.guard(method)
  class_eval <<-END
    alias original_#{method} #{method}
    def #{method}(*args, &block)
      original_#{method}(*args, &block)
    rescue Exception => e
      backtrace = e.backtrace
      trimmed = backtrace.take(backtrace.length - 2)
      text = VIM::escape_for_single_quotes trimmed.join("\n")
      ::VIM::command "echo '\#{text}'"
      raise e
    end
  END
end

Instance Method Details

#accept_selection(options = {}) ⇒ Object



234
235
236
237
238
# File 'lib/command-t/controller.rb', line 234

def accept_selection(options = {})
  selection = @match_window.selection
  hide
  open_selection(selection, options) unless selection.nil?
end

#active_finderObject

For possible use in status lines.



41
42
43
# File 'lib/command-t/controller.rb', line 41

def active_finder
  @active_finder && @active_finder.class.name
end

#backspaceObject



207
208
209
210
211
212
# File 'lib/command-t/controller.rb', line 207

def backspace
  if @focus == prompt
    prompt.backspace!
    update
  end
end

#cancelObject



248
249
250
# File 'lib/command-t/controller.rb', line 248

def cancel
  hide
end

#clearObject



263
264
265
266
# File 'lib/command-t/controller.rb', line 263

def clear
  prompt.clear!
  list_matches!
end

#clear_prev_wordObject



269
270
271
272
# File 'lib/command-t/controller.rb', line 269

def clear_prev_word
  prompt.clear_prev_word!
  list_matches!
end

#cursor_endObject



285
286
287
# File 'lib/command-t/controller.rb', line 285

def cursor_end
  prompt.cursor_end if @focus == prompt
end

#cursor_leftObject



275
276
277
# File 'lib/command-t/controller.rb', line 275

def cursor_left
  prompt.cursor_left if @focus == prompt
end

#cursor_rightObject



280
281
282
# File 'lib/command-t/controller.rb', line 280

def cursor_right
  prompt.cursor_right if @focus == prompt
end

#cursor_startObject



290
291
292
# File 'lib/command-t/controller.rb', line 290

def cursor_start
  prompt.cursor_start if @focus == prompt
end

#deleteObject



215
216
217
218
219
220
# File 'lib/command-t/controller.rb', line 215

def delete
  if @focus == prompt
    prompt.delete!
    update
  end
end

#flushObject



187
188
189
190
191
192
193
# File 'lib/command-t/controller.rb', line 187

def flush
  @file_finder  = nil
  @max_height   = nil
  @min_height   = nil
  @prompt       = nil
  @tag_finder   = nil
end

#handle_keyObject



196
197
198
199
200
201
202
203
204
# File 'lib/command-t/controller.rb', line 196

def handle_key
  key = ::VIM::evaluate('a:arg').to_i.chr
  if @focus == prompt
    prompt.add! key
    update
  else
    @match_window.find key
  end
end

#hideObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/command-t/controller.rb', line 154

def hide
  @match_window.leave
  if VIM::Window.select @initial_window
    if @initial_buffer.number == 0
      # upstream bug: buffer number misreported as 0
      # see: https://wincent.com/issues/1617
      ::VIM::command "silent b #{@initial_buffer.name}"
    else
      ::VIM::command "silent b #{@initial_buffer.number}"
    end
  end
end

#is_own_buffer(buffer_number) ⇒ Object

For possible use in status lines.



51
52
53
# File 'lib/command-t/controller.rb', line 51

def is_own_buffer(buffer_number)
  @match_window && buffer_number == @match_window.buffer_number
end

#leaveObject



295
296
297
# File 'lib/command-t/controller.rb', line 295

def leave
  @match_window.leave
end

#list_matches(options = {}) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/command-t/controller.rb', line 303

def list_matches(options = {})
  return unless @needs_update || options[:force]

  @matches = @active_finder.sorted_matches_for(
    prompt.abbrev,
    :case_sensitive => case_sensitive?,
    :limit          => match_limit,
    :threads        => CommandT::Util.processor_count,
    :ignore_spaces  => VIM::get_bool('g:CommandTIgnoreSpaces', true),
    :recurse        => VIM::get_bool('g:CommandTRecursiveMatch', true)
  )
  @match_window.matches = @matches

  # Scanner may have overwritten prompt to show progress.
  prompt.redraw

  @needs_update = false
end

#pathObject

For possible use in status lines.



46
47
48
# File 'lib/command-t/controller.rb', line 46

def path
  @path
end

#quickfixObject

Take current matches and stick them in the quickfix window.



168
169
170
171
172
173
174
175
176
177
# File 'lib/command-t/controller.rb', line 168

def quickfix
  hide

  matches = @matches.map do |match|
    "{ 'filename': '#{VIM::escape_for_single_quotes match}' }"
  end.join(', ')

  ::VIM::command 'call setqflist([' + matches + '])'
  ::VIM::command 'cope'
end

#refreshObject



180
181
182
183
184
# File 'lib/command-t/controller.rb', line 180

def refresh
  return unless @active_finder && @active_finder.respond_to?(:flush)
  @active_finder.flush
  list_matches!
end

#remove_bufferObject



223
224
225
226
227
228
229
230
231
# File 'lib/command-t/controller.rb', line 223

def remove_buffer
  return unless @active_finder.class <= CommandT::Finder::BufferFinder
  selection = @match_window.selection

  if @initial_buffer.name != selection
    ::VIM::command "bd #{selection}"
  end
  list_matches!
end

#return_is_own_buffer(buffer_number) ⇒ Object

For possible use in status lines.



56
57
58
59
60
61
62
# File 'lib/command-t/controller.rb', line 56

def return_is_own_buffer(buffer_number)
  if is_own_buffer(buffer_number)
    ::VIM::command 'return 1'
  else
    ::VIM::command 'return 0'
  end
end

#select_nextObject



253
254
255
# File 'lib/command-t/controller.rb', line 253

def select_next
  @match_window.select_next
end

#select_prevObject



258
259
260
# File 'lib/command-t/controller.rb', line 258

def select_prev
  @match_window.select_prev
end

#show_buffer_finderObject



64
65
66
67
68
# File 'lib/command-t/controller.rb', line 64

def show_buffer_finder
  @path          = VIM::pwd
  @active_finder = buffer_finder
  show
end

#show_command_finderObject



71
72
73
74
75
# File 'lib/command-t/controller.rb', line 71

def show_command_finder
  @path          = VIM::pwd
  @active_finder = command_finder
  show
end

#show_file_finderObject



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
# File 'lib/command-t/controller.rb', line 127

def show_file_finder
  # optional parameter will be desired starting directory, or ""

  arg = ::VIM::evaluate('a:arg')
  if arg && arg.size > 0
    @path = File.expand_path(arg, VIM::pwd)
  else
    traverse = VIM::get_string('g:CommandTTraverseSCM') || 'file'
    case traverse
    when 'file'
      @path = nearest_ancestor(VIM::current_file_dir, scm_markers) || VIM::pwd
    when 'dir'
      @path = nearest_ancestor(VIM::pwd, scm_markers) || VIM::pwd
    else
      @path = VIM::pwd
    end
  end

  @active_finder    = file_finder
  file_finder.path  = @path
  show
rescue Errno::ENOENT
  # probably a problem with the optional parameter
  @match_window.print_no_such_file_or_directory
end

#show_help_finderObject



78
79
80
81
82
# File 'lib/command-t/controller.rb', line 78

def show_help_finder
  @path          = VIM::pwd
  @active_finder = help_finder
  show
end

#show_history_finderObject



85
86
87
88
89
# File 'lib/command-t/controller.rb', line 85

def show_history_finder
  @path          = VIM::pwd
  @active_finder = history_finder
  show
end

#show_jump_finderObject



92
93
94
95
96
# File 'lib/command-t/controller.rb', line 92

def show_jump_finder
  @path          = VIM::pwd
  @active_finder = jump_finder
  show
end

#show_line_finderObject



99
100
101
102
103
# File 'lib/command-t/controller.rb', line 99

def show_line_finder
  @path          = VIM::pwd
  @active_finder = line_finder
  show
end

#show_mru_finderObject



106
107
108
109
110
# File 'lib/command-t/controller.rb', line 106

def show_mru_finder
  @path          = VIM::pwd
  @active_finder = mru_finder
  show
end

#show_search_finderObject



113
114
115
116
117
# File 'lib/command-t/controller.rb', line 113

def show_search_finder
  @path          = VIM::pwd
  @active_finder = search_finder
  show
end

#show_tag_finderObject



120
121
122
123
124
# File 'lib/command-t/controller.rb', line 120

def show_tag_finder
  @path          = VIM::pwd
  @active_finder = tag_finder
  show
end

#split_commandObject



327
328
329
# File 'lib/command-t/controller.rb', line 327

def split_command
  VIM::get_string('g:CommandTAcceptSelectionSplitCommand') || 'CommandTOpen split'
end

#tab_commandObject



323
324
325
# File 'lib/command-t/controller.rb', line 323

def tab_command
  VIM::get_string('g:CommandTAcceptSelectionTabCommand') || 'CommandTOpen tabedit'
end

#toggle_focusObject



241
242
243
244
245
# File 'lib/command-t/controller.rb', line 241

def toggle_focus
  @focus.unfocus # old focus
  @focus = @focus == prompt ? @match_window : prompt
  @focus.focus # new focus
end

#unloadObject



299
300
301
# File 'lib/command-t/controller.rb', line 299

def unload
  @match_window.unload
end

#vsplit_commandObject



331
332
333
# File 'lib/command-t/controller.rb', line 331

def vsplit_command
  VIM::get_string('g:CommandTAcceptSelectionVSplitCommand') || 'CommandTOpen vsplit'
end