Class: Ruber::FindInFiles::FindInFilesPlugin

Inherits:
ExternalProgramPlugin show all
Defined in:
plugins/find_in_files/find_in_files.rb

Overview

Plugin class for the find_in_files feature

The @output_widget instance variable contains the widget output should by displayed by at the time. This varies according to whether a search or a replacement search is being performed.

Defined Under Namespace

Classes: ReplacementData

Constant Summary

Constants inherited from Plugin

LICENSES

Instance Attribute Summary

Attributes inherited from ExternalProgramPlugin

process

Attributes included from PluginLike

plugin_description

Instance Method Summary (collapse)

Methods inherited from ExternalProgramPlugin

#do_stderr, #do_stdout, #failed_to_start, #run_process, #shutdown, #slot_process_finished, #stop_process

Methods inherited from GuiPlugin

#action_collection, #execute_action, #register_action_handler, #setup_actions, #unload

Methods inherited from Plugin

#about_data

Methods included from PluginLike

#add_extensions_to_project, #add_options_to_project, #add_widgets_to_project, #create_tool_widget, #delayed_initialize, #initialize_plugin, #plugin_name, #query_close, #register_options, #register_with_project, #remove_extensions_from_project, #remove_from_project, #remove_options_from_project, #remove_widgets_from_project, #restore_session, #save_settings, #session_data, #setup_action, #setup_actions, #shutdown, #unload, #update_project

Signal Summary

API for feature find_in_files

Constructor Details

- (FindInFilesPlugin) initialize(psf)

plugin

Parameters:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'plugins/find_in_files/find_in_files.rb', line 93

def initialize psf
  super
  Ruber[:autosave].register_plugin self, true
  @rak_path = nil
  @dlg = FindReplaceInFilesDlg.new
  @replace_data = nil
  self.connect SIGNAL('process_finished(int, QString)') do
    Ruber[:main_window].change_state 'find_in_files_running', false
  end
  connect @replace_widget, SIGNAL('file_added(QString)'), self, SLOT('show_replacements(QString)')
  self.connect(SIGNAL(:process_started)) do
    emit @replace_data ? replace_search_started : search_started
  end
  self.connect SIGNAL('process_finished(int, QString)') do
    emit @replace_data ? replace_search_finished : search_finished
  end
  Ruber[:components].connect(SIGNAL('feature_loaded(QString, QObject*)')) do |f, o|
    o.register_plugin self, true if f == 'autosave'
  end
  Ruber[:main_window].change_state 'find_in_files_running', false
end

Instance Method Details

- (<String>) cmd_line(text, opts) (private)

Creates the command line to pass to rak #options_from_dialog

Parameters:

  • text (String)

    the string to look for

  • opts (Hash)

    an hash containing the options to use, as that returned by

Returns:

  • (<String>)

    an array with the arguments and options to pass to rak



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'plugins/find_in_files/find_in_files.rb', line 300

def cmd_line text, opts
  cmd = []
  cmd << '-i' if opts[:case_insensitive]
  cmd << '-w' if opts[:whole_words]
  cmd << '-Q' if opts[:plain]
  if opts[:filters] then opts[:filters].each{|f| cmd << "--#{f}"}
  else cmd << '-a'
  end
  cmd << '-g' << opts[:custom_filter] if opts[:custom_filter]
  cmd << '-l' if opts[:replace]
  cmd << text
  cmd << opts[:places]
  #opts[:places] may be an array, so with flatten it will be reduced to strings
  cmd.flatten
end

- (Regexp) create_regexp(find_text, opts) (private)

Creates a regexp to search for the given string with the given options

This method is used after the user starts a search from the dialog to create the regexp to pass to rak.

interpreted as a string rather than as a regexp. If true, characters which have special meaning in a regexp will be escaped) than be allowed to match in the middle of a word. If true, the regexp will be enclosed in a pair of \b sequences ignore case. If true, the regexp will be given the Regexp::IGNORECASE flag

Parameters:

  • find_text (String)

    the string to make the regexp for

  • opts (Hash)

    the options to use for the search

Options Hash (opts):

  • plain (Boolean) — default: false

    whether the find_text parameter should be

  • whole_words (Boolean) — default: false

    whether the regexp must match whole words rather

  • case_insensitive (Boolean) — default: false

    whether or not the regexp should

Returns:

  • (Regexp)

    a regexp matching the given text and options



253
254
255
256
257
258
259
# File 'plugins/find_in_files/find_in_files.rb', line 253

def create_regexp find_text, opts
  str = find_text.dup
  str = Regexp.quote str if opts[:plain]
  str = "\\b#{str}\\b" if opts[:whole_words]
  reg_opts = opts[:case_insensitive] ? Regexp::IGNORECASE : 0
  Regexp.new str, reg_opts
end

- (nil) display_exit_message(code, reason) (private)

Override of ExternalProgramPlugin#display_exit_message

It doesn’ display any message if rak exited succesfully

Returns:

  • (nil)


142
143
144
145
# File 'plugins/find_in_files/find_in_files.rb', line 142

def display_exit_message code, reason
  super if code != 0
  nil
end

- (Boolean) do_autosave(places) (private)

Autosaves documents in preparation for a search

its understood as an absolute directory name and all open documents corresponding to files in that directory are saved. If it is an array, each element of the array is interpreted as an absolute file name. All documents corresponding to such files are saved.

Parameters:

  • places (String, <String>)

    the files to save. If it is a string, then

Returns:



227
228
229
230
231
232
233
# File 'plugins/find_in_files/find_in_files.rb', line 227

