1 / 40

Graph-based, Pattern-oriented, Context-sensitive Code Completion

Graph-based, Pattern-oriented, Context-sensitive Code Completion. Anh Nguyen, Tung Nguyen, Hoan Nguyen, Ahmed Tamrawi , Hung Nguyen , Jafar Al- Kofahi , and Tien N. Nguyen Electrical and Computer Engineering Department Iowa State University. Eclipse’s Built-in Code Completion.

kasia
Download Presentation

Graph-based, Pattern-oriented, Context-sensitive Code Completion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph-based, Pattern-oriented, Context-sensitive Code Completion Anh Nguyen, Tung Nguyen, Hoan Nguyen, Ahmed Tamrawi, Hung Nguyen, Jafar Al-Kofahi, and Tien N. Nguyen Electrical and Computer Engineering Department Iowa State University

  2. Eclipse’s Built-in Code Completion code completion invocation point Documentation on a proposed method List of recommended methods

  3. Eclipse’s Built-in Code Completion Filled-in code

  4. Source Code Completion • Plays an important role in modern IDEs • Supports developers by • Recommending relevant code • Automatically filling in code

  5. State-of-the-Art Code Completion • Single method/field recommendation • Non-ranked list (sorted by alphabetical order) • Ranked list • By return type (Ye et al. ICSE ‘02) • Via co-occurring methods (Bruch et al. FSE ‘09) • Via editing history (Robbes and Lanza ASE ’08) • Template-based recommendation (e.g., Eclipse)

  6. Template-based Code Completion Recommending a template without considering context

  7. Our Goal • A code completion approach and tool • Auto-completing a high volume of code • Taking into consideration the context of the currently edited code

  8. GraPacc Approach • Developing a pattern-oriented, context-sensitive code completion approach • Evaluating usefulness of our code completion method and tool

  9. Programming Pattern • A correct and frequent usage of API elements • Is used to perform a specific programming task //Reading a text file char-by -char using FileReader and BufferedReader String fileName = “myfile.txt”; FileReaderfReader = new FileReader(fileName); BufferedReaderbReader = new BufferedReader(fReader); while(bReader.ready()) { bReader.read(); } bReader.close(); fReader.close(); Declaring Reading characters Closing

  10. Pattern-oriented Completion Multiple method invocations of multiple variables with different types and control structure (if, for,…) are recommended to adapt the currently editing code

  11. Context-sensitive Recommendation • Query • A code fragment under editing • A sequence of textual tokens • Often incomplete and may be un-parseable

  12. Context-sensitive Recommendation a) b) Different cursor positions  Potentially different recommendation lists

  13. GraPacc Overview Patterns {P} Query features {fq} Pattern features {fp} Pattern Database Searching & Ranking Query Processing Query Q Ranked list of patterns {P} Filled-in code Code Completion

  14. Pattern Management Patterns {P} Query features {fq} Pattern features {fp} Pattern Database Searching & Ranking Query Processing Query Q Ranked list of patterns {P} Filled-in code Code Completion

  15. Pattern Representation Graph-based Object Usage Model - Groum [Nguyen et al. FSE ’09] • A directed acyclic graph • Representing control and data dependencies Action node FileReader.new FileReaderfReader = new FileReader(“c:/aTextFile.txt”); BufferedReaderbReader = new BufferedReader(fReader); while (bReader.ready()){ } Control dependency FileReader Data dependency BufferedReader.new Data node BufferedReader Control node BufferedReader.ready WHILE

  16. Features • Graph-based feature: a sequence of the textual labels of the nodes along a path of a Groum • Token-based feature: a lexical token extracted in a query

  17. Features FileReader.new FileReader BufferedReader.new BufferedReader BufferedReader.ready WHILE FileReader.newFileReader FileReaderBufferedReader.new FileReader.newBufferedReader.new BufferedReader.new BufferedReader BufferedReader BufferedReader.ready BufferedReader.ready WHILE FileReader.newFileReaderBufferedReader.new FileReader.new BufferedReader.new BufferedReader FileReader.new BufferedReader.new BufferedReader.ready FileReader BufferedReader.new BufferedReader FileReader BufferedReader.new BufferedReader.ready BufferedReader.newBufferedReaderBufferedReader.ready BufferedReader.newBufferedReader.readyWHILE BufferedReaderBufferedReader.ready->WHILE … FileReader.new Size-3 features Size-1 features Size-2 features FileReader BufferedReader.new BufferedReader BufferedReader.ready WHILE A feature’s size: number of nodes of the path

  18. Patterns’ Feature Weighting • Significance of feature f in pattern P (tf-idf): • Nf,P: number of occurrences of f in P • NP: number of features in P • Nf: number of patterns containing f • N: number of patterns in pattern database • Popularity of pattern P: Pr(P)

  19. Storing Patterns • GraPacc stores each pattern with • Features and their weights • Code templates • Inverted indexing is applied to patterns via their features.

  20. Query Processing Patterns {P} Query features {fq} Pattern features {fp} Pattern Database Searching & Ranking Query Processing Query Q Ranked list of patterns {P} Filled-in code Code Completion

  21. Partial Program Analysis [Dagenais et al. OOPSLA ’08] • Tokenizing and parsing the code under editing into an Abstract Syntax Tree (AST) Method declaration Method body public void readText(){ FileReaderfReader; BufferedReaderbReader = new BufferedReader(fReader); } Declaration Declaration Assignment fReader bReader Initialization fReader

  22. Building Groum • Query’s AST is used to build query’s Groum FileReader BufferedReader.new Method declaration Method body BufferedReader Declaration Declaration Assignment fReader bReader Initialization fReader

  23. Feature Extraction FileReader FileReader BufferedReader.new BufferedReader FileReaderBufferedReader.new BufferedReader.newBufferedReader FileReader.newBufferedReader.newBufferedReader BufferedReader.new BufferedReader Remaining textual tokens

  24. Weighting Query’s Features • Context-sensitive: taking into account the current editing point and surrounding code • Features are weighted to represent their significance in a query Centrality-based factor Location-based factor (Distance to the focus point) Size-based factor

  25. Searching and Ranking Patterns {P} Query features {fq} Pattern features {fp} Pattern Database Searching & Ranking Query Processing Query Q Ranked list of patterns {P} Filled-in code Code Completion

  26. Searching and Ranking Pattern P1 rel(Pi, Q) Pattern P2 Query Q ….. Pattern Pn

  27. Pattern Relevancy • Relevancy between pattern P and query Q is defined as: Combined relevancy Popularity of P Weighted maximum matching Relevancy between pairs of features p in P and q in Q

  28. Feature Relevancy The relevancy between two features p in pattern P and q in query Q: Similarity between p and q Significance of p in P Weight of q in Q

  29. Feature Similarity The similarity between two size-k features: • Feature p of P: p1p2..pk • Feature q of Q: q1q2..qk name-based similarity between the pair of elements pi and qi Example: p: FileReaderBufferedReader.new q: FileReadBufferedReader.new

  30. Code Completion Patterns {P} Query features {fq} Pattern features {fp} Pattern Database Searching & Ranking Query Processing Query Q Ranked list of patterns {P} Filled-in code Code Completion

  31. Aligning Nodes Query Pattern FileReader.new String.new String FileReader FileReader.new Maximal alignment FileReader BufferedReader.new BufferedReader.new BufferedReader BufferedReader BufferedReader.ready FOR maximum weighted bipartite matching BufferedReader.close FileReader.close

  32. Inserting Nodes & Edges Query Pattern Inserting unaligned nodes FileReader.new String.new String FileReader FileReader.new Aligned nodes FileReader BufferedReader.new BufferedReader.new BufferedReader BufferedReader BufferedReader.ready FOR BufferedReader.close FileReader.close

  33. Empirical Evaluation • Goal: measure how accurately GraPaccrecommends and fills in the current code • 28 subject systems: • 4 systems for mining patterns • 24 systems for testing • Using 197 patterns from java.util and java.io

  34. Simulation divide and take the first half GraPacc’s Recomm- endation • Comparing and calculating accuracy • # shared nodes between recommended and real code • # nodes that appear in real code • # nodes that appear in recommended code

  35. Accuracy results 71% of API usages are covered by API usage patterns. Approximately 1.2 patterns are recommended for 1 test method.

  36. Conclusions • Code completion using graph-based patterns and context information • Future work includes user study and adaptive code completion • Demo on Friday10:45-12:45 GraPacc http://home.engineering.iastate.edu/~anhnt/Research/GraPacc

  37. Return-type-based Ranking

  38. Sources of inaccuracies • Customization of a pattern (e.g., two consecutive readLine method calls) • Don't aim to replace developers • Developers can easily customize • Lack of patterns in database • API usage spans 2 methods

More Related