Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Reversible TextEdits?
Reversible TextEdits? [message #302999] Fri, 05 May 2006 01:00 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous message
Eclipse UserFriend
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
Previous Topic:New Window action causing Perspective Issues
Next Topic:how to get local file from RemoteResourceTypedElement
Goto Forum:
  


Current Time: Sun May 11 20:56:57 EDT 2025

Powered by FUDForum. Page generated in 0.02601 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top