Add new method and position cursor in body [message #758854] |
Thu, 24 November 2011 13:09  |
Eclipse User |
|
|
|
I have a requirement to add a new method to a class and then open the class code and position the cursor in it's body. The approach I have is as under:
1. Use ASTRewrite to add the method at the correct location.
2. Add a //TODO comment in the body of the method. This should create a TaskMarker at that location.
3. Recocile the compilation unit
4. Open the editor at that marker location.
The approach works but I have to add a sleep for about a second after the reconcile operation for it to work.
The following code:
compilationUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
connectedCU = compilationUnit;
IType createdType = compilationUnit.getType(className);
.... use ASTRewrite to create the method here
compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
Thread.sleep(1000); //marker lookup does not work if I remove this line
connectedCU.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1));
}
finally
{
if (connectedCU != null)
{
connectedCU.discardWorkingCopy();
}
}
...
IResource resource = compilationUnit.getResource();
IMarker[] markers = resource.findMarkers(IMarker.TASK, true, IResource.DEPTH_INFINITE);
But if I remove the Thread.sleep after the reconcile statement the findMarkers does not find the marker I just added. It makes no sense as the entire code is executing in the same thread. What am I doing wrong here ?
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.24244 seconds