Abstract
While pre-trained language models (LM) for code have achieved great success in code completion, they generate code conditioned only on the contents within the file, i.e., in-file context, but ignore the rich semantics in other files within the same project, i.e., project-level cross-file context, a critical source of information that is especially useful in modern modular software development. Such overlooking constrains code LMs’ capacity in code completion, leading to unexpected behaviors such as generating hallucinated class member functions or function calls with unexpected arguments. In this work, we propose CoCoMIC, a novel framework that jointly learns the in-file and cross-file context on top of code LMs. To empower CoCoMIC, we develop CCFinder, a static-analysis-based tool that locates and retrieves the most relevant project-level cross-file context for code completion. CoCoMIC successfully improves the existing code LM with a 33.94% relative increase in exact match and 28.69% in identifier matching for code completion when the cross-file context is provided. Finally, we perform a series of ablation studies and share valuable insights for future research on integrating cross-file context into code LMs.
Introduction
Most current code completion models suffer from "tunnel vision," focusing exclusively on the current file while ignoring the broader project structure. This limitation is increasingly problematic in modern software engineering, which relies heavily on modularity and cross-file dependencies. CoCoMIC (Code Completion by Jointly Modeling In-file and Cross-file Context) addresses this by integrating project-wide semantics into the generation process.
The Problem: Limitations of In-file Context
Large Language Models (LLMs) for code are typically trained to predict the next token based on "in-file" context—the code immediately preceding the cursor. However, if a model lacks access to the definitions of imported classes or functions, it often resorts to hallucination. For instance, it might guess a member function name or pass incorrect arguments because it cannot "see" the signature defined in a separate utility file.
Methods: CCFinder and Joint Modeling
The CoCoMIC framework consists of two primary components designed to bridge the gap between local and global context.
CCFinder: The Retriever
CCFinder is a static-analysis tool that constructs a Project Context Graph. This graph maps entities such as files, classes, functions, and global variables, along with their inter-relationships. When a completion is triggered, CCFinder analyzes the import statements in the active file and performs a 2-hop search on the graph to retrieve the most relevant definitions from the rest of the project.
CoCoMIC: The Model
To avoid exceeding context limits or overwhelming the model with raw text, CoCoMIC employs two key techniques:
- Entity Compression: Each retrieved cross-file entity is encoded into a single summary vector using a special
[SUM]token. This allows the model to process information from dozens of files simultaneously within a compact representation. - Joint Attention: CoCoMIC utilizes a modified Transformer architecture. At every layer, the model performs attention over both the "compressed" cross-file tokens and the "concrete" in-file tokens, allowing it to dynamically weigh project-wide knowledge against local logic.
Experimental Setup and Datasets
The researchers curated a new large-scale dataset from the Python Package Index (PyPI), comprising 60,891 permissively licensed Python projects. They specifically filtered for projects with meaningful internal dependencies and ensured no information leakage between training and evaluation sets. The final benchmark consisted of 6,888 high-quality prompts where cross-file context was demonstrably necessary for a correct completion.
Results and Key Insights
Using CodeGen-350M-Mono as the backbone, CoCoMIC demonstrated significant performance gains:
- Exact Match (EM): Achieved a 33.94% relative increase in accuracy.
- Identifier Match: Improved by 28.69%, indicating superior accuracy in naming external functions and variables.
- Perplexity: A decrease in overall uncertainty suggests that cross-file context makes code significantly more predictable for the LM.
Key insights from the study include:
- The Power of [SUM]: Representing entire functions or classes with a dedicated summary token was more effective than standard pooling techniques, acting as a "semantic hook" for APIs.
- Relational Hints: Prepending hierarchical paths (e.g.,
# file.class.method) to retrieved code helped the model map imports to their definitions. - The 2-Hop Advantage: Retrieving only direct imports (1-hop) was insufficient; the model often required the members of imported classes, necessitating a 2-hop search.
Conclusion and Future Work
CoCoMIC proves that project-level context is vital for high-fidelity code completion. While the current implementation is Python-specific and shows a slight performance penalty on files with no dependencies, it sets a new standard for context-aware code modeling. Future work will focus on cross-language support and improving robustness when external context is unavailable.
Summary updated