Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to prevent cursor re-positioning?
How to prevent cursor re-positioning? [message #251828] Mon, 03 March 2008 09:34 Go to next message
Eclipse UserFriend
Originally posted by: topgun.in.gmail.com

I created a sample plug-in to change the access modifier of a method from
a pop up menu. Every time I right click and select an option to change the
modifier, the access modifier is changed as required, but once I apply the
changes the cursor runs to beginning of the source. How do I prevent that?

I use ASTRewrite to modify the ast and Document object to commit the
modified ast. I believe the changes should be applied in some what
different manner. AT present it is as follows :-

code snippet :
...
Document document = null;
try {
document = new Document(cu.getBuffer().getContents());
} catch (JavaModelException e) {
e.printStackTrace();
}
TextEdit edits = astRewrite.rewriteAST(document,
cu.getJavaProject().
getOptions(true));
try {
edits.apply(document);
String newSource = document.get();
cu.getBuffer().setContents(newSource);
} catch (MalformedTreeException e) {
e.printStackTrace();
}
...

Thanks,
Pushkar
Re: How to prevent cursor re-positioning? [message #251891 is a reply to message #251828] Wed, 05 March 2008 04:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: benno_baumgartner.ch.ibm.com

Pushkar wrote:
> I created a sample plug-in to change the access modifier of a method
> from a pop up menu. Every time I right click and select an option to
> change the modifier, the access modifier is changed as required, but
> once I apply the changes the cursor runs to beginning of the source. How
> do I prevent that?
>
> I use ASTRewrite to modify the ast and Document object to commit the
> modified ast. I believe the changes should be applied in some what
> different manner. AT present it is as follows :-
>
> code snippet :
> ...
> Document document = null;
> try {
> document = new Document(cu.getBuffer().getContents());
> } catch (JavaModelException e) {
> e.printStackTrace();
> }
> TextEdit edits =
> astRewrite.rewriteAST(document,
> cu.getJavaProject().
> getOptions(true));
> try {
> edits.apply(document);
> String newSource = document.get();
> cu.getBuffer().setContents(newSource);
> } catch (MalformedTreeException e) {
> e.printStackTrace();
> }
> ...
>
> Thanks,
> Pushkar
>

Hi Pushkar

You must not set the cus buffer content:
> cu.getBuffer().setContents(newSource);

Instead have a look at:
http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html

See also:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=117694

HTH
Benno
Re: How to prevent cursor re-positioning? [message #251994 is a reply to message #251891] Tue, 11 March 2008 10:37 Go to previous message
Eclipse UserFriend
Originally posted by: topgun.in.gmail.com

Hello Benno,

I now apply the changes in the following manner,

ITextFileBufferManager bufferManager =
FileBuffers.getTextFileBufferManager();
IPath path = cu.getPath();
bufferManager.connect(path, null, null);
ITextFileBuffer textFileBuffer =
bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
IDocument document = textFileBuffer.getDocument();
TextEdit edits =
astRewrite.rewriteAST(document, cu.getJavaProject().getOptions(true));
edits.apply(document);

This works. Thanks Benno.

Pushkar
Previous Topic:source refresh after running ant task
Next Topic:Multiple undo required for a single change.
Goto Forum:
  


Current Time: Sat Apr 26 06:55:15 EDT 2025

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

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

Back to the top