Skip to content

Commit

Permalink
feat: add condition for check method
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jul 13, 2023
1 parent 631c818 commit ea6c87a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Expand Up @@ -16,8 +16,13 @@ class CodeCompleteAction : ChatBaseAction() {
val document = editor.document

val primaryCaret = editor.caretModel.primaryCaret;
val start = primaryCaret.selectionStart;
val end = primaryCaret.selectionEnd
val start = primaryCaret.selectionStart
var end = primaryCaret.selectionEnd

if (start == end) {
primaryCaret.setSelection(start, start + 1)
end = start + 1
}

return { response ->
WriteCommandAction.runWriteCommandAction(project) {
Expand Down
Expand Up @@ -2,7 +2,7 @@ package cc.unitmesh.devti.gui.chat

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.connector.ConnectorService
import cc.unitmesh.devti.parser.CodePostProcessor
import cc.unitmesh.devti.parser.JavaCodePostProcessor
import com.intellij.openapi.application.ApplicationManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -64,7 +64,7 @@ class ChatCodingService(var actionType: ChatBotActionType) {

if (match != null) return match.groupValues[1].trim()

return CodePostProcessor(prefixText, suffixText, content).execute()
return JavaCodePostProcessor(prefixText, suffixText, content).execute()
}
}

Expand Down
Expand Up @@ -3,10 +3,10 @@ package cc.unitmesh.devti.parser
/**
* will format complete code by prefix code and suffix code
*/
class CodePostProcessor(
val prefixCode: String,
val suffixCode: String,
val completeCode: String
class JavaCodePostProcessor(
private val prefixCode: String,
private val suffixCode: String,
private val completeCode: String
) {
private val spaceRegex = Regex("\\s+")

Expand Down Expand Up @@ -36,6 +36,11 @@ class CodePostProcessor(
result = result.split("\n").joinToString("\n") { " $it" }
}

// if suffix ends with "}", and complete code ends with "}\n}", then remove complete code's last "}"
if (result.endsWith("}\n}") and suffixCode.endsWith("}")) {
result = result.substring(0, result.length - 1)
}

// if lastLineSpaceCount > 0, then remove same space in result begin if exists
if (lastLineSpaceCount > 0) {
val spaceRegex = Regex("^\\s{$lastLineSpaceCount}")
Expand Down
Expand Up @@ -3,7 +3,7 @@ package cc.unitmesh.devti.parser
import org.junit.Assert.assertEquals
import org.junit.Test

class CodePostProcessorTest {
class JavaCodePostProcessorTest {
@Test
fun should_handle_with_no_indent_code() {
val prefix = "public class Test {\n"
Expand All @@ -15,7 +15,7 @@ public void test() {
}
""".trimIndent()

val postProcessor = CodePostProcessor(prefix, suffix, complete)
val postProcessor = JavaCodePostProcessor(prefix, suffix, complete)
val result = postProcessor.execute()

assertEquals(
Expand All @@ -33,7 +33,7 @@ public void test() {
val complete = """public void test() {
}
}"""
val postProcessor = CodePostProcessor(prefix, suffix, complete)
val postProcessor = JavaCodePostProcessor(prefix, suffix, complete)
val result = postProcessor.execute()

assertEquals(
Expand Down

0 comments on commit ea6c87a

Please sign in to comment.