Sha256: 384de3631ddbd230f22dceaa35796d06dd02a05626382e46936ddaf18a619391

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

module Redcar
  class AutoCompleter
    # the list of words plus their relative distance (beginning of the word to the cursor)
    # this information can just be updated, as long as the cursor is moving through the document.
    # as soon as typing happens, a word will have to be added or replaced.
    class WordList
      include Enumerable
      
      attr_reader :words
      
      def initialize
        @words = Hash.new
        @offset = 0
      end
      
      # adds a new word, iff it doesn't yet exist.
      # if the word exists, the distance will be adjusted, if it's lower.
      def add_word(word, distance)
        if @words.include?(word)
          if distance < @words[word]
            @words[word] = distance
          end
        else
          @words[word] = distance
        end
      end
      
      # yield the completions for the given prefix
      def completions
        filtered = @words.keys
        return filtered.sort!{|a,b| @words[a] <=> @words[b] }
      end
      
      def each
        @words.each {|word,distance| yield word, distance }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
redcar-0.3.4.3 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.3.4.2 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.3.4.1 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.3.4 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.3.3 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.3.2dev plugins/auto_completer/lib/auto_completer/word_list.rb