def do_autosave places
  docs = Ruber[:world].documents.documents_with_file
  if places.is_a? String then docs = docs.select{|d| d.path.start_with? places}
  else docs = docs.select{|d| places.include? d.path}
  end
  Ruber[:autosave].autosave self, docs, :on_failure => :ask, :message => 'Do you want to go on with replacing?'
end

- (nil) find(text, opts) (private)

Starts a search using the parameters the user entered in the dialog

After starting the search (which is an asynchronous process), it activates and displays the Search tool widget.

Returns:

  • (nil)


173
174
175
176
177
178
179
180
181
182
183
# File 'plugins/find_in_files/find_in_files.rb', line 173

def find text, opts
  @replace_data = nil
  @output_widget = @find_widget
  cmd = cmd_line text, opts
  @find_widget.clear_output
  Ruber[:main_window].change_state 'find_in_files_running', true
  @find_widget.working_directory = '/'
  run_process 'rak', '/', cmd, nil
  Ruber[:main_window].activate_tool 'find_in_files_widget'
  nil
end

- (nil) find_replace (private)

Starts a search or replcement search

This method is called when the user closes the dialog pressing the Search or Replace button

Returns:

  • (nil)


154
155
156
157
158
159
160
161
162
163
# File 'plugins/find_in_files/find_in_files.rb', line 154

def find_replace
  @dlg.clear
  @dlg.allow_project = Ruber[:world].active_project
  @dlg.exec
  case @dlg.action
  when :find then find @dlg.find_text, options_from_dialog
  when :replace then replace @dlg.find_text, @dlg.replacement_text, options_from_dialog
  else return
  end
end

Slot Signature:

find_replace()

- (Object) load_settings (private)

Loads the settings



210
211
212
213
# File 'plugins/find_in_files/find_in_files.rb', line 210

def load_settings
  @rak_path = Ruber[:config][:find_in_files, :rak_path]
  nil
end

- (Hash) options_from_dialog (private)

Gathers the options entered in the find dialog an puts them in a hash

the following entries:

  • :case_insensitive: whether the search should be case insensitive or not
  • :plain: whether the contents of the find widget in the dialog should be interpreted as a regexp or a plain string
  • :whole_words: whether the search should match full words or not
  • :places: an array of files and directory to search
  • :all_files: whether all files or only those matching some filter must be searched
  • :filter: an array of the standard filters to use
  • :custom_filter: a custom filter to use

Returns:

  • (Hash)

    a hash containing the options chosen by the user. It can contain



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'plugins/find_in_files/find_in_files.rb', line 275

def options_from_dialog
  opts = {}
  opts[:case_insensitive] = !@dlg.case_sensitive?
  opts[:plain] = @dlg.mode == :plain
  opts[:whole_words] = @dlg.whole_words?
  places = case @dlg.places
  when :custom_dir then @dlg.directory
  when :project_dir then Ruber[:world].active_project.project_directory
  when :project_files then Ruber[:world].active_project.project_files.to_a
  when :open_files then Ruber[:world].documents.documents_with_file.map{|d| d.path}
  end
  opts[:places] = Array(places)
  opts[:all_files] = @dlg.all_files?
  opts[:filters] = @dlg.filters
  opts[:custom_filter] = @dlg.custom_filter
  opts
end

- (Object) process_standard_error(lines)

Override of ExternalProgramPlugin#process_standard_error

It does nothing, so errors are ignored

Parameters:

  • the (<String>)

    error lines, as produced by rak



131
132
# File 'plugins/find_in_files/find_in_files.rb', line 131

def process_standard_error lines
end

- (Object) process_standard_output(lines)

Override of ExternalProgramPlugin#process_standard_output

It passes the text to the output widget’s display_output method

Parameters:

  • the (<String>)

    lines to display, as produced by rak



121
122
123
# File 'plugins/find_in_files/find_in_files.rb', line 121

def process_standard_output lines
  @output_widget.display_output lines
end

- (nil) replace(find_text, replace_text, opts) (private)

Starts a replacement search using the parameters the user entered in the dialog

After starting the search (which is an asynchronous process), it activates and displays the Replace tool widget.

Returns:

  • (nil)


193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'plugins/find_in_files/find_in_files.rb', line 193

def replace find_text, replace_text, opts
  opts[:replace] = true
  regexp = create_regexp find_text, opts
  @replace_data = ReplacementData.new regexp, replace_text, opts
  @output_widget = @replace_widget
  cmd = cmd_line find_text, opts
  do_autosave opts[:places]
  @replace_widget.clear_output
  Ruber[:main_window].change_state 'find_in_files_running', true
  @replace_widget.working_directory = '/'
  run_process 'rak', '/', cmd, nil
  Ruber[:main_window].activate_tool 'replace_in_files_widget'
end

- (nil) show_replacements(file) (private)

Inserts matches for the given file in the Replace tool widget

Parameters:

  • file (String)

    the name of the file

Returns:

  • (nil)


322
323
324
325
326
327
328
329
330
331
# File 'plugins/find_in_files/find_in_files.rb', line 322

def show_replacements file
  lines = File.readlines file
  lines.each_with_index do |l, i|
    l.chomp!
    new = l.dup
    changed = new.gsub! @replace_data.regexp, @replace_data.replacement_text
    @replace_widget.add_line file, i, l, new if changed
  end
  nil
end

Slot Signature:

show_replacements(QString)

Signal Details

- search_started

Signal emitted when a search is started

- replace_search_started

Signal emitted when a search for replacements is started

- search_finished

Signal emitted when a search is finished

- replace_search_finished

Signal emitted when a search for replacements is finished