Implementing Refactorings  in IntelliJ IDEA Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/
Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural...
Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural...
Building Blocks <ul><li>Not a set of Lego bricks </li></ul><ul><li>A workflow framework into which you can plug your code ...
Lexing and Parsing <ul><li>Lexer converts text into stream of lexems </li></ul><ul><li>Recursive-descent parser builds PSI...
 
References <ul><li>Usages of an element declared elsewhere </li></ul><ul><li>Can cross languages (e.g. XML to Java) </li><...
General Refactoring Workflow <ul><li>Show UI for entering options </li></ul><ul><li>Find usages of target element </li></u...
Find Usages <ul><li>Finds all references to PSI element </li></ul><ul><li>Based on inverted index of all identifiers </li>...
Code Modification <ul><li>Stub of new element created from text </li></ul><ul><li>Targets of references in fragment to be ...
Formatter <ul><li>Defines a set of constraints on layout of pairs of elements </li></ul><ul><ul><li>Minimum spacing / line...
Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural...
Structural Search <ul><li>QBE interface for navigating and modifying Java and XML/HTML code </li></ul><ul><ul><li>Code fra...
Structural Search Template
 
Structural Search Problems <ul><li>Very hard to use by non-expert users </li></ul><ul><li>Patterns are written in a non-ob...
Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural...
Context Dependence in Java <ul><li>Java language is not uniform </li></ul><ul><li>Many contexts in which only certain code...
Refactoring Quality <ul><li>Users typically don’t review the results of applying an automated refactoring </li></ul><ul><l...
 
DSL for Code Transformations <ul><li>If the complexity can’t be avoided, maybe it can be compressed? </li></ul><ul><li>Des...
Patterns <ul><li>Internal DSL for matching AST fragments </li></ul>
Language Independent Refactorings <ul><li>Currently only the basic refactorings (Rename and Move) are mostly language-agno...
Summary <ul><li>Refactorings in IntelliJ IDEA involve a lot of complex code with limited reuse  </li></ul><ul><li>Complexi...
Q&A Dmitry Jemerov [email_address]
Implementing Refactorings  in IntelliJ IDEA Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/
Upcoming SlideShare
Loading in...5
×

Implementing Refactorings in IntelliJ IDEA

3,187

Published on

Presentation on Second ACM Workshop on Refactoring Tools https://netfiles.uiuc.edu/dig/RefactoringInfo/WRT08/

