通过理解完成:利用多粒度理解引导代码生成
Completion by Comprehension: Guiding Code Generation with Multi-Granularity Understanding
AI总结:
CoCo通过多粒度理解引导代码生成,利用结构化上下文提升代码补全质量,实验表明其在多个基准测试中表现优异。
摘要:
作为从函数级到仓库级的代码补全任务,利用大规模代码库中的上下文信息成为核心挑战。然而,现有的检索增强生成(RAG)方法通常将代码视为普通自然语言,主要依赖于浅层语义匹配,而忽略了结构语义和代码特定的依赖关系。这限制了它们捕捉控制流和底层意图的能力,最终限制了生成代码的质量。因此,我们提出CoCo,一个新颖的框架,通过从大规模代码仓库中多粒度上下文中理解来实现代码补全。CoCo采用静态代码分析提取函数、文件和项目级别的结构化上下文,捕捉执行逻辑和语义依赖。然后,它采用基于图的多粒度上下文选择机制来过滤冗余信息并去除噪声。 consequently,信息被转换成自然语言,从而作为显式的上下文提示来指导后续的代码补全。此外,结构感知的代码重排机制确保在语义和结构层面的对齐。在CrossCodeEval和RepoEval基准上的广泛实验表明,CoCo在很大程度上超越了最先进的基线,实现了高达20.2%的EM提升。此外,该框架是模型无关的,可以无缝集成到现有方法中,从而带来显著的性能提升。
英文摘要:
As code completion task from function-level to repository-level, leveraging contextual information from large-scale codebases becomes a core challenge. However, existing retrieval-augmented generation (RAG) methods typically treat code as plain natural language, relying primarily on shallow semantic matching while overlooking structural semantics and code-specific dependencies. This limits their ability to capture control flow and underlying intent, ultimately constraining the quality of generated code. Therefore, we propose CoCo, a novel framework that enables code Completion by Comprehension of multi-granularity context from large-scale code repositories. CoCo employs static code analysis to extract structured context at the function, file, and project levels, capturing execution logic and semantic dependencies. It then adopts an graph-based multi-granularity context selection mechanism to filter out redundant information and remove noise. Consequently, the information is converted into natural language in a consistent manner, thereby functioning as explicit contextual prompts to guide subsequent code completion. Additionally, a structure-aware code re-ranker mechanism ensures alignment at both semantic and structural levels. Extensive experiments on CrossCodeEval and RepoEval benchmarks demonstrate that CoCo consistently surpasses state-of-the-art baselines, achieving up to 20.2% gains in EM. Moreover, the framework is model-agnostic and can be seamlessly integrated into existing methods, leading to significant performance.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。