mercredi 17 février 2016

Is there a way to get a hierarchical index to the node the cursor is in?

In the XML document I want to find the node that the cursor is in.

I don't think Ace Editor can do this. But I think maybe it keeps track by using indexes system.

What do I mean? Well, in XML there is a hierarchical structure of leaves and branches or ancestors and descendants. If you are keeping track of the number of nodes and locations you can create a system to find it again.

For example take this code:

enter image description here

The root node would be item [0]. The first descendant of that would be [0][0]. The second descendant would be [0][1]. If the second descendant had three descendants then their position would be [0][1][0], [0][1][1], [0][1][2].

Is there a way to get that position in Ace Editor? The reason is I have an XML object in my application. BUT Ace Editor is in JavaScript and it does not support XML or E4X. I can only get a string values from it to pass back to my application.

So I first have to get the node that the cursor is in JavaScript, then I have to find out how to map that back to the XML object in my application.

Right now I've got this far:

var xml:XML = new XML(ace.text);
var token:Object = ace.getTokenAt(ace.row, ace.column);
var type:String = token ? token.type : "";
var tagName:String;
var index:int = token ? token.index : -1; // index = 2
var start:int = token ? token.start : -1; // start = 5

if (type=="meta.tag.tag-name.xml") {
    tagName = token.value; // head
}

var matchingTagsList:XMLList = xml.descendants(tagName);

if (matchingTagsList.length()==1) {
     var match:XML = matchingTagsList[0];
}
else {
     // numerous matches. how to find one from the other?
}

BTW Ace Editor returns an index and start value. I could use that to try to find the matching tag.

I could also, possibly, convert all the XML matches to strings, then if I could get from the range in Ace from the start tag to the end tag I could compare each. But that is really hacky because there are a lot of points of failure. If there are namespaces the strings won't match, if there is whitespace characters the strings won't match, or encoded entities, etc.

Aucun commentaire:

Enregistrer un commentaire