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

Anh Tuan Nguyen, Tung Thanh Nguyen, Hoan Anh Nguyen, Ahmed Tamrawi, Hung Viet Nguyen, Jafar Al-Kofahi,
Tien N. Nguyen

Video Demo Downloads

GraPacc's Features

Code Completion Support


As a plugin, GraPacc operates within Eclipse's editor and can be invoked via Ctrl+Space shortcut key.


Figure 1. Pattern code completion


Figure 1 shows a snapshot from GraPacc. A developer wants to read the contents of a text file line-by-line via two Java classes, FileReader and BufferedReader, and a while loop. After typing while, (s)he invokes GraPacc for code completion. GraPacc then analyzes the currently editing code and extracts context-sensitive features including:
  1. The currently used data types (e.g. FileReader and BufferedReader),
  2. The control and branching structures (e.g. while, if), and
  3. The distances of those features with respect to the current editing position.

GraPacc uses those features to search in its database of API usage patterns to find and rank the usage patterns relevant to the current code. The developer can browse the list of returned patterns and observe the code completion preview for each pattern. For example, as shown in the ordered list of patterns in Figure 1, pattern 55003 is ranked first as it contains more similar features as those in the current code. With less similarity levels, other patterns are ranked lower in the recommended list (For illustration purpose, we also show the contents of three patterns. GraPacc shows only the content of the selected pattern). GraPacc will then fill in the current code with respect to the selected pattern.

There are several departure points from GraPacc in comparison with existing code completion tools:

  1. It takes into account all features in the context of the current code and is able to auto-complete more code with the entire selected usage pattern. In existing tools, only the currently editing variable is considered and a single method call is auto-completed.
  2. GraPacc considers multiple variables in different types in the current code (e.g. FileReader and BufferedReader), and a user can invoke it at any point within their code (e.g. within the while statement). In contrast, other tools require users to request code completion only on a selected variable.
  3. GraPacc is context-sensitive in which as the user changes his/her editing cursor to a different program element or types more words in the current code, the tool will adjust the ranking of returned usage patterns.


Figure 2. Ranking of Patterns based on Context-sensitive Features


In Figure 1, a user types the keyword while, thus, the patterns corresponding to the procedure of reading a text file via a while loop are ranked higher. Assume that (s)he instead wanted to read the file via a for loop and (s)he typed the keyword for. The patterns with FileReader, BufferedReader, and a for loop would be ranked higher (see Figure 2). As the developer selects a pattern P, GraPacc compares the current code with the pattern P and determines the tokens in P that need to be filled in. It then makes the appropriate transformations and modifications to the current code, instead of placing entire selected pattern at the end of the current text in the editor as in existing tools. For example, in Figure 1, as pattern 55003 is selected, GraPacc automatically completes the initializations for fileName and fReader variables, and the condition of the while loop, while it still maintains the initialization of variable bReader. Another advanced feature is that GraPacc can recognize a user&#39s switching between different usage patterns and rank those patterns accordingly to his/her intention based on the editing cursor.


Figure 3. Switching between Interleaving Patterns


For example, assume that in our current running example (Figure 3), the developer proceeds the task of reading a text file line-by-line via FileReader, BufferedReader, and a for loop. (S)he then continues with the task of tokenizing each line read from the text file via StringTokenizer inside the for loop. In this case, as (s)he invokes GraPacc and the cursor is at strTokenizer, the patterns relevant to StringTokenizer are ranked higher (see Figure 3a). However, if (s)he decides to switch his/her task to continue with closing the opened file as the cursor is at bReader in Figure 3b, GraPacc will rank the patterns of file reading higher and complete the code with bReader.close(); and fReader.close();. In other words, GraPacc is able to switch between two interleaving patterns: one for text file reading and one for string tokenizing depending on the editing cursor.