Reversible TextEdits? [message #302999] |
Fri, 05 May 2006 01:00  |
Eclipse User |
|
|
|
Originally posted by: andrew_NOSPAM_.eisenberg.as
Hi,
I am looking to make changes to an IDocument object using record changes and
then be able to map arbitrary offsets from the old document into the new
document. What I mean is something like this:
try {
// create a document, make changes, and update document
IDocument document = createDocument();
CompilationUnit cu = createAST(document);
cu.recordModifications();
cu.accept(new ChangeASTVisitor());
TextEdit edit = cu.rewrite(document, new HashMap<Object, Object>());
edit.apply(document);
// get an arbitrary, but interesting offset into original document
int offset = getAnOffsetFromOriginalDocument(...);
// this is the part I don't understand
int newOffset = translateOffsetToAlteredDocument(offset, ...);
} catch
...
}
My question is: how do I create the method
translateOffsetToAlteredDocument(offset, ...)? Are there any eclipse APIs that
can help with this (and what would the arguments be), or do I have to write this
myself?
Any help would be greatly appreciated. Also, if this is not the appropriate
newsgroup, please let me know what the most appropriate one is.
thanks,
--andrew
|
|
|
Re: Reversible TextEdits? [message #303002 is a reply to message #302999] |
Fri, 05 May 2006 02:30   |
Eclipse User |
|
|
|
Hi Andrew,
I believe you can do what you request by using a RangeMarker, see below.
HTH, tom
Andrew Eisenberg wrote:
> try {
> // create a document, make changes, and update document
> IDocument document = createDocument();
> CompilationUnit cu = createAST(document);
> cu.recordModifications();
> cu.accept(new ChangeASTVisitor());
> TextEdit edit = cu.rewrite(document, new HashMap<Object, Object>());
> // get an arbitrary, but interesting offset into original document
> int offset = getAnOffsetFromOriginalDocument(...);
RangeMarker rangeMarker= new RangeMarker(offset, 0);
MultiTextEdit wrapper= new MultiTextEdit();
wrapper.addChild(edit);
wrapper.addChild(rangeMarker);
> edit.apply(document);
>
> // this is the part I don't understand
> int newOffset = translateOffsetToAlteredDocument(offset, ...);
// retrieve the updated marker
int newOffset= rangeMarker.getOffset();
>
> } catch
> ...
> }
>
|
|
|
Re: Reversible TextEdits? [message #303021 is a reply to message #303002] |
Fri, 05 May 2006 13:59  |
Eclipse User |
|
|
|
Originally posted by: andrew_NOSPAM_.eisenberg.as
Hi Tom,
Thanks for your help. I think this can work. There is an issue, though. I get
a malformed tree exception (overlapping edits).
I think I can get around this by being a little more careful about where I place
these RangeMarkers. I'll have to the RangeMarkers children of the TextEdits
that surround them.
> RangeMarker rangeMarker= new RangeMarker(offset, 0);
>
> MultiTextEdit wrapper= new MultiTextEdit();
> wrapper.addChild(edit);
> wrapper.addChild(rangeMarker); // throws MalformedTreeException
thanks,
--a
|
|
|
Powered by
FUDForum. Page generated in 0.02601 seconds