Class: Ruvi::Completion::WordCompleter
- Inherits:
-
Object
- Object
- Ruvi::Completion::WordCompleter
- Defined in:
- lib/plugins/completion.rb
Instance Method Summary collapse
- #add_word_list(buffer, word_list, diff) ⇒ Object
- #do_completion ⇒ Object
-
#initialize(app) ⇒ WordCompleter
constructor
A new instance of WordCompleter.
- #notify_of_change(change, direction) ⇒ Object
- #process_changes(buffer) ⇒ Object
- #process_line(buffer, line, num) ⇒ Object
Constructor Details
#initialize(app) ⇒ WordCompleter
Returns a new instance of WordCompleter.
27 28 29 30 31 32 33 |
# File 'lib/plugins/completion.rb', line 27 def initialize app @app = app @app.add_insert_binding("\C-n") { do_completion } @app.change_listeners << self.method(:notify_of_change) @changes = [] @buffer_blub = {} end |
Instance Method Details
#add_word_list(buffer, word_list, diff) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/plugins/completion.rb', line 49 def add_word_list buffer, word_list, diff word_list.each { |word| @buffer_blub[buffer][word] += diff } end |
#do_completion ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/plugins/completion.rb', line 89 def do_completion buffer = @app.current_buffer process_changes buffer word_list = process_line buffer, buffer.lines[buffer.y], buffer.y curx = 0 token = word_list.find { |tok| a = (tok == word_list.last) || (curx > buffer.x) curx += tok.length a } possibles = @buffer_blub[buffer].keys possibles.delete token = possibles.find_all { |possible| possible.index(token) == 0 or token.empty? } = .sort_by { |val| -@buffer_blub[buffer][val] } if .empty? @app. "Sorry, no completion found!" return end to_insert = .first.slice(token.length..-1) EditorApp::DiffLogger::ModifyLineChange.new(buffer, buffer.y) { buffer.lines[buffer.y][buffer.ax, 0] = to_insert EditorApp.invalidate_buffer_line buffer, buffer.y } ecm = @app.end_char_mode buffer.x += to_insert.length @app.end_char_mode = ecm end |
#notify_of_change(change, direction) ⇒ Object
34 35 36 |
# File 'lib/plugins/completion.rb', line 34 def notify_of_change change, direction @changes << [change, direction] end |
#process_changes(buffer) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/plugins/completion.rb', line 55 def process_changes buffer if !(@buffer_blub.has_key? buffer) @buffer_blub[buffer] = Hash.new { 0 } (0...buffer.lines.length).each { |num| word_list = process_line buffer, buffer.lines[num], num add_word_list buffer, word_list, 1 } else @buffer_blub[buffer] = Hash.new { 0 } @changes.each { |mod_with_direction| change, direction = *mod_with_direction sign = direction ? 1 : -1 case change when EditorApp::DiffLogger::InsertLineAfterChange word_list = process_line buffer, change.new_line_data, change.line_num add_word_list buffer, word_list, sign * 1 when EditorApp::DiffLogger::RemoveLineChange word_list = process_line buffer, change.old_line_data, change.line_num add_word_list buffer, word_list, sign * -1 when EditorApp::DiffLogger::ModifyLineChange word_list = process_line buffer, change.old_line_data, change.line_num add_word_list buffer, word_list, sign * -1 word_list = process_line buffer, change.new_line_data, change.line_num add_word_list buffer, word_list, sign * 1 else raise "unhandled change type!! #{change.type}" end } @app. "would process completion! got #{@changes.length} changes!" @changes = [] end end |
#process_line(buffer, line, num) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/plugins/completion.rb', line 37 def process_line buffer, line, num word_list = [] buffer.iterate_line_highlight(num, line) { |k| if buffer.highlighter.is_a? PlainTextHighlighter word_list += line.split(/([,| ])/) else word_list << k end } word_list end |