Home Download Buy Blog Forum Support

adding "<" to auto_completions.py

adding "<" to auto_completions.py

Postby atomi on Tue May 01, 2012 1:32 am

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.

      if ch != '<':
html = ([(list(item)[-2],"<" + list(item)[1]) for item in html])

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:

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 = locations[0] - 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


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

Re: adding "<" to auto_completions.py

Postby C0D312 on Tue May 01, 2012 1:55 am

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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: adding "<" to auto_completions.py

Postby atomi on Tue May 01, 2012 2:12 am

C0D312 wrote: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.



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

Re: adding "<" to auto_completions.py

Postby C0D312 on Tue May 01, 2012 2:20 am

Okay. That makes sense, but why convert the tuples to a list and get the -2 element and 1 element?
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: adding "<" to auto_completions.py

Postby C0D312 on Tue May 01, 2012 2:23 am

Also, substr accepts points as a parameter. So you can replace
Code: Select all
ch = view.substr(sublime.Region(pt, pt + 1))
with
Code: Select all
ch = view.substr(pt)
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: adding "<" to auto_completions.py

Postby atomi on Tue May 01, 2012 2:28 am

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


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

Re: adding "<" to auto_completions.py

Postby agibsonsw on Tue May 01, 2012 2:31 am

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:

Code: Select all
    // 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 <p> tag, there is no way to distinguish whether you are continuing to type text or starting a new tag :!:
"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: adding "<" to auto_completions.py

Postby atomi on Tue May 01, 2012 2:33 am

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


Nice! That might be a recent thing since that part of the code is from jps' html_completions.py in the default HTML package.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: adding "<" to auto_completions.py

Postby atomi on Tue May 01, 2012 2:39 am

agibsonsw wrote: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:

Code: Select all
    // 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 <p> tag, there is no way to distinguish whether you are continuing to type text or starting a new tag :!:


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

Re: adding "<" to auto_completions.py

Postby C0D312 on Tue May 01, 2012 2:42 am

In the on_query_completions,
Code: Select all
if 'tag' in scope_name(pt):
  return
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests