I happened to find some code within the HTML/html_completions.py plugin, which did exactly what I asked for â dynamic completion
However, I do have some questions, if someone happens to know an answer and minds to answer:
-
If the plugin accepts the EventListener parameter, and has an on_query_completions method, the method is called on âautocompleteâ event. The method returns either ], or an array of unions. The question is â what the first element of the union is for? E.g. return (u'/**', snippet)]
â the first part doesnât seem to affect pretty much anything
-
What are the methods of identifying what is the syntax of current file? I want to return empty result as early as possible, if filetype is not yet supported (currently it is php only)
The code is as follows (please donât mock me yet):
import sublime, sublime_plugin
import re
class CodeDoc(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
# only complete single line/selection
if len(locations) != 1:
return ]
# todo check is supported type of file (php)
line = view.substr(sublime.Region(view.line(locations[0]).a, locations[0]))
rex = re.compile("^\s*(.+)")
m = rex.match(line)
if not m:
return ]
if m.group(1) != '/**':
return ]
# find end of completion line
currLineEnd = view.find('\n\r]', locations[0])
if currLineEnd is None:
return ]
# find end of function/class declaration (php delimiter)
nextLineEnd = view.find('{]', currLineEnd.end())
if nextLineEnd is None:
return ]
declaration = view.substr(sublime.Region(currLineEnd.end(), nextLineEnd.begin()))
if declaration.find("function") > -1:
snippet = self.expandPhpFunction(declaration)
if snippet:
return (u'/**', snippet)]
elif declaration.find("class") > -1:
snippet = self.expandPhpClass(declaration)
if snippet:
return (u'/**', snippet)]
return ]
def expandPhpClass(self, declaration):
snippet = '/**\n'
snippet += ' * ${1}\n'
snippet += ' * @package ${2:default}\n'
snippet += ' */'
return snippet
def expandPhpFunction(self, declaration):
rex = re.compile("\((.*)\)", re.DOTALL)
m = rex.search(declaration)
if not m:
return None
params = m.group(1).split(',')
snippet = '/**\n * ${1:Description}\n'
i = 2
for p in params:
p2 = p.find('=')
if p2 > -1:
p = p[0 : p2]
p = p.strip()
p = p.replace('$', '\$')
p = p.replace('&', '')
if p == '':
continue
snippet += ' * @param ${' + str(i) + ':type} ' + p + ' ${' + str(i+1) + '}\n'
i += 2
snippet += ' * @return ${' + str(i) + ':type}\n'
snippet += ' */'
return snippet
Currently, I try to expand â/**â to a phpdoc block for a php class/function. If anybody has any suggestions on the code, Iâd appreciate them a lot (since I have pretty much no knowledge in python, but I try to learn as I go)