Sublime Forum

Dynamic auto-complete

#1

Hi,

I’d like write a plugin to add support for XML tag auto-completion based on the defined schema(s). I know, done by lunchtime.

(Or alternatively, use one that already exists…?)

Anyway, I’ve been looking around the tutorials and reading some code, and I can’t work out if it’s possible to inject dynamic values into the ctrl+space autocomplete command. I see examples of static additions, such as in the HTML auto-completions list, but to add schema support it needs to be aware of a bunch of stuff, including what schemas you have linked, where the caret is in the file and what else the file contains etc.

Are there any examples I can look at?

0 Likes

#2

OK, so I found SublimeClang’s code, might be a good starting point.

0 Likes

#3

‘Packages/HTML/html_completions.py’ might provide an easier starting point.

The key method is ‘on_query_completions’ which should return a list of tuples. I’m guessing that this would completely replace any other available completions with the list that you return.

There’s a library PyXML but, apparently, this is mostly now in the standard Python library (I’m sure you don’t want to write your own XML parser :astonished: )

Again, I’m guessing, that you might use ‘on_load’ to then use file i/o to find, and parse, the attached schema file, creating a global list of tags that you might then refer to in your ‘on_query_completions’ event. I’m assuming that this list should persist between calls to your event(?). That is, you probably need/want to avoid re-parsing the schema file every time the completions list is required! Actually, using ‘on_load’ is probably not suitable - a keyboard shortcut should probably initiate your code.

If I were doing this I would probably concentrate, initially, on discovering and parsing the schema file, and deciding how/where to store the extracted tags and attributes. But maybe someone has already done this :sunglasses:. Andy.

0 Likes

#4

Actually,

import xml.etree # or import xml.etree.ElementTree as ET

might be what you need :question:

[code]import elementtree.ElementTree as ET

tree = ET.parse(“page.xhtml”)

the tree root is the toplevel html element

print tree.findtext(“head/title”)

if you need the root element, use getroot

root = tree.getroot()

…manipulate tree…

tree.write(“out.xml”)[/code]
This sample refers to an ‘xhtml’ file, but isn’t XSD just XML in disguise?

effbot.org/zone/element-index.htm

0 Likes

#5

One last input :laughing: **lxml **looks perhaps more promising (using XPath), but it’s not in the standard library:

[code]from lxml import etree
from cStringIO import StringIO

f = StringIO()

f = StringIO(’’’
<xsd:schema xmlns:xsd=“http://www.w3.org/2001/XMLSchema”>
<xsd:element name=“a” type=“AType”/>
<xsd:complexType name=“AType”>
xsd:sequence
<xsd:element name=“b” type=“xsd:string” />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
‘’’)

xmlschema_doc = etree.parse(f)

xmlschema_doc.xpath(‘xsd:element’,
namespaces={“xsd”: “http://www.w3.org/2001/XMLSchema”})[/code]
yields

<Element {http://www.w3.org/2001/XMLSchema}element at 0x9a17f2c>]
0 Likes

#6

Is there anyone working on this?

I’m also considering to develop an auto-complete for XML based on its xsd schema, maybe we could join forces. :smile:

0 Likes