Skip to content

Commit f7caff1

Browse files
committed
feat(java): add code prompter methods and documentation
This commit adds new methods and documentation to the JavaContextPrompter and JvmPromptStrategy classes. The JavaContextPrompter class now has a private additionContext property, and the JvmPromptStrategy class has a new getByFields method that retrieves lines matching a specified field regex. Additionally, the JvmPromptStrategy class now has a new advice method that generates code prompt text for implementing missing methods in a service file. These changes improve the functionality and usability of the code prompter feature in the Java context.
1 parent e13fc40 commit f7caff1

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ open class JavaCodeModifier : CodeModifier {
7474
}
7575

7676
WriteCommandAction.runWriteCommandAction(project) {
77-
val classEndOffset = rootElement.textRange.endOffset
78-
val document = PsiDocumentManager.getInstance(project).getDocument(rootElement.containingFile)
79-
document?.insertString(classEndOffset - 1, "\n ")
80-
document?.insertString(classEndOffset - 1 + "\n ".length, newTestMethod.text)
77+
try {
78+
rootElement.add(newTestMethod)
79+
} catch (e: Throwable) {
80+
val classEndOffset = rootElement.textRange.endOffset
81+
val document = PsiDocumentManager.getInstance(project).getDocument(rootElement.containingFile)
82+
document?.insertString(classEndOffset - 1, "\n ")
83+
document?.insertString(classEndOffset - 1 + "\n ".length, newTestMethod.text)
84+
}
8185
}
8286

8387
project.guessProjectDir()?.refresh(true, true)

java/src/main/kotlin/cc/unitmesh/idea/prompting/JavaContextPrompter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.intellij.temporary.similar.chunks.SimilarChunksWithPaths
2121
import kotlinx.coroutines.runBlocking
2222

2323
open class JavaContextPrompter : ContextPrompter() {
24-
protected var additionContext: String = ""
24+
private var additionContext: String = ""
2525
protected open val testDataBuilder: TestDataBuilder = JavaTestDataBuilder()
2626

2727
private val autoDevSettingsState = AutoDevSettingsState.getInstance()

java/src/main/kotlin/cc/unitmesh/idea/prompting/JvmPromptStrategy.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ class JvmPromptStrategy : PromptStrategy() {
120120
return methodCodes
121121
}
122122

123-
// get all line when match by field usage (by Regex)
123+
/**
124+
* Retrieves all lines from the given code string that match the specified field regex.
125+
*
126+
* @param codeString The code string to search for field usage.
127+
* @param firstFieldRegex The regular expression pattern to match the first field usage in each line.
128+
* @return A string containing all the lines that match the field regex, with additional code added before each line.
129+
*/
124130
private fun getByFields(
125131
codeString: @NlsSafe String,
126132
firstFieldRegex: Regex
@@ -144,6 +150,14 @@ class JvmPromptStrategy : PromptStrategy() {
144150
}
145151
}
146152

153+
/**
154+
* Generates a code prompt text for implementing missing methods in a given service file.
155+
*
156+
* @param serviceFile the PsiJavaFile representing the service file
157+
* @param usedMethod a list of used methods in the service file
158+
* @param noExistMethods a list of methods that do not exist in the service file
159+
* @return a CodePromptText object containing the generated code prompt text
160+
*/
147161
fun advice(serviceFile: PsiJavaFile, usedMethod: List<String>, noExistMethods: List<String>): CodePromptText {
148162
val code = serviceFile.text
149163
val filterNeedImplementMethods = usedMethod.filter {

0 commit comments

Comments
 (0)