Skip to content

Commit

Permalink
Replacing text should move the selection so you can see what's happen…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
danlucraft committed Aug 1, 2010
1 parent 6402752 commit 98c73d5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 21 deletions.
66 changes: 64 additions & 2 deletions plugins/document_search/features/replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,39 @@ Feature: Replace in file
And I run the command DocumentSearch::SearchAndReplaceCommand
Then the DocumentSearch::SearchAndReplaceSpeedbar speedbar should be open

Scenario: Replace next occurrence on the same line
When I replace the contents with "Foo\nBar Rab Rab\nBaz"
And I move the cursor to 4
And I run the command DocumentSearch::SearchAndReplaceCommand
And I type "Rab" into the "Search" field in the speedbar
And I type "RAB" into the "Replace" field in the speedbar
And I press "Replace" in the speedbar
Then the contents should be "Foo\nBar RAB Rab\nBaz"
And the selected text should be "RAB"
And the selection range should be from 8 to 11

Scenario: Replace next occurrence on the same line twice
When I replace the contents with "Foo\nBar Rab Rab\nBaz"
And I move the cursor to 4
And I run the command DocumentSearch::SearchAndReplaceCommand
And I type "Rab" into the "Search" field in the speedbar
And I type "RAB" into the "Replace" field in the speedbar
And I press "Replace" in the speedbar
And I press "Replace" in the speedbar
Then the contents should be "Foo\nBar RAB RAB\nBaz"
And the selected text should be "RAB"
And the selection range should be from 12 to 15

Scenario: Replace next occurrence
When I replace the contents with "Foo\nBar\nBaz"
When I replace the contents with "Foo\nBar\nBaz\nBar\nQux"
And I move the cursor to 0
And I run the command DocumentSearch::SearchAndReplaceCommand
And I type "Bar" into the "Search" field in the speedbar
And I type "Rab" into the "Replace" field in the speedbar
And I press "Replace" in the speedbar
Then the contents should be "Foo\nRab\nBaz"
Then the contents should be "Foo\nRab\nBaz\nBar\nQux"
And the selected text should be "Rab"
And the selection should be on line 1

Scenario: Replace next occurrence twice
When I replace the contents with "Foo\nBar\nBaz\nBar\nQux"
Expand All @@ -30,6 +55,18 @@ Feature: Replace in file
Then the contents should be "Foo\nRab\nBaz\nBar\nQux"
When I press "Replace" in the speedbar
Then the contents should be "Foo\nRab\nBaz\nRab\nQux"
And the selected text should be "Rab"
And the selection should be on line 3

Scenario: Replace next occurrence wraps
When I replace the contents with "Foo\nBar\nBaz"
And I move the cursor to 8
And I run the command DocumentSearch::SearchAndReplaceCommand
And I type "Bar" into the "Search" field in the speedbar
And I type "Rab" into the "Replace" field in the speedbar
When I press "Replace" in the speedbar
Then the contents should be "Foo\nRab\nBaz"
And the selected text should be "Rab"

Scenario: Replace all replaces one
When I replace the contents with "Foo\nBar\nBaz"
Expand All @@ -40,6 +77,8 @@ Feature: Replace in file
Then I should see a message box containing "Replaced 1 occurrence"
When I press "Replace All" in the speedbar
Then the contents should be "Foo\nRab\nBaz"
And the selected text should be "Rab"
And the selection should be on line 1

Scenario: Replace all replaces two
When I replace the contents with "Foo\nBar\nBaz\nBar\nQux"
Expand All @@ -50,3 +89,26 @@ Feature: Replace in file
Then I should see a message box containing "Replaced 2 occurrences"
When I press "Replace All" in the speedbar
Then the contents should be "Foo\nRab\nBaz\nRab\nQux"
And the selected text should be "Rab"
And the selection should be on line 3

Scenario: Replace next occurrence test bug
When I replace the contents with "the\n* Speedbars have access to the properties of the widgets in them."
And I move the cursor to 0
And I run the command DocumentSearch::SearchAndReplaceCommand
And I type "the" into the "Search" field in the speedbar
And I type "THE" into the "Replace" field in the speedbar
And I press "Replace" in the speedbar
Then the contents should be "THE\n* Speedbars have access to the properties of the widgets in them."
And the selection range should be from 0 to 3
And I press "Replace" in the speedbar
Then the contents should be "THE\n* Speedbars have access to THE properties of the widgets in them."
And the selection range should be from 31 to 34
And I press "Replace" in the speedbar
Then the contents should be "THE\n* Speedbars have access to THE properties of THE widgets in them."
And the selection range should be from 49 to 52
And I press "Replace" in the speedbar
Then the contents should be "THE\n* Speedbars have access to THE properties of THE widgets in THEm."
And the selection range should be from 64 to 67


