Code Completion using Keyword Queries in Java

Greg Little, Robert C. Miller · 2007

We propose a code completion technique that uses a small number of keywords, provided by the user in a code editor, to generate method calls and field references in Java. The keywords are interpreted as a search query over a space of possible expressions that might appear at the current cursor position in the user's code editor. This search space is ob- tained from the context around the cursor position, includ- ing local variables, methods, imported classes, and the type required at the cursor position. The syntactic and type con- straints of Java often make it possible to query this space with a small number of keywords, even though the search space may contain thousands of methods, and even though the generated code may require nested method calls and field references. We present two efficient algorithms for convert- ing a keyword query into generated code. The first uses a genetic algorithm to first arrange the user's keywords into an approximation of an abstract syntax tree; the second uses dy- namic programming to find method calls that generate each possible type. We evaluate both algorithms using a corpus of keyword queries mined automatically from open-source software. An interesting result from this evaluation is that the information content of method calls in Java is not partic- ularly high: even if punctuation is completely removed and tokens are split and reordered, more than 95% of the method calls we tested could be completely reconstructed based on the remaining keywords and type constraints.

Read the paper · More papers on PaperTik