Simple Autocomplete For MacRuby

Written by steve on 12-01-2009 at 01:41 AM

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

Permalink

 

0 comments

name (opt.)
email (req.)
comment
what letter comes after "j" in the alphabet?

blog home