400 likes | 619 Views
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.
E N D
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 code completion invocation point Documentation on a proposed method List of recommended methods
Eclipse’s Built-in Code Completion Filled-in code
Source Code Completion • Plays an important role in modern IDEs • Supports developers by • Recommending relevant code • Automatically filling in code
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)
Template-based Code Completion Recommending a template without considering context
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
GraPacc Approach • Developing a pattern-oriented, context-sensitive code completion approach • Evaluating usefulness of our code completion method and tool
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
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
Context-sensitive Recommendation • Query • A code fragment under editing • A sequence of textual tokens • Often incomplete and may be un-parseable
Context-sensitive Recommendation a) b) Different cursor positions Potentially different recommendation lists
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
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
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
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
Features FileReader.new FileReader BufferedReader.new BufferedReader BufferedReader.ready WHILE FileReader.newFileReader FileReaderBufferedReader.new FileReader.newBufferedReader.new BufferedReader.new BufferedReader BufferedReader BufferedReader.ready BufferedReader.ready WHILE FileReader.newFileReaderBufferedReader.new FileReader.new BufferedReader.new BufferedReader FileReader.new BufferedReader.new BufferedReader.ready FileReader BufferedReader.new BufferedReader FileReader BufferedReader.new BufferedReader.ready BufferedReader.newBufferedReaderBufferedReader.ready BufferedReader.newBufferedReader.readyWHILE BufferedReaderBufferedReader.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
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)
Storing Patterns • GraPacc stores each pattern with • Features and their weights • Code templates • Inverted indexing is applied to patterns via their features.
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
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
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
Feature Extraction FileReader FileReader BufferedReader.new BufferedReader FileReaderBufferedReader.new BufferedReader.newBufferedReader FileReader.newBufferedReader.newBufferedReader BufferedReader.new BufferedReader Remaining textual tokens
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
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
Searching and Ranking Pattern P1 rel(Pi, Q) Pattern P2 Query Q ….. Pattern Pn
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
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
Feature Similarity The similarity between two size-k features: • Feature p of P: p1p2..pk • Feature q of Q: q1q2..qk name-based similarity between the pair of elements pi and qi Example: p: FileReaderBufferedReader.new q: FileReadBufferedReader.new
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
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
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
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
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
Accuracy results 71% of API usages are covered by API usage patterns. Approximately 1.2 patterns are recommended for 1 test method.
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
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