Skip to content

Commit 1bf9fd6

Browse files
committed
fix: fix java gen doc return error format issue #99
1 parent c19f879 commit 1bf9fd6

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaLivingDocumentation.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.psi.*
1010
import com.intellij.psi.codeStyle.CodeStyleManager
1111
import com.intellij.psi.util.PsiTreeUtil
1212
import com.intellij.util.IncorrectOperationException
13+
import org.jetbrains.annotations.NonNls
1314

1415
class JavaLivingDocumentation : LivingDocumentation {
1516
override val parameterTagInstruction: String get() = "use @param tag"
@@ -38,8 +39,9 @@ class JavaLivingDocumentation : LivingDocumentation {
3839

3940
when (type) {
4041
LivingDocumentationType.COMMENT -> {
42+
val doc = Companion.preHandleDoc(newDoc)
4143
val psiElementFactory = JavaPsiFacade.getElementFactory(project)
42-
val newDocComment = psiElementFactory.createDocCommentFromText(newDoc)
44+
val newDocComment = psiElementFactory.createDocCommentFromText(doc)
4345

4446
if (target is PsiDocCommentOwner) {
4547
val oldDocComment = target.docComment
@@ -121,4 +123,17 @@ class JavaLivingDocumentation : LivingDocumentation {
121123
return methodsAndFieldsInRange
122124
}
123125

126+
companion object {
127+
fun preHandleDoc(newDoc: String): @NonNls String {
128+
// 1. remove ```java and ``` from the newDoc if it exists
129+
val newDocWithoutCodeBlock = newDoc.removePrefix("```java")
130+
.removePrefix("```")
131+
.removeSuffix("```")
132+
133+
// 2. lookup for the first line of the newDoc
134+
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDocWithoutCodeBlock, "/**", "*/")
135+
return fromSuggestion
136+
}
137+
}
138+
124139
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cc.unitmesh.idea.provider
2+
3+
import com.intellij.testFramework.LightPlatformTestCase
4+
import org.junit.Test
5+
6+
class JavaLivingDocumentationTest {
7+
@Test
8+
fun should_handle_when_llm_return_error_java_code() {
9+
val code = """```Java
10+
/**
11+
Converts a byte array to a short array.
12+
@param bytes the input byte array.
13+
@return the converted short array. If the input byte array is null, returns null.
14+
*/
15+
public short[] bytesToShort(byte[] bytes) {
16+
if (bytes == null) {
17+
return null;
18+
}
19+
short[] shorts = new short[bytes.length / 2];
20+
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
21+
return shorts;
22+
}
23+
"""
24+
25+
val result = JavaLivingDocumentation.preHandleDoc(code)
26+
LightPlatformTestCase.assertEquals(result, """/**
27+
Converts a byte array to a short array.
28+
@param bytes the input byte array.
29+
@return the converted short array. If the input byte array is null, returns null.
30+
*/""")
31+
}
32+
}

kotlin/src/main/kotlin/cc/unitmesh/kotlin/provider/KotlinLivingDocumentation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class KotlinLivingDocumentation : LivingDocumentation {
118118
try {
119119
doInsertComment(target, project, newDoc)
120120
} catch (e: Exception) {
121-
val fromSuggestion = buildDocFromSuggestion(newDoc, "/**", "*/")
121+
val fromSuggestion = LivingDocumentation.buildDocFromSuggestion(newDoc, "/**", "*/")
122122
if (fromSuggestion.isNotEmpty()) {
123123
try {
124124
doInsertComment(target, project, fromSuggestion)

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ interface LivingDocumentation {
3232
return selectionModel.selectionStart <= element.textRange.startOffset && element.textRange.endOffset <= selectionModel.selectionEnd
3333
}
3434

35-
fun buildDocFromSuggestion(suggestion: String, commentStart: String, commentEnd: String): String {
36-
val startIndex = suggestion.indexOf(commentStart)
37-
if (startIndex < 0) {
38-
return ""
39-
}
40-
41-
val docComment = suggestion.substring(startIndex)
42-
val endIndex = docComment.indexOf(commentEnd, commentStart.length)
43-
if (endIndex < 0) {
44-
return docComment + commentEnd
45-
}
46-
47-
val substring = docComment.substring(0, endIndex + commentEnd.length)
48-
return substring
49-
}
50-
5135
companion object {
5236
private val languageExtension: LanguageExtension<LivingDocumentation> =
5337
LanguageExtension("cc.unitmesh.livingDocumentation")
@@ -64,5 +48,21 @@ interface LivingDocumentation {
6448

6549
return null
6650
}
51+
52+
fun buildDocFromSuggestion(suggestion: String, commentStart: String, commentEnd: String): String {
53+
val startIndex = suggestion.indexOf(commentStart)
54+
if (startIndex < 0) {
55+
return ""
56+
}
57+
58+
val docComment = suggestion.substring(startIndex)
59+
val endIndex = docComment.indexOf(commentEnd, commentStart.length)
60+
if (endIndex < 0) {
61+
return docComment + commentEnd
62+
}
63+
64+
val substring = docComment.substring(0, endIndex + commentEnd.length)
65+
return substring
66+
}
6767
}
6868
}

0 commit comments

Comments
 (0)