Skip to content

Commit

Permalink
feat: update for related code completion
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 8, 2023
1 parent 5ee0c26 commit 3e540ae
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SimpleCodePicker(private val config: PickerConfig) : CodePicker {
logger.info("start picker")

val languageWorker = LanguageWorker()
val workerManager = WorkerManager(WorkerContext(config.builderTypes))
val workerManager = WorkerManager(WorkerContext(config.builderTypes, config.codeQualityTypes))
val walkdirChannel = Channel<FileJob>()
val summary = mutableListOf<Instruction>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ interface InstructionBuilder<T> {

fun hasIssue(node: CodeDataStruct, types: List<CodeQualityType>): Boolean {
return QualityAnalyser.create(types).map { analyser ->
analyser.analysis(listOf(node)).isNotEmpty()
}.any { it }
analyser.analysis(listOf(node))
}.flatten().isEmpty()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ class RelatedCodeCompletionBuilder(private val context: InstructionContext) :
context.fileTree[it.Source]?.container?.DataStructures
}
.flatten()
.filter {
hasIssue(it, context.qualityTypes)
}

// 2. convert all related data structure to uml
val relatedCode = relatedDataStructure.joinToString("\n", transform = CodeDataStruct::toUml)

return container.DataStructures.map { ds ->
val dataStructs = container.DataStructures.filter {
hasIssue(it, context.qualityTypes)
}

if (dataStructs.isEmpty()) {
return emptyList()
}

return dataStructs.map { ds ->
ds.Functions.map {
val position = it.Position
val beforeCursor = context.job.codeLines.subList(0, position.StartLine).joinToString("\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cc.unitmesh.pick.prompt.builder

import cc.unitmesh.pick.picker.InstructionJob
import cc.unitmesh.pick.prompt.InstructionContext
import cc.unitmesh.quality.CodeQualityType
import chapi.ast.javaast.JavaAnalyser
import chapi.domain.core.CodeDataStruct
import kotlinx.serialization.json.Json
import org.archguard.scanner.analyser.count.FileJob
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.io.File

class RelatedCodeCompletionBuilderTest {
private fun loadNodes(source: String): List<CodeDataStruct> {
return Json { ignoreUnknownKeys = true }.decodeFromString(
File(this.javaClass.classLoader.getResource(source)!!.file).readText()
)
}

@Test
fun shouldReturnEmptyWhenHasQualityIssue() {
val code = """package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/blog/get")
public String index() {
return "Greetings from Spring Boot!";
}
}"""
val container = JavaAnalyser().analysis(code, "HelloController.java")
val job = InstructionJob(
FileJob(
),
codeLines = code.lines(),
code = code,
container = container
)
val context = InstructionContext(
job = job,
qualityTypes = listOf(CodeQualityType.JavaController),
fileTree = hashMapOf("" to job)
)
val builder = RelatedCodeCompletionBuilder(context)
val result = builder.convert()

assertEquals(0, result.size)
}

@Test
fun shouldReturnOneItemWhenNoQualityIssue() {
val code = """package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}"""
val container = JavaAnalyser().analysis(code, "HelloController.java")
val job = InstructionJob(
FileJob(
),
codeLines = code.lines(),
code = code,
container = container
)
val context = InstructionContext(
job = job,
qualityTypes = listOf(CodeQualityType.JavaController),
fileTree = hashMapOf("" to job)
)
val builder = RelatedCodeCompletionBuilder(context)
val result = builder.convert()

assertEquals(1, result.size)
}
}

0 comments on commit 3e540ae

Please sign in to comment.