Written by: steve ross on December 1st 2009

This is a skeletal way to implement autocompletion where candidates are chosen according to whether the letters (independent of order) are present in the search string and the candidate completion. Textmate does this extremely well in the Cmd+T file picker.

Someplace, implement:


  def unique_chars_match(partial, candidates)
    candidates.select{|candidate| partial.count(candidate) == partial.length}.sort
  end

  def completion_list_for(target, candidates)
    unique_chars_match(target, candidates)
  end

In your window delegate, implement:


  def control(control, 
              textView: view, 
              completions: completions, 
              forPartialWordRange: range, 
              indexOfSelectedItem: index)

    target = view.textStorage.string[range.location, range.length]
    completion_list_for(target, Strings::AutocompletionsFor[:keywords])
  end

Someplace else, create a Strings module and put a hash in there for custom autocompletions:


  module Strings
    AutocompletionsFor = {
      :animals => ['dog', 'cat', 'elephant', 'llama']
      :plants  => ['fern', 'tree', 'rosebush']
    }
  end