36 changes: 25 additions & 11 deletions plugins/document_search/lib/document_search/replace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ def replace_next(query, replace, &body)
line_seg = curr_line[cursor_line_offset..-1]

# Call the search method passed by the caller
new_line = body.call(line_seg, query, replace)
new_line, startoff, endoff = body.call(line_seg, query, replace)

# The passed in method returns the string replacement or nil
if new_line
# Add the replacment to the end of the line, and then replace in the document
curr_line[cursor_line_offset..-1] = new_line
@doc.replace_line(@doc.cursor_line, curr_line.chomp)
line_offset = @doc.offset_at_line(@doc.cursor_line)
@doc.set_selection_range(cursor_line_offset + line_offset + startoff, cursor_line_offset + line_offset + endoff)
return 1
end

#Look at the rest of the lines starting at the next line
start_line = @doc.cursor_line
(start_line+1..@doc.line_count-1).each do |i|
new_line = body.call(@doc.get_line(i), query, replace)
new_line, startoff, endoff = body.call(@doc.get_line(i), query, replace)
if new_line
@doc.replace_line(i, new_line.chomp)
line_offset = @doc.offset_at_line(i)
@doc.set_selection_range(line_offset + startoff, line_offset + endoff)
@doc.ensure_visible(@doc.offset_at_line(i))
return 1
end
Expand All @@ -35,33 +39,43 @@ def replace_next(query, replace, &body)
#Look at the rest of the lines starting at the beginning
start_line = @doc.cursor_line
(0..start_line-1).each do |i|
new_line = body.call(@doc.get_line(i), query, replace)
new_line, startoff, endoff = body.call(@doc.get_line(i), query, replace)

if new_line
@doc.replace_line(i, new_line.chomp)
line_offset = @doc.offset_at_line(i)
@doc.set_selection_range(line_offset + startoff, line_offset + endoff)
@doc.ensure_visible(@doc.offset_at_line(i))
return 1
end
end
puts "Not found"
return 0
0
end

# Replace All starts at the begnning of the doc and iterates over all of the lines.
def replace_all(query, replace, &body)
count = 0
(0..@doc.line_count-1).each do |i|
last_match_line = nil
startoff = nil
endoff = nil
(0..(@doc.line_count-1)).each do |i|
begin
line = body.call(@doc.get_line(i), query, replace)

line, a, b = body.call(@doc.get_line(i), query, replace)
if line
startoff = a
endoff = b
last_match_line = i
@doc.replace_line(i, line.chomp)
count+=1
count += 1
end
end while line != nil
end
puts count
return count
if last_match_line
line_offset = @doc.offset_at_line(last_match_line)
@doc.set_selection_range(line_offset + startoff, line_offset + endoff)
@doc.ensure_visible(@doc.offset_at_line(last_match_line))
end
count
end
end
end
8 changes: 4 additions & 4 deletions plugins/document_search/lib/document_search/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module DocumentSearch
class Search
# An instance of a search type method: Regular expression
def self.regex_search_method(line, query, replace)
if line =~ /#{query}/o
if line =~ /#{query}/
startoff = $`.length
endoff = (startoff + $&.length) -1
endoff = (startoff + $&.length) - 1
line[startoff..endoff] = replace
return line
return line, startoff, startoff + replace.length
end
return nil
end
Expand All @@ -18,7 +18,7 @@ def self.plain_search_method(line, query, replace)
startoff = i
endoff = i + query.length - 1
line[startoff..endoff] = replace
return line
return line, startoff, startoff + replace.length
end
return nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def after_draw
end

def self.search_replace
puts "query = '#{@previous_query}', replace = '#{@previous_replace}', search_type = '#{@previous_search_type}', match_case = '#{@previous_match_case}' "
current_query = @previous_query
current_replace = @previous_replace
case @previous_search_type
Expand All @@ -54,14 +53,12 @@ def self.search_replace
end
adoc = Redcar.app.focussed_notebook_tab.document
count = Replace.new(adoc).replace_next(current_query, current_replace, &search_method)
puts "count #{count}"
if count == 0
Redcar::Application::Dialog.message_box("No instance of the search string were found", {:type => :info, :buttons => :ok})
end
end

def self.search_replace_all
puts "query = '#{@previous_query}', replace = '#{@previous_replace}', search_type = '#{@previous_search_type}', match_case = '#{@previous_match_case}' "
current_query = @previous_query
current_replace = @previous_replace
case @previous_search_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ def unescape_text(text)

Then /^the selection range should be from (\d+) to (\d+)$/ do |from_str, to_str|
doc = Redcar::EditView.focussed_edit_view_document
doc.block_selection_mode = true
r = doc.selection_range
r.begin.should == from_str.to_i
r.end.should == to_str.to_i
Expand Down

0 comments on commit 98c73d5

Please sign in to comment.