Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (71 sloc) 3.37 KB
# This Python file uses the following encoding: utf-8
# Dictionary Auto-Complete
# This plug-in adds auto-completion entries from the dictionary file.
# useful for very lazy typers or if you're searching for a particular word.
# (c) Florian Zinggeler
import sublime
import sublime_plugin
ST3 = int(sublime.version()) > 3000
import os
class DictionaryAutoComplete(sublime_plugin.EventListener):
settings = None
b_first_edit = True
b_fully_loaded = True
word_list = []
# on first modification in comments, get the dictionary and save items.
def on_modified(self, view):
if self.b_first_edit and self.b_fully_loaded:
self.b_fully_loaded = False
sublime.set_timeout(lambda: self.load_completions(view), 3)
def load_completions(self, view):
scope_name = view.scope_name(view.sel()[0].begin()) #[0].active_view()
if self.should_trigger(scope_name):
if not self.settings:
self.settings = sublime.load_settings('Preferences.sublime-settings')
encoding = sublime.load_settings('DictionaryAutoComplete.sublime-settings').get('encoding')
if ST3:
words = sublime.load_binary_resource(self.settings.get('dictionary')).decode(encoding).splitlines()
for word in words:
word = word.split('/')[0].split('\t')[0]
elif not ST3:
self.dict_path = os.path.join(sublime.packages_path()[:-9], self.settings.get('dictionary'))
with open(self.dict_path, 'r') as dictionary:
words =
for word in words:
word = word.split('/')[0].split('\t')[0]
self.b_first_edit = False
self.b_fully_loaded = True
# This will return all words found in the dictionary.
def get_autocomplete_list(self, word):
autocomplete_list = []
# filter relevant items:
for w in self.word_list:
if word.lower() in w.lower():
if len(word) > 0 and word[0].isupper():
W = w.title()
autocomplete_list.append((W, W))
autocomplete_list.append((w, w))
except UnicodeDecodeError:
# autocomplete_list.append((w, w))
return autocomplete_list
def should_trigger(self, scope):
if "comment" in scope or "string.quoted" in scope or "text" == scope[:4]:
return True
return False
# gets called when auto-completion pops up.
def on_query_completions(self, view, prefix, locations):
scope_name =[0].active_view().scope_name([0].active_view().sel()[0].begin())
if self.should_trigger(scope_name):
return self.get_autocomplete_list(prefix)