Permalink
Browse files

Very simple forward search

  • Loading branch information...
1 parent 392b922 commit 44311ac7819367ef0aebdce45235f655ebe08a30 @danlucraft committed Jan 17, 2010
Showing with 103 additions and 9 deletions.
  1. +2 −0 CHANGES
  2. +37 −0 plugins/edit_view/lib/edit_view/document.rb
  3. +8 −0 plugins/edit_view_swt/lib/edit_view_swt.rb
  4. +56 −9 plugins/redcar/redcar.rb
View
@@ -9,6 +9,7 @@ Enhancements:
* Remembers last directory when opening a file/directory. (thanks Roger Pack)
* Word movement (alt-left, alt-right) now works as it should in an editor for
programmers.
+ * Very simple forward search command.
* Somewhat faster startup time.
* Nicer error message when jruby jar is missing.
@@ -22,6 +23,7 @@ Fixes:
* Cut/Copy sometimes didn't work.
* The show more tabs menu in a notebook is handled properly.
* Deleting a lot of lines no longer causes the last few to lose highlighting.
+ * Clojure highlighting works.
Version 0.3.1dev (9 Jan 2010)
=============================
@@ -262,10 +262,47 @@ def offset_at_inner_end_of_line(line_ix)
end
end
+ # Does the minimum amount of scrolling that brings the given line
+ # into the viewport. Which may be none at all.
+ #
+ # @param [Integer] line_ix a zero-based line index
def scroll_to_line(line_ix)
+ if line_ix > biggest_visible_line
+ top_line_ix = smallest_visible_line + (line_ix - biggest_visible_line) + 2
+ top_line_ix = [top_line_ix, line_count - 1].min
+ scroll_to_line_at_top(top_line_ix)
+ elsif line_ix < smallest_visible_line
+ bottom_line_ix = line_ix - 2
+ bottom_line_ix = [bottom_line_ix, 0].max
+ scroll_to_line_at_top(bottom_line_ix)
+ end
+ end
+
+ # Tries to scroll so the given line is at the top of the viewport.
+ #
+ # @param [Integer] line_ix a zero-based line index
+ def scroll_to_line_at_top(line_ix)
@edit_view.controller.scroll_to_line(line_ix)
end
+
+ # The line_ix of the line at the top of the viewport.
+ #
+ # @return [Integer] a zero-based line index
+ def smallest_visible_line
+ @edit_view.controller.smallest_visible_line
+ end
+ # The line_ix of the line at the bottom of the viewport.
+ #
+ # @return [Integer] a zero-based line index
+ def biggest_visible_line
+ @edit_view.controller.biggest_visible_line
+ end
+
+ def num_lines_visible
+ biggest_visible_line - smallest_visible_line
+ end
+
private
def update_from_mirror
@@ -179,6 +179,14 @@ def model_grammar_changed(name)
@mate_text.set_grammar_by_name(name)
end
+ def smallest_visible_line
+ @mate_text.viewer.get_top_index
+ end
+
+ def biggest_visible_line
+ @mate_text.viewer.get_bottom_index
+ end
+
def update_grammar(new_mirror)
title = new_mirror.title
return if @mate_text.set_grammar_by_filename(title)
View
@@ -355,17 +355,64 @@ def execute
end
end
- class SpeedbarExample < Redcar::Command
- class SearchSpeedbar < Redcar::Speedbar
- label "Query"
- textbox(:query, "search for this") { |v| p [:query_changed, v] }
- toggle(:case_sensitive, "case sensitive", "Ctrl+I", false) {|v| p [:case_sensitive_changed, v] }
- button(:search, "Cmd+N") { p :search_pressed }
- key("Ctrl+R") { p :ctrl_r_pressed }
+ class SearchForwardCommand < Redcar::EditTabCommand
+ key :osx => "Ctrl+S"
+
+ class Speedbar < Redcar::Speedbar
+ label "Regex"
+ textbox :query
+ button :search, "Return" do
+ FindNextRegex.new(Regexp.new(@speedbar.query), false).run
+ end
end
def execute
- win.open_speedbar(SearchSpeedbar.new)
+ @speedbar = SearchForwardCommand::Speedbar.new(self)
+ win.open_speedbar(@speedbar)
+ end
+ end
+
+ class FindNextRegex < Redcar::EditTabCommand
+ def initialize(re, wrap=nil)
+ @re = re
+ @wrap = wrap
+ end
+
+ def to_s
+ "<#{self.class}: @re:#{@re.inspect} wrap:#{!!@wrap}>"
+ end
+
+ def execute
+ # first search the remainder of the current line
+ curr_line = doc.get_line(doc.cursor_line)
+ cursor_line_offset = doc.cursor_offset - doc.offset_at_line(doc.cursor_line)
+ curr_line = curr_line[cursor_line_offset..-1]
+ if curr_line =~ @re
+ line_start = doc.offset_at_line(doc.cursor_line)
+ startoff = line_start + $`.length + cursor_line_offset
+ endoff = startoff + $&.length
+ doc.set_selection_range(startoff..endoff)
+ else
+ # next search the rest of the lines
+ line_num = doc.cursor_line + 1
+ curr_line = doc.get_line(line_num)
+ until line_num == doc.line_count - 1 or
+ found = (curr_line.to_s =~ @re)
+ line_num += 1
+ curr_line = doc.get_line(line_num)
+ end
+ if found
+ line_start = doc.offset_at_line(line_num)
+ startoff = line_start + $`.length
+ endoff = startoff + $&.length
+ doc.set_selection_range(startoff..endoff)
+ doc.scroll_to_line(line_num)
+ end
+ if !doc.get_line(line_num) and @wrap
+ doc.cursor_offset = 0
+ execute
+ end
+ end
end
end
@@ -403,6 +450,7 @@ def self.menus
item "Strip Whitespace", StripWhitespaceCommand
separator
item "Goto Line", GotoLineCommand
+ item "Regex Search", SearchForwardCommand
end
sub_menu "Project" do
item "Find File", Project::FindFileCommand
@@ -414,7 +462,6 @@ def self.menus
item "Print Scope Tree", PrintScopeTreeCommand
item "Refresh Directory", Project::RefreshDirectoryCommand
item "Dialog Tester", DialogExample
- item "Speedbar Example", SpeedbarExample
end
sub_menu "View" do
item "Rotate Notebooks", RotateNotebooksCommand

0 comments on commit 44311ac

Please sign in to comment.