Sublime Forum

Can't get a custom Completions file to work in ST3

#1

I’m wanting to create some custom auto-completions for html, however I cannot for the life of me get it to work.

My initial file is based on the documentation found here and it looks like this

[code]{
“scope”: “text.html - source - meta.tag, punctuation.definition.tag.begin”,

"completions":

	{ "trigger": "test\tTest", "contents": "This is a test" }
]

}[/code]

I named the file “test.sublime-completions” and put it in a subfolder in my …/Packages/User directory. However when I went to an HTML file to try it out the auto-completion window didn’t pop up and pressing tab or enter only resulted in a new line or tab being inserted.

What do I need to do to get this working?

I tried changing the “scope” to things like “text.html.basic”, “text.html - source - meta.tag”, and a few others I can’t remember with no luck. I also tried changing the name of the file to “HTML.sublime-completions” with no luck. I tried taking it out of the subfolder and putting it directly in my …/Packagers/User directory and again no luck.

0 Likes

#2

So after changing my custom completions file and doing a lot of research I discovered that “html_completions.py” is preventing my custom completions to work.

My current custom completions file looks like this.

[code]{
“scope”: “text.html.basic meta.tag.block.any.html string.quoted.double.html”,

“completions”:

  { "trigger": "test\tTest", "contents": "This is a test" }

]
}[/code]

In the html_completions.py file there is a line that gets the current scope/selector.

is_inside_tag = view.match_selector(locations[0], "text.html meta.tag - text.html punctuation.definition.tag.begin")

Which is passed to the “get_completions” method and checked against and returns sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS

[code] def get_completions(self, view, prefix, locations, is_inside_tag):
# see if it is in tag.attr or tag#attr format
if not is_inside_tag:
tag_attr_expr = self.expand_tag_attributes(view, locations)
if tag_attr_expr != ]:
return (tag_attr_expr, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

    pt = locations[0] - len(prefix) - 1
    ch = view.substr(sublime.Region(pt, pt + 1))

    # print('prefix:', prefix)
    # print('location0:', locations[0])
    # print('substr:', view.substr(sublime.Region(locations[0], locations[0] + 3)), '!end!')
    # print('is_inside_tag', is_inside_tag)
    # print('ch:', ch)

    completion_list = ]
    if is_inside_tag and ch != '<':
        if ch in ' ', '\t', '\n']:
            # maybe trying to type an attribute
            completion_list = self.get_attribute_completions(view, locations[0], prefix)
        # only ever trigger completion inside a tag if the previous character is a <
        # this is needed to stop completion from happening when typing attributes
        return (completion_list, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

    if prefix == '':
        # need completion list to match
        return (], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

    # match completion list using prefix
    completion_list = self.prefix_completion_dict.get(prefix[0], ])

    # if the opening < is not here insert that
    if ch != '<':
        completion_list = (pair[0], '<' + pair[1]) for pair in completion_list]

    flags = 0
    if is_inside_tag:
        sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS

    return (completion_list, flags)[/code]

if I comment out the “text.html” part of the selector above then my custom completions will work but I’m not 100% sure what the ramifications of this would be.

is_inside_tag = view.match_selector(locations[0], "text.html meta.tag - punctuation.definition.tag.begin")

Can anyone suggest a means to allow my completions inside class attributes without messing up what html_completion.py does?

0 Likes