Skip to content

Commit

Permalink
fix: fix after block issue & write test for after block
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 20, 2023
1 parent 44f35fd commit f7afbc0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import chapi.domain.core.CodeFunction
class AfterBlockCodeCompletionBuilder(val context: JobContext) : CompletionBuilder {
override fun build(function: CodeFunction): List<CodeCompletionIns> {
val position = function.Position
val beforeCursor = context.job.codeLines.subList(0, position.StartLine).joinToString("\n")
val codeLines = context.job.codeLines
val beforeCursor = codeLines.subList(0, position.StopLine).joinToString("\n")
var afterCursor = codeLines.subList(position.StopLine, codeLines.size).joinToString("\n")

// pick after lines
val stopLine = if (position.StopLine == 0) {
context.job.codeLines.size
} else {
position.StopLine
if (position.StopLine == 0) {
afterCursor = codeLines.joinToString("\n")
}

val afterCursor = context.job.codeLines.subList(position.StartLine, stopLine).joinToString("\n")
// create line break by os
val lineBreak = System.lineSeparator()
if (afterCursor == "}$lineBreak") {
return emptyList()
}

return listOf(CodeCompletionIns(beforeCursor, afterCursor))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cc.unitmesh.pick.prompt.completion;

import cc.unitmesh.pick.builder.BuilderConfig
import cc.unitmesh.pick.builder.InstructionFileJob
import cc.unitmesh.pick.prompt.CodeCompletionIns
import cc.unitmesh.pick.prompt.JobContext
import chapi.domain.core.CodeFunction
import chapi.domain.core.CodePosition
import io.kotest.matchers.shouldBe
import org.archguard.scanner.analyser.count.FileJob
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class AfterBlockCodeCompletionBuilderTest {

@Test
fun should_success_split_after_block_code() {
val codeFunction = CodeFunction(
Position = CodePosition(1, 0, 3)
)
val codeLines = """
fun myFunction() {
println("Hello, world!")
}
// other code here
""".trimIndent().lines()
val job = InstructionFileJob(
fileSummary = FileJob(),
codeLines = codeLines,
code = codeLines.joinToString("\n")
)
val jobContext = JobContext(job, emptyList(), hashMapOf("" to job), BuilderConfig(), emptyList())
val builder = AfterBlockCodeCompletionBuilder(jobContext)

val result = builder.build(codeFunction)

result shouldBe listOf(
CodeCompletionIns(
beforeCursor = codeLines.subList(0, 3).joinToString("\n"),
afterCursor = "// other code here"
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cc.unitmesh.pick.prompt.completion;

import cc.unitmesh.pick.builder.BuilderConfig
import cc.unitmesh.pick.builder.InstructionFileJob
import cc.unitmesh.pick.prompt.CodeCompletionIns
import cc.unitmesh.pick.prompt.JobContext
import chapi.domain.core.CodeFunction
import chapi.domain.core.CodePosition
import io.kotest.matchers.shouldBe
import org.archguard.scanner.analyser.count.FileJob
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

class InBlockCodeCompletionBuilderTest {

@Test
fun should_return_single_CodeCompletionIns() {
val codeFunction = CodeFunction(
Position = CodePosition(1, 0, 3)
)
val codeLines = """
fun myFunction() {
println("Hello, world!")
}
// other code here
""".trimIndent().lines()
val job = InstructionFileJob(
fileSummary = FileJob(),
codeLines = codeLines,
code = codeLines.joinToString("\n")
)
val jobContext = JobContext(job, emptyList(), hashMapOf("" to job), BuilderConfig(), emptyList())
val builder = InBlockCodeCompletionBuilder(jobContext)

// when
val result = builder.build(codeFunction)

// then
result.size shouldBe 1
result[0] shouldBe CodeCompletionIns(
beforeCursor = codeLines.subList(0, 1).joinToString("\n"),
afterCursor = codeLines.subList(1, 3).joinToString("\n")
)
}
}

0 comments on commit f7afbc0

Please sign in to comment.