Sublime Forum

Adding "<" to auto_completions.py

#1

I think this might be bad and I should feel bad.
But could someone tell me if this is crazy for a completions plugin?

Specifically the part where I add “<” (less than sign) into the array using for item in html.

[pre=#141414] if ch != ‘<’:
html = ((list(item)-2],"<" + list(item)1]) for item in html])[/pre]
I’m just covering my bases since a lot of folks are using this plugin I’d hate to have to revert these changes later.
Here is the plugin example:

[pre=#141414]class HtmlTagCompletions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):

  •  #** restrict scope somewhere here*
    
  •  #** array list tags etc...*
    html = 
          ("FOOTER\tTag", "FOOTER>$1</FOOTER>"),
          ("HEADER\tTag", "HEADER>$1</HEADER>"),
          ("NAV\tTag", "NAV>$1</NAV>"),
          ("SECTION\tTag", "SECTION>$1</SECTION>"),
          ("VIDEO\tTag", "VIDEO>$1</VIDEO>")
    ]
    
    
    pt = locations0] - len(prefix) - 1
    ch = view.substr(sublime.Region(pt, pt + 1))
    if ch != '<':
          html = ((list(item)-2],"<" + list(item)1]) for item in html])
    
    
    return html, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS[/pre]
    

Thanks in advance!

0 Likes

#2

Hmm… I’m not exactly sure what you’re trying to do. If the question is, can you put a < into the autocompletion, yes. You can. However, I’m not sure I understand what the line is doing.

If I understand correctly, HTML is a list of tuples.
So for each tuple, you are converting the tuple into a list and getting the 2nd and 2nd to last item and adding it to the autocomplete? You lost me.

0 Likes

#3

[quote=“C0D312”]Hmm… I’m not exactly sure what you’re trying to do. If the question is, can you put a < into the autocompletion, yes. You can. However, I’m not sure I understand what the line is doing.

If I understand correctly, HTML is a list of tuples.
So for each tuple, you are converting the tuple into a list and getting the 2nd and 2nd to last item and adding it to the autocomplete? You lost me.[/quote]

If you notice, the html tag completions in the html array for this plugin do not begin with a less than character (tag begin <). The user will get a complete tag if they start with the less than character but not if they start with say the letter F. The code just adds a less than character to the completion but only when it’s triggered by a character that is not the less than character.

I hope I explained that well…

0 Likes

#4

Okay. That makes sense, but why convert the tuples to a list and get the -2 element and 1 element?

0 Likes

#5

Also, substr accepts points as a parameter. So you can replace ch = view.substr(sublime.Region(pt, pt + 1)) with ch = view.substr(pt)

0 Likes

#6

I’m not sure how else to add in the less than character, it was the easiest way I could think of (can you suggest something easier?).
By the way the code works, the main problem is any extra sublime-completions files aren’t being added to the autocomplete.

0 Likes

#7

I can see what you are trying to achieve but I am not sure that I would be happy with the end result. This setting:

// Additional situations to trigger auto complete "auto_complete_triggers": {"selector": "text.html", "characters": "<"} ],
persuades ST to display the tag auto-completions after the opening angle is typed. If you follow the path you are suggesting then your completions will pop-up for practically every single character typed. For example, if you are typing plain text within a

tag, there is no way to distinguish whether you are continuing to type text or starting a new tag :exclamation:

0 Likes

#8

Nice! That might be a recent thing since that part of the code is from jps’ html_completions.py in the default HTML package.

0 Likes

#9

[quote=“agibsonsw”]I can see what you are trying to achieve but I am not sure that I would be happy with the end result. This setting:

// Additional situations to trigger auto complete "auto_complete_triggers": {"selector": "text.html", "characters": "<"} ],
persuades ST to display the tag auto-completions after the opening angle is typed. If you follow the path you are suggesting then your completions will pop-up for practically every single character typed. For example, if you are typing plain text within a

tag, there is no way to distinguish whether you are continuing to type text or starting a new tag :exclamation:[/quote]

Well, you’re right. Since you understand my direction, could we prevent triggering autocomplete within tags using the api?
Edit: I realize this won’t be possible and it makes me a little sad that autocomplete works the way it does for tags. Thanks.

0 Likes

#10

In the on_query_completions,

if 'tag' in scope_name(pt): return

0 Likes

#11

[quote=“C0D312”]In the on_query_completions,

if 'tag' in scope_name(pt): return[/quote]

@COD312 I would not have thought of that without really spending more time with the API. Brilliant thanks!

The bigger issue though is the sublime-completions files aren’t loading in the autocomplete list - unfortunately.

0 Likes

#12

I’m not super familar with how the scoping system works but I believe theres away to say “minus scope x.” So in your completion file, you can have scope = text.html, minus tags.

Edit: yep. Here’s some info: viewtopic.php?f=2&t=1809&p=8405&hilit=scope+except#p8405 text.html - tag

0 Likes

#13

Well yeah, your posts are really helpful man.

I’ll see if I can’t get to the bottom of that and post a new thread with more information if I can’t get it to do what I need.
Edit: Perfect. Thanks!

0 Likes

#14

So okay. That solves the triggering unwanted autocompletes in tags.

But completions in sublime-completions files arent being loaded into the autocomplete list (even with just text.html as the scope).
I think it might be that ST is expecting an array/list to be returned by the plugin when there is no autocomplete so that it has something to extend or append to.

0 Likes

#15

[quote=“atomi”]So okay. That solves the triggering unwanted autocompletes in tags.

But completions in sublime-completions files arent being loaded into the autocomplete list (even with just text.html as the scope).
I think it might be that ST is expecting an array/list to be returned by the plugin when there is no autocomplete so that it has something to extend or append to.[/quote]

Yes I think you need to return an empty list:

if 'tag' in scope_name(pt): return ]
Also sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS is requesting that only your completions will appear in the list.

I still think you should test a small subset of your completions before getting too deeply involved. You can suppress them from appearing within ‘tag’ scope, but if you edit within a tag they won’t reappear. And Typing this text will cause your completions to pop up for every letter typed.

0 Likes

#16

[quote=“agibsonsw”]
Yes I think you need to return an empty list:

if 'tag' in scope_name(pt): return ]
Also sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS is requesting that only your completions will appear in the list.

I still think you should test a small subset of your completions before getting too deeply involved. You can suppress them from appearing within ‘tag’ scope, but if you edit within a tag they won’t reappear. And Typing this text will cause your completions to pop up for every letter typed.[/quote]

So I removed INHIBIT_WORD_COMPLETIONS | INHIBIT_EXPLICIT_COMPLETIONS and sublime-completions files still are not working with this.

Sublime-completion files work well when I return an empty list, but not when that list contains completions. Even after removing the INHIBIT directives.

Would this be a bug?

0 Likes

#17

I’m not sure about this line - it might be creating a generator.

html = ((list(item)-2],"<" + list(item)[1]) for item in html])

try following it with ‘print html’ and check it in the Console (Ctrl '). Also check the Console for error messages. Alternatively, try

html = (list(item)-2],"<" + list(item)[1]) for item in html] print html return (html)

0 Likes

#18

I don’t think it’s that line I can comment it out and sublime-completions files still don’t load for html.

That line just adds a leading less than character to every second string in the completions list.
I’m not sure how efficient it is; I’ll likely add to a global variable later.

0 Likes