Sublime Forum

Completions - find previous word

#1

Hello.
I’ve created a JavaScript completions list and I’m fairly happy with it so far. I eventually worked out how to persuade it to ignore string, comment and constant scope, and to also display after a dot ‘.’ .

What I would like to do is persuade it that after entering ‘document.’ it should only show document members. Currently it seems to treat the dot as a terminating character, and so shows my full list of completions. Is there a way to do this by amending the completions scope statement, or by adding something to the JavaScript.tmLanguage file?

Alternatively, I’m considering creating a js_completions.py file. I am a programmer - but not in Python. So I would appreciate some guidance as to how I could modify the following to check the word that appears before the ‘.’ (assuming there is one…) ?

[code]class TagCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
# Only trigger within HTML
if not view.match_selector(locations[0],
“text.html - source”):
return ]

    pt = locations[0] - len(prefix) - 1
    ch = view.substr(sublime.Region(pt, pt + 1))
    # I'll change the following to .
    if ch != '<':
        return ][/code]
0 Likes

#2

Did you ever get any of these things going?
I would love to check them out and maybe borrow some ideas if that’s okay.

0 Likes

#3

Attached completions file. It doesn’t filter methods shown (to document, window etc. members) - this is not really possible with a dynamic language such as JavaScript. However, words Window, Document, etc., are part of the trigger. So typing ‘Wind’ will reduce the list to mainly ‘window’ members.

Also, if I use auto-completions for ‘body’, ‘alert()’ it will pre-pend these with the correct object - ‘document.body’ etc.

That is, if I use

[code]mywindow.ale // it will complete as…
mywindow.alert(‘message here’) // but

ale // will complete as…

window.alert(‘message here’)[/code]
Optional arguments appear as , someoption]. When highlighted either press Delete or over-type it, re-instating the comma and space.

There is a list of css properties in the list but their trigger is preceded by an underscore _ so that they don’t clutter the main list. That is, entering an underscore will reduce the list largely to these css properties, but it is not actually necessary to type the underscore.

Personally, I don’t use ‘.’ as a trigger - this would be too intrusive, and I tweaked the auto-complete delay setting so the completions don’t pop up too quickly.
AndyJS.zip (10.1 KB)

0 Likes

#4

Nice!
I also found your py completions post A few questions on a completions plugin

Thanks for sharing!

0 Likes