Monday, September 15, 2008

Reorder Eclipse Plug-in : How does it work?

In the last entry I published the Reorder Eclipse plug-in. In this entry I will talk about how it works.

Basically, the Reorder plug-in uses the AST APIs to get parsed structure of the Java source code surrounding the caret in the Java editor.
  • It gets the ASTNode at caret position using NodeFinder API and starts traversing the parent ASTNodes until it find a Class Instance Creation, Method Invocation, Method Declaration or an Array initializer node.
  • Once found it gets the ASTNode's list of arguments, parameters or array initialization elements and stores the text of each node in a ordered list of items. While doing so, it also records the intervening white spaces as items.
  • It also records which item's extent surrounds the caret position. It records it as a current item.
  • Then, it swaps the current item with the following or preceding non-whitespace item in the list based on the action that was invoked - forward or backward swap.
  • Lastly it builds the string from the list of items and replaces the original text with the new string.
That's it.

No comments: