diff --git a/README.md b/README.md index c34a54c..90b1cfb 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,18 @@ Settings You can disable the additional autocompletion provided by this package for specific source files and even select syntax within files. In the Sublime menu go to Preferences > Package Settings > All Autocomplete > Settings – User. -Example: the following Setting would disable All Autocomplete for CSS and JavaScript code: +Example: the following Setting would disable completions when you're editing CSS or JavaScript code, and would not source any completions from Markdown files: -``` +```json "exclude_from_completion": [ "css", "js" -] +], +"exclude_sources": [ + "markdown" +], +"min_word_size": 5, // don't show completions for words with fewer than this many chars +"max_word_size": 40 // don't show completions for words with more than this many chars ``` The names provided in this list are matched against the so-called "syntax scope" of the currently autocompleted input. For example, in a CSS file, when you start typing a new CSS class name, the syntax scope is "source.css meta.selector.css". The names you provide in the config above are partially matched against this scope. This means, you can completely disable All Autocomplete for all CSS code by specifying "css" – or you can disable it only for specific parts, for example, CSS selectors by specifying "selector.css". Or to disable completion in comments, include "comment" in the list. @@ -53,4 +58,4 @@ as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION -0. You just DO WHAT THE FUCK YOU WANT TO. \ No newline at end of file +0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/all_views_completions.py b/all_views_completions.py index 57a9ad0..e9a0459 100644 --- a/all_views_completions.py +++ b/all_views_completions.py @@ -7,9 +7,6 @@ import time from os.path import basename -# limits to prevent bogging down the system -MIN_WORD_SIZE = 3 -MAX_WORD_SIZE = 50 MAX_VIEWS = 20 MAX_WORDS_PER_VIEW = 100 @@ -20,17 +17,22 @@ def plugin_loaded(): global settings settings = sublime.load_settings('All Autocomplete.sublime-settings') + class AllAutocomplete(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations): - if is_disabled_in(view.scope_name(locations[0])): + if is_excluded(view.scope_name(locations[0]), settings.get("exclude_from_completion", [])): return [] - + words = [] # Limit number of views but always include the active view. This # view goes first to prioritize matches close to cursor position. - other_views = [v for v in sublime.active_window().views() if v.id != view.id] + other_views = [ + v + for v in sublime.active_window().views() + if v.id != view.id and not is_excluded(v.scope_name(0), settings.get("exclude_sources", [])) + ] views = [view] + other_views views = views[0:MAX_VIEWS] @@ -51,30 +53,32 @@ def on_query_completions(self, view, prefix, locations): contents = w.replace('$', '\\$') if v.id != view.id and v.file_name(): trigger += '\t(%s)' % basename(v.file_name()) + if v.id == view.id: + trigger += '\tabc' matches.append((trigger, contents)) return matches -def is_disabled_in(scope): - excluded_scopes = settings.get("exclude_from_completion", []) +def is_excluded(scope, excluded_scopes): for excluded_scope in excluded_scopes: - if scope.find(excluded_scope) != -1: + if excluded_scope in scope: return True return False + def filter_words(words): - words = words[0:MAX_WORDS_PER_VIEW] - return [w for w in words if MIN_WORD_SIZE <= len(w) <= MAX_WORD_SIZE] + MIN_WORD_SIZE = settings.get("min_word_size", 3) + MAX_WORD_SIZE = settings.get("max_word_size", 50) + return [w for w in words if MIN_WORD_SIZE <= len(w) <= MAX_WORD_SIZE][0:MAX_WORDS_PER_VIEW] -# keeps first instance of every word and retains the original order -# (n^2 but should not be a problem as len(words) <= MAX_VIEWS*MAX_WORDS_PER_VIEW) +# keeps first instance of every word and retains the original order, O(n) def without_duplicates(words): result = [] - used_words = [] + used_words = set() for w, v in words: if w not in used_words: - used_words.append(w) + used_words.add(w) result.append((w, v)) return result