Code comment generation aims to produce concise natural-language comments that help developers understand and maintain source code. Recent work has expanded the input to include information outside the target method, but most approaches still treat context as a single pool of evidence. This is limiting in multi-intent summarization, because a comment that explains what a method does may require different evidence from a comment that explains how to use the method or why it exists. We propose IntentContextCS, a framework for multi-intent code comment generation that selects context according to the intended documentation goal. The framework combines three selection signals: token overlap between the target method and context types, semantic similarity based on CodeBERT embeddings, and reasoning-based context ranking using large language models. These signals are used to construct an intent-specific policy over five context types: callers, callees, sibling methods, class attributes, and project-level information. On the CodeSearchNet Java dataset, the empirical analysis shows that intent categories have different context profiles. For example, caller contexts are most useful for usage-oriented comments, whereas callee and sibling contexts are more informative for comments about functionality and implementation. In the generation experiments, IntentContextCS improves METEOR by 8.9%, USE by 3.5%, and SIDE by 3.0% over the strongest full-context LLM baseline, while using 54% fewer context tokens. These results suggest that, for code comment generation, selecting the right context is more effective than simply providing more context.
更多
查看译文
关键词
Code summarization,Large language models,Developer intent,Context selection,Code comment generation