[Paper Review] CoCoMIC: Code Completion By Jointly Modeling In-file and Cross-file Context
CoCoMIC proposes a framework that jointly models in-file and cross-file context to improve code completion in pre-trained code language models. By using CCFinder to retrieve relevant cross-file context via import-based static analysis and integrating it through joint attention, CoCoMIC achieves a 33.94% relative improvement in exact match and 28.69% in identifier matching over in-file-only baselines.
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., cross-file context, a critical source of information that is especially useful in modern modular software development. Such overlooking constrains code language models' 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 develop a cross-file context finder tool, CCFINDER, that effectively locates and retrieves the most relevant cross-file context. We propose CoCoMIC, a framework that incorporates cross-file context to learn the in-file and cross-file context jointly on top of pretrained code LMs. CoCoMIC successfully improves the existing code LM with a 33.94% relative increase in exact match and a 28.69% relative increase in identifier matching for code completion when the cross-file context is provided.
Motivation & Objective
- To address the limitation of pre-trained code language models (LMs) that rely solely on in-file context, ignoring project-wide dependencies.
- To improve code completion accuracy by incorporating relevant cross-file context from the same project, which is often critical for correct code generation.
- To develop a scalable and efficient method for retrieving and integrating cross-file context without exceeding model context length.
- To design a joint modeling framework that treats in-file and cross-file context differently, respecting their distinct semantic roles.
- To empirically validate the effectiveness of cross-file context retrieval and integration on a real-world code completion benchmark.
Proposed method
- CCFinder is developed as a static code analysis tool that constructs a project context graph from source code, capturing entities (e.g., classes, functions) and their relationships.
- Given a code snippet, CCFinder identifies relevant cross-file context by analyzing import statements and retrieving neighboring entities in the project context graph.
- CoCoMIC compresses the retrieved cross-file context into a compact representation using a dedicated encoder module.
- During autoregressive generation, CoCoMIC employs a joint attention mechanism that attends to both the in-file context and the compressed cross-file context.
- The framework is built on top of existing pre-trained code LMs (e.g., CodeGen) and fine-tunes them end-to-end with joint context modeling.
- The model is evaluated on a custom dataset derived from PyPI, using exact match and identifier matching as metrics.
Experimental results
Research questions
- RQ1Can cross-file context significantly improve code completion performance beyond in-file context alone?
- RQ2How effectively can a static analysis tool like CCFinder retrieve relevant cross-file context based on import statements?
- RQ3What is the impact of jointly modeling in-file and cross-file context on code generation accuracy?
- RQ4How does the performance of CoCoMIC compare to in-file-only baselines and fine-tuned code LMs?
- RQ5What are the limitations of CoCoMIC when cross-file context is unavailable or when applied to larger models?
Key findings
- CCFinder successfully retrieves 27.07% more relevant context for code completion than in-file context alone, indicating its effectiveness in identifying project-level dependencies.
- CoCoMIC achieves a 33.94% relative improvement in exact match and a 28.69% relative improvement in identifier matching compared to an in-file-context-only baseline.
- The performance gain is consistent across different code completion scenarios, especially in cases requiring knowledge of imported classes and functions.
- When cross-file context is unavailable, CoCoMIC's performance drops by 5–7% relatively compared to a fine-tuned CodeGen model, highlighting the dependency on external context.
- The framework demonstrates strong potential for extension to larger models, though empirical validation on 2B, 6B, or 16B variants remains future work.
- The method effectively addresses context length constraints by compressing cross-file context instead of concatenating raw code.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.