Code completion aims to help improve developers' productivity by suggesting the next code tokens from a given context. Various approaches have been proposed to incorporate abstract syntax tree (AST) information for model training, ensuring that code completion is aware of the syntax of the programming languages. However, existing syntax-aware code completion approaches are not on-the-fly, as we found that for every two-thirds of characters that developers type, AST fails to be extracted because it requires the syntactically correct source code, limiting its practicality in real-world scenarios. On the other hand, existing on-the-fly code completion does not consider syntactic information yet. In this paper, we propose PyCoder to leverage token types, a kind of lightweight syntactic information, which is readily available and aligns with the natural order of source code. Our PyCoder is trained in a multi-task training manner so that by learning the supporting task of predicting token types during the training phase, the models achieve better performance on predicting tokens and lines of code without the need for token types in the inference phase. Comprehensive experiments show that PyCoder achieves the first rank on the CodeXGLUE leaderboard with an accuracy of 77.12% for the token-level predictions, which is 0.43%-24.25% more accurate than baselines. In addition, PyCoder achieves an exact match of 43.37% for the line-level predictions, which is 3.63%-84.73% more accurate than baselines. These results lead us to conclude that token type information (an alternative to syntactic information) that is rarely used in the past can greatly improve the performance of code completion approaches, without requiring the syntactically correct source code like AST-based approaches do. Our PyCoder is publicly available on HuggingFace and GitHub.
翻译:代码补全旨在通过从给定上下文中预测下一个代码标记来帮助提高开发者的生产力。已有多种方法将抽象语法树(AST)信息融入模型训练,确保代码补全能感知编程语言的语法结构。然而,现有语法感知的代码补全方法无法实现即时性,因为我们发现当开发者每输入三分之二字符时,AST便无法正常提取——这要求源代码在语法上完全正确,从而限制了其在真实场景中的实用性。另一方面,现有的即时代码补全尚未考虑语法信息。本文提出PyCoder,利用标记类型(一种轻量级语法信息),该信息易于获取且与源代码的自然顺序一致。我们采用多任务训练方式训练PyCoder,使模型在训练阶段通过预测标记类型的辅助任务学习后,能在推理阶段无需标记类型即可更优地预测代码标记和代码行。综合实验表明,PyCoder在CodeXGLUE排行榜上以77.12%的标记级预测准确率位居首位,比基线方法高出0.43%-24.25%。此外,PyCoder在代码行级预测上实现了43.37%的精确匹配率,比基线方法高出3.63%-84.73%。这些结果表明,过去鲜少使用的标记类型信息(作为语法信息的替代)能显著提升代码补全方法的性能,且无需像基于AST的方法那样依赖语法正确的源代码。我们的PyCoder已开源至HuggingFace和GitHub。