Skip to content

Commit 19647cf

Browse files
committed
fix(java): Clean up PsiMethod and improve MethodContext creation
- 修复了 PsiMethod 的清理方法,并改进了 MethodContext 的创建过程。
1 parent 9d3340f commit 19647cf

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/LivingDocPromptBuilder.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ open class LivingDocPromptBuilder(
4848
protected val contextProviders = listOf(
4949
VariableContextProvider(false, false, false),
5050
ClassContextProvider(false),
51-
MethodContextProvider(false, false)
51+
MethodContextProvider(true, false)
5252
)
5353

5454
private fun contextInstruction(context: LLMCodeContext?): String? {
@@ -91,7 +91,10 @@ open class LivingDocPromptBuilder(
9191
""".trimIndent()
9292
}
9393

94-
return instruction.trimStart()
94+
// here is related context information of the method
95+
96+
val related = "\nHere is related context information of the method\n\n```${context.language}" + context.format() + "\n```\n"
97+
return instruction.trimStart() + related
9598
}
9699

97100
/**
@@ -139,7 +142,7 @@ open class LivingDocPromptBuilder(
139142
instruction.append("\n\nStart your documentation with ${startEndString.first} here, and ends with `${startEndString.second}`.\n")
140143
}
141144

142-
instruction.append(documentation.forbiddenRules.joinToString { "\n- $it" })
145+
instruction.append(documentation.forbiddenRules.joinToString { "\n- $it\n" })
143146
instruction.toString()
144147
}
145148
}

core/src/main/kotlin/cc/unitmesh/devti/provider/LivingDocumentation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface LivingDocumentation {
2727
fun findNearestDocumentationTarget(psiElement: PsiElement): PsiNameIdentifierOwner?
2828

2929
fun findDocTargetsInSelection(root: PsiElement, selectionModel: SelectionModel): List<PsiNameIdentifierOwner>
30+
3031
fun containsElement(selectionModel: SelectionModel, element: PsiElement): Boolean {
3132
return selectionModel.selectionStart <= element.textRange.startOffset && element.textRange.endOffset <= selectionModel.selectionEnd
3233
}

java/src/main/kotlin/cc/unitmesh/idea/context/JavaMethodContextBuilder.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cc.unitmesh.idea.context
33
import cc.unitmesh.devti.context.MethodContext
44
import cc.unitmesh.devti.context.builder.ClassContextBuilder
55
import cc.unitmesh.devti.context.builder.MethodContextBuilder
6+
import cc.unitmesh.idea.service.JavaRelatedContext.cleanUp
67
import cc.unitmesh.idea.service.JavaTypeUtil
78
import com.intellij.openapi.application.runReadAction
89
import com.intellij.psi.PsiElement
@@ -19,7 +20,7 @@ class JavaMethodContextBuilder : MethodContextBuilder {
1920
return null
2021
}
2122

22-
val parameterList = runReadAction { psiElement.parameters.mapNotNull { it.name }}
23+
val parameterList = runReadAction { psiElement.parameters.mapNotNull { it.name } }
2324
val variableContextList = parameterList.map { it }
2425

2526
val usagesList = if (gatherUsages) {
@@ -34,30 +35,32 @@ class JavaMethodContextBuilder : MethodContextBuilder {
3435
emptyList()
3536
}
3637

37-
return runReadAction { MethodContext(
38-
psiElement,
39-
text = psiElement.text,
40-
name = psiElement.name,
41-
signature = getSignatureString(psiElement),
42-
enclosingClass = psiElement.containingClass,
43-
language = psiElement.language.displayName,
44-
returnType = processReturnTypeText(psiElement.returnType?.presentableText),
45-
variableContextList,
46-
includeClassContext,
47-
usagesList,
48-
ios
49-
)}
38+
return runReadAction {
39+
MethodContext(
40+
psiElement,
41+
text = psiElement.text,
42+
name = psiElement.name,
43+
signature = getSignatureString(psiElement),
44+
enclosingClass = psiElement.containingClass,
45+
language = psiElement.language.displayName,
46+
returnType = processReturnTypeText(psiElement.returnType?.presentableText),
47+
variableContextList,
48+
includeClassContext,
49+
usagesList,
50+
ios
51+
)
52+
}
5053
}
5154

5255
private fun processReturnTypeText(returnType: String?): String? {
5356
return if (returnType == "void") null else returnType
5457
}
5558

5659
private fun getSignatureString(method: PsiMethod): String {
57-
val bodyStart = runReadAction { method.body?.startOffsetInParent ?: method.textLength }
58-
val text = runReadAction { method.text }
59-
val substring = text.substring(0, bodyStart)
60-
val trimmed = substring.replace('\n', ' ').trim()
60+
val text = runReadAction {
61+
cleanUp(method).text
62+
}
63+
val trimmed = text.replace('\n', ' ').trim()
6164
return trimmed
6265
}
6366
}

java/src/main/kotlin/cc/unitmesh/idea/service/JavaRelatedContext.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ object JavaRelatedContext {
6060
return psiElement
6161
}
6262

63+
fun cleanUp(psiMethod: PsiMethod): PsiMethod {
64+
val psiElement = psiMethod.copy() as PsiMethod
65+
psiElement.body?.delete()
66+
psiElement.docComment?.delete()
67+
return psiElement
68+
}
69+
6370
private fun findSuperClasses(psiClass: PsiClass): List<PsiClass> {
6471
val superClass = psiClass.superClass ?: return emptyList()
6572
if (isProjectContent(superClass)) {

0 commit comments

Comments
 (0)