Home Download Buy Blog Forum Support

Dynamic auto-complete

Dynamic auto-complete

Postby SCdF on Wed Feb 29, 2012 8:59 pm

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?
SCdF
 
Posts: 4
Joined: Wed Feb 29, 2012 8:46 pm

Re: Dynamic auto-complete

Postby SCdF on Wed Feb 29, 2012 9:11 pm

OK, so I found SublimeClang's code, might be a good starting point.
SCdF
 
Posts: 4
Joined: Wed Feb 29, 2012 8:46 pm

Re: Dynamic auto-complete

Postby agibsonsw on Wed Feb 29, 2012 10:46 pm

SCdF wrote:OK, so I found SublimeClang's code, might be a good starting point.


'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 :o )

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 8-). Andy.
"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: Dynamic auto-complete

Postby agibsonsw on Wed Feb 29, 2012 11:07 pm

Actually,

Code: Select all
import xml.etree    # or
import xml.etree.ElementTree as ET


might be what you need :?:

Code: Select all
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")

This sample refers to an 'xhtml' file, but isn't XSD just XML in disguise?

http://effbot.org/zone/element-index.htm
"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: Dynamic auto-complete

Postby agibsonsw on Wed Feb 29, 2012 11:18 pm

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

Code: Select all
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"})

yields
Code: Select all
[<Element {http://www.w3.org/2001/XMLSchema}element at 0x9a17f2c>]
"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: Dynamic auto-complete

Postby andersonfreitas on Fri Jul 27, 2012 6:31 pm

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. :)
andersonfreitas
 
Posts: 1
Joined: Fri Jul 27, 2012 6:26 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron