Here is an extract from my ‘on_query_completions’. It’s for Python, but you can gain a few hints from it.
match_selector allows you to target/scope the completions behaviour. Remove ‘-comments’ if you wish;
‘prefix’ allows you to check what they’ve typed as the trigger, and ‘locations’ gains you access to preceding text;
extract_completions enables reading of the default completions list;
INHIBIT_WORD_COMPLETIONS and INHIBIT_EXPLICIT_COMPLETIONS enables you to prevent the default completions from appearing.
[code]import sublime, sublime_plugin
py_funcs =
(“import()\t__import__ fn”,
“import(${1:name}${2:, globals, locals, fromlist, level]})$0”),
(“abs()\tabs fn”, “abs(${1:number})$0”),
(“all()\tall fn”, “all(${1:iterable})$0”),
(“any()\tany fn”, “any(${1:iterable})$0”),
(“bin()\tbin fn”, “bin(${1:integer})$0”),
(“bool()\tbool fn”, “bool(${1:[value]})$0”),
(“super()\tsuper fn”, “super(${1:type}${2:, object/type]})$0”),
(“tuple()\ttuple fn/ctor”, “tuple(${1:[iterable]})$0”),
(“type()\ttype fn”, “type(${1:object})$0”),
(“type()\ttype ctor”, “type(${1:name}, ${2:bases}, ${3:dict})$0”),
(“unichr()\tunichr fn”, “unichr(${1:[integer]})$0”),
(“unicode()\tunicode fn”, “unicode(${1:[object, ]}${2:encoding}${3:, errors]})$0”),
(“vars()\tvars fn”, “vars(${1:[object]})$0”),
(“xrange()\txrange fn”, “xrange(${1:[start, ]}${2:stop}${3:, step]})$0”),
(“zip()\tzip fn”, “zip(${1:iterable})$0”)
]
py_members = # methods and attributes
(“add()\tset”, “add(${1:elem})$0”),
(“append()\tMutable”, “append(${1:x})$0”),
(“split()\tstring”, “split(${1:[sep]}${2:, maxsplit]})$0”),
(“splitlines()\tstring”, “splitlines(${1:[keepends]})$0”),
(“writelines()\tfile”, “writelines(${1:sequence})$0”),
(“zfill()\tstring”, “zfill(${1:width})$0”)
]
subl_methods =
(“active_group()\tST Window”, “active_group()$0”),
(“active_view()\tST Window”, “active_view()$0”),
(“active_view_in_group()\tST Window”, “active_view_in_group(${1:group})$0”),
(“active_window()\tsublime”, “active_window()$0”),
(“add()\tST RegionSet”, “add(${1:region})$0”),
(“add_all()\tST RegionSet”, “add_all(${1:region_set})$0”),
(“add_on_change()\tST Settings”, “add_on_change(${1:key}, ${2:on_change})$0”),
(“window()\tST View”, “window()$0”),
(“windows()\tsublime”, “windows()$0”),
(“word()\tST View”, “word(${1:region/pt})$0”)
]
sublime_methods_all = list(py_members)
sublime_methods_all.extend(subl_methods)
class PythonCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
global py_funcs, py_members, subl_methods, subl_methods_all
if not view.match_selector(locations[0], ‘source.python -string -comment -constant’):
return ]
completions = ]
pt = locations[0] - len(prefix) - 1
ch = view.substr(sublime.Region(pt, pt + 1)) # the character before the trigger
is_dot = (ch == ‘.’)
if is_dot: completions = py_members
if not is_dot: completions = py_funcs
if view.find("(?:from|import)\s+sublime", 0) is not None and is_dot:
completions = sublime_methods_all # include Python methods/attributes
compl_default = [view.extract_completions(prefix)]
compl_default = (item + “\tDefault”, item) for sublist in compl_default
for item in sublist if len(item) > 3] # flatten
compl_default = list(set(compl_default)) # make unique
compl_full = list(completions)
compl_full.extend(compl_default)
compl_full.sort()
return (compl_full, sublime.INHIBIT_WORD_COMPLETIONS |
sublime.INHIBIT_EXPLICIT_COMPLETIONS)[/code]
I’ve attached my JS completions file as well, as it may save you some typing . Mine exhibit specific behaviour. For example, it prepends ‘window’ or ‘document’ for some members.
AndyJS.zip (10.1 KB)