RazrFalcon / roxmltree

Represent an XML document as a read-only tree.
Apache License 2.0
434 stars 37 forks source link

End position of elements #96

Closed ctron closed 1 year ago

ctron commented 1 year ago

I noticed that there is a starting position of an element, but no end position available.

I am looking for a way to find position in a case like this:

<root>
  <foo>
    <bar/>
    <bar/>
  </foo>
</root>

Taking the element /root/foo, I would want to have the < of the opening <foo> and then the > of the closing </foo>.

I noticed #10 and the commit mentioning the addition of range, but can't seem to find it in the current API.

RazrFalcon commented 1 year ago

Yes, it was removed recently. You can use 0.15 for now.

The reason it was removed is because each node and attribute would have to store Range<usize>, which is 16 bytes on 64bit targets and affects memory usage quite significantly.

RazrFalcon commented 1 year ago

I plan to re-add this feature, but can you explain your use case? I'm really curious. The reason it was removed was not only because it was a bit hard to implement, but because I just couldn't see when it could be useful.

ctron commented 1 year ago

I plan to re-add this feature, but can you explain your use case? I'm really curious. The reason it was removed was not only because it was a bit hard to implement, but because I just couldn't see when it could be useful.

Sure: I am working on a Language Server Protocol server (for a VScode plugin). Parsing XML, and highlighting some part of the XML for the editor. This requires to have start and end position in the form of line/position-in-line.

Using ropey it is possible to translate between position in file <-> line/position-in-line pretty easily. But even that requires to know where an element starts, and where it ends.

I guess, writing editors/editing functionality, it makes sense to have all kind of other positional information (start of element, end of element, end of closing element, …).

ctron commented 1 year ago

Yay, I noticed you added this, and made a new release. Many thanks!

Here is how this looks like in action:

image

RazrFalcon commented 1 year ago

Great!