Published in: Technology, Business
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,187
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Implementing Refactorings in IntelliJ IDEA

  1. 1. Implementing Refactorings in IntelliJ IDEA Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/
  2. 2. Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural search and replace </li></ul><ul><li>Problems and future directions </li></ul>
  3. 3. Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural search and replace </li></ul><ul><li>Problems and future directions </li></ul>
  4. 4. Building Blocks <ul><li>Not a set of Lego bricks </li></ul><ul><li>A workflow framework into which you can plug your code </li></ul><ul><li>And a set of utility functions and classes to implement the essence of refactoring </li></ul><ul><li>Every refactoring is somewhat different </li></ul>
  5. 5. Lexing and Parsing <ul><li>Lexer converts text into stream of lexems </li></ul><ul><li>Recursive-descent parser builds PSI (Program Structure Interface) tree </li></ul><ul><ul><li>Contains all tokens including whitespace </li></ul></ul><ul><ul><li>Error markers at places of syntax errors </li></ul></ul><ul><ul><li>Supports high-level semantic operations </li></ul></ul><ul><ul><li>Supports write operations (add, replace, delete) </li></ul></ul>
  6. 7. References <ul><li>Usages of an element declared elsewhere </li></ul><ul><li>Can cross languages (e.g. XML to Java) </li></ul><ul><li>Key operations: </li></ul><ul><ul><li>Resolve (get declaration by usage) </li></ul></ul><ul><ul><li>Callbacks on move/rename of target </li></ul></ul>
  7. 8. General Refactoring Workflow <ul><li>Show UI for entering options </li></ul><ul><li>Find usages of target element </li></ul><ul><li>Check for conflicts (accessibility etc.) </li></ul><ul><li>Show list of affected usages, allow user to exclude some </li></ul><ul><li>Modify target element and each usage as appropriate </li></ul>
  8. 9. Find Usages <ul><li>Finds all references to PSI element </li></ul><ul><li>Based on inverted index of all identifiers </li></ul><ul><li>All words that look like a reference are checked if they indeed resolve to target </li></ul>
  9. 10. Code Modification <ul><li>Stub of new element created from text </li></ul><ul><li>Targets of references in fragment to be moved are remembered </li></ul><ul><li>Stub contents is replaced with fragment to be moved </li></ul><ul><li>Modifiers etc. are updated via PSI </li></ul><ul><li>References are updated so that they point to the same targets </li></ul><ul><li>Updated fragment is reformatted </li></ul>
  10. 11. Formatter <ul><li>Defines a set of constraints on layout of pairs of elements </li></ul><ul><ul><li>Minimum spacing / line breaks </li></ul></ul><ul><ul><li>Indentation </li></ul></ul><ul><ul><li>Alignment </li></ul></ul><ul><li>Whitespace not violating constraints is preserved </li></ul>
  11. 12. Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural search and replace </li></ul><ul><li>Problems and future directions </li></ul>
  12. 13. Structural Search <ul><li>QBE interface for navigating and modifying Java and XML/HTML code </li></ul><ul><ul><li>Code fragment with some variables </li></ul></ul><ul><ul><li>Constraints for variables </li></ul></ul><ul><ul><li>Replacement pattern specifying how occurrences should be changed (optional) </li></ul></ul><ul><li>Allows to perform custom refactoring operations </li></ul>
  13. 14. Structural Search Template
  14. 16. Structural Search Problems <ul><li>Very hard to use by non-expert users </li></ul><ul><li>Patterns are written in a non-obvious way </li></ul><ul><li>Unintuitive UI </li></ul><ul><li>Doesn’t cover all operations </li></ul><ul><li>No façade around internal API for writing scripts </li></ul>
  15. 17. Agenda <ul><li>Demo of new refactoring features </li></ul><ul><li>Refactoring building blocks </li></ul><ul><li>Structural search and replace </li></ul><ul><li>Problems and future directions </li></ul>
  16. 18. Context Dependence in Java <ul><li>Java language is not uniform </li></ul><ul><li>Many contexts in which only certain code is allowed </li></ul><ul><ul><li>Constructors, super constructor calls </li></ul></ul><ul><ul><li>Enum constants </li></ul></ul><ul><ul><li>Anonymous classes </li></ul></ul><ul><ul><li>Initializer blocks </li></ul></ul><ul><li>Lots of special-case code needed to handle weird context changes </li></ul>
  17. 19. Refactoring Quality <ul><li>Users typically don’t review the results of applying an automated refactoring </li></ul><ul><li>Expect their tool to be 100% correct </li></ul><ul><li>Solutions which provide 90% correctness with simple implementation aren’t acceptable </li></ul><ul><li>No way to escape hand-coding corner cases </li></ul>
  18. 21. DSL for Code Transformations <ul><li>If the complexity can’t be avoided, maybe it can be compressed? </li></ul><ul><li>Desire to replace verbose Java code with a domain-specific language </li></ul><ul><ul><li>Internal DSL </li></ul></ul><ul><ul><li>Groovy/Ruby </li></ul></ul><ul><ul><li>JetBrains MPS </li></ul></ul>
  19. 22. Patterns <ul><li>Internal DSL for matching AST fragments </li></ul>
  20. 23. Language Independent Refactorings <ul><li>Currently only the basic refactorings (Rename and Move) are mostly language-agnostic </li></ul><ul><li>All other refactorings are reimplemented from scratch for every new language </li></ul><ul><li>Desire to extract language-independent essence to common part </li></ul>
  21. 24. Summary <ul><li>Refactorings in IntelliJ IDEA involve a lot of complex code with limited reuse </li></ul><ul><li>Complexity caused by large number of concepts in Java language </li></ul><ul><li>Necessity of 100% correctness means that complexity is essential </li></ul><ul><li>Main hope for improvement is building powerful code transformation DSLs </li></ul>
  22. 25. Q&A Dmitry Jemerov [email_address]
  23. 26. Implementing Refactorings in IntelliJ IDEA Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/

×