Sha256: a800a28efc0fe314391e859fe29f46443a370579145154a17a667e814545ed30

Contents?: true

Size: 1.24 KB

Versions: 70

Compression:

Stored size: 1.24 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
      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
      
      def merge!(other)
        @words.merge!(other.words)
      end
      
      def inspect
        "<WordList: " + @words.map {|k,v| "#{k}:#{v}"}.join(" ") + ">"
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
redcar-0.13 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.5dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.4dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.3dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.2dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.1dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.12.1 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.13.0dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-0.12 plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.27dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.26dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.25dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.24dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.23dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.22dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.21dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.20dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.19dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.18dev plugins/auto_completer/lib/auto_completer/word_list.rb
redcar-dev-0.12.17dev plugins/auto_completer/lib/auto_completer/word_list.rb