Using Context and Hierarchy Features to Enhance Code Completion in Code Modification Scenarios

Linhai Li, Mingjie Xing, Ling Li

Abstract

Traditional code completion models often treat source code as a linear sequence of text, failing to capture the inherent hierarchical structure of programming languages. This paper introduces Contextual AST Serialization (CAS), a novel method that enhances code completion by combining post-order and level-order traversals of Abstract Syntax Trees (ASTs). By preserving structural depth and providing bi-directional context, CAS achieves a 15.8% improvement in Mean Reciprocal Rank (MRR) over existing state-of-the-art methods, particularly excelling in code modification scenarios.

Introduction

Modern Integrated Development Environments (IDEs) rely heavily on code completion to improve developer productivity. However, most existing tools suffer from two primary limitations. First, they exhibit a "prefix bias," primarily considering code preceding the cursor, which is insufficient for modification tasks where surrounding context is critical. Second, the process of flattening hierarchical ASTs into linear sequences for Transformer models often results in "structural loss," where vital depth and relationship information is discarded.

Mapping Python code to its Abstract Syntax Tree (AST) structure.

Methodology: Contextual AST Serialization (CAS)

To address these gaps, the researchers proposed CAS, which redefines how ASTs are serialized for neural networks. Unlike standard top-down approaches, CAS utilizes a post-order traversal (left-right-root). This ensures that a node is processed only after its children, providing the model with a complete semantic context of the node's operations.

Furthermore, CAS incorporates level-order traversal to capture the hierarchy. Each node is assigned a level value representing its depth in the tree. This information is integrated into the Transformer via Hierarchical Positional Encoding, allowing the attention mechanism to distinguish between nodes at different structural levels even in a flattened sequence.

Post-order traversal and level-order hierarchy used in the CAS serialization process.

Experimental Results

The CAS method was evaluated using the Py150k dataset, a standard benchmark consisting of 150,000 Python repositories. The results demonstrated significant performance gains over the baseline TravTrans model:

  • Overall Accuracy: A 15.8% improvement in MRR for next-token prediction.
  • Value Node Prediction: Accuracy for specific identifiers and variables increased from 60.4% to 77.7%.
  • Type Node Prediction: The most dramatic improvement was seen in node type prediction (e.g., Call, Attribute), which rose from 15.0% to 68.5%.

Ablation studies confirmed that the inclusion of level information was a key driver of these improvements, as removing structural features led to a measurable decline in performance.

Discussion and Future Work

While CAS significantly improves structural understanding, challenges remain. The model showed slightly lower performance on List type nodes, likely due to the dynamic and heterogeneous nature of Python lists. Additionally, while post-order traversal is excellent for child-node context, it may occasionally obscure direct parental relationships. Future research will focus on developing serialization algorithms that more holistically capture parent-child dynamics without compromising the gains in value prediction.

Conclusion

By prioritizing the hierarchical nature of code and leveraging bi-directional context, CAS represents a significant step forward for code completion in real-world modification scenarios. The integration of structural depth through level-order features proves essential for Transformer models to understand complex program logic.

Reference: Li, L., Xing, M., & Li, L. (2024). Using Context and Hierarchy Features to Enhance Code Completion in Code Modification Scenarios. Proceedings of SEKE 2024.

Summary updated

BibTeX
@inproceedings{li2024using,
  author = {Linhai Li and Mingjie Xing and Ling Li},
  title = {{Using Context and Hierarchy Features to Enhance Code Completion in Code Modification Scenarios}},
  booktitle = {SEKE},
  year = {2024},
  doi = {10.18293/seke2024-123},
}
Figure