Home Download Buy Blog Forum Support

Completions - find previous word

Completions - find previous word

Postby agibsonsw on Fri Jan 27, 2012 9:31 pm

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: Select all
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 []
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Completions - find previous word

Postby atomi on Tue May 01, 2012 5:34 am

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.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Completions - find previous word

Postby agibsonsw on Tue May 01, 2012 10:24 am

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: Select all
mywindow.ale    // it will complete as..
mywindow.alert('message here')    // but

ale    // will complete as..

window.alert('message here')

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.
Attachments
AndyJS.zip
(10.06 KiB) Downloaded 73 times
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Completions - find previous word

Postby atomi on Tue May 01, 2012 5:08 pm

Nice!
I also found your py completions post viewtopic.php?p=28289#p28289

Thanks for sharing!
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests