Skip to content

Commit

Permalink
feat(rust): init for Rust worker
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 2, 2024
1 parent 313320e commit b30b1dd
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 14 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ chapi-domain = { group = "com.phodal.chapi", name = "chapi-domain", version.ref
chapi-java = { group = "com.phodal.chapi", name = "chapi-ast-java", version.ref = "chapi" }
chapi-kotlin = { group = "com.phodal.chapi", name = "chapi-ast-kotlin", version.ref = "chapi" }
chapi-typescript = { group = "com.phodal.chapi", name = "chapi-ast-typescript", version.ref = "chapi" }
chapi-rust = { group = "com.phodal.chapi", name = "chapi-ast-rust", version.ref = "chapi" }


# ArchGurad
Expand Down
4 changes: 3 additions & 1 deletion unit-core/src/main/kotlin/cc/unitmesh/core/SupportedLang.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum class SupportedLang(val extension: String) {
JAVA("java"),
TYPESCRIPT("ts"),
KOTLIN("kt"),
RUST("rs")
;

companion object {
Expand All @@ -12,12 +13,13 @@ enum class SupportedLang(val extension: String) {
"java" -> JAVA
"typescript" -> TYPESCRIPT
"kotlin" -> KOTLIN
"rust" -> RUST
else -> null
}
}

fun all(): List<SupportedLang> {
return listOf(JAVA, TYPESCRIPT, KOTLIN)
return listOf(JAVA, TYPESCRIPT, KOTLIN, RUST)
}
}
}
1 change: 1 addition & 0 deletions unit-picker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation(libs.chapi.java)
implementation(libs.chapi.kotlin)
implementation(libs.chapi.typescript)
implementation(libs.chapi.rust)

implementation(libs.archguard.scanner.core)
implementation(libs.archguard.analyser.estimate)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.unitmesh.pick.builder

import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.SupportedLang.*
import cc.unitmesh.core.comment.DocInstruction
import cc.unitmesh.core.completion.TypedIns
import cc.unitmesh.core.completion.TypedInsBuilder
Expand All @@ -15,11 +16,13 @@ class DocumentationTypedInsBuilder(val context: JobContext) : TypedInsBuilder {
override fun build(container: CodeContainer): List<TypedIns> {
val language = context.project.language
return when (language) {
SupportedLang.TYPESCRIPT -> TODO()
SupportedLang.JAVA -> javaCommentBuilder.build(context.job.code, container)
SupportedLang.KOTLIN -> {
JAVA -> javaCommentBuilder.build(context.job.code, container)
KOTLIN -> {
kotlinCommentBuilder.build(context.job.code, container)
}

TYPESCRIPT -> TODO()
RUST -> TODO()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.core.SupportedLang
import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.builder.unittest.java.JavaTestCodeService
import cc.unitmesh.pick.builder.unittest.kotlin.KotlinTestCodeService
import cc.unitmesh.pick.builder.unittest.rust.RustTestCodeService
import cc.unitmesh.pick.builder.unittest.typescript.TypeScriptTestCodeService
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct
Expand Down Expand Up @@ -35,6 +36,7 @@ interface UnitTestService {
SupportedLang.JAVA -> JavaTestCodeService(job)
SupportedLang.TYPESCRIPT -> TypeScriptTestCodeService(job)
SupportedLang.KOTLIN -> KotlinTestCodeService(job)
SupportedLang.RUST -> RustTestCodeService(job)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cc.unitmesh.pick.builder.unittest.rust

import cc.unitmesh.core.unittest.TypedTestIns
import cc.unitmesh.pick.builder.unittest.base.UnitTestService
import cc.unitmesh.pick.worker.job.JobContext
import chapi.domain.core.CodeDataStruct

class RustTestCodeService(val job: JobContext): UnitTestService {
override fun isApplicable(dataStruct: CodeDataStruct): Boolean {
return false
}

override fun build(dataStruct: CodeDataStruct): List<TypedTestIns> {
TODO()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class TypeScriptTestCodeService(val job: JobContext): UnitTestService {
}

override fun build(dataStruct: CodeDataStruct): List<TypedTestIns> {
return emptyList()
TODO()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import org.archguard.scanner.core.sca.CompositionDependency

class TestFrameworkIdentifier(val language: SupportedLang, private val dependencies: List<CompositionDependency>) {
fun identify(): List<String> {
return when (language.name.lowercase()) {
"java" -> identifyJava()
"typescript" -> identifyTypescript()
else -> listOf()
return when (language) {
SupportedLang.JAVA -> identifyJava()
SupportedLang.KOTLIN -> identifyJava()
SupportedLang.TYPESCRIPT -> identifyTypescript()
SupportedLang.RUST -> TODO()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SimilarChunksStrategyBuilder(private val context: JobContext) : CodeStrate
SupportedLang.JAVA -> JavaSimilarChunker(context.fileTree)
SupportedLang.KOTLIN -> JavaSimilarChunker(context.fileTree)
SupportedLang.TYPESCRIPT -> TypeScriptSimilarChunker(context.fileTree)
SupportedLang.RUST -> TODO()
}

val builders = completionBuilders(context.completionBuilderTypes, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cc.unitmesh.pick.worker.base.LangWorker
import cc.unitmesh.pick.worker.job.InstructionFileJob
import cc.unitmesh.pick.worker.lang.JavaWorker
import cc.unitmesh.pick.worker.lang.KotlinWorker
import cc.unitmesh.pick.worker.lang.RustWorker
import cc.unitmesh.pick.worker.lang.TypescriptWorker
import org.archguard.scanner.analyser.ScaAnalyser
import org.archguard.scanner.core.client.ArchGuardClient
Expand All @@ -19,11 +20,14 @@ import java.util.*


class WorkerManager(private val context: WorkerContext) {
private val workers: Map<SupportedLang, LangWorker> = mapOf(
SupportedLang.JAVA to JavaWorker(context),
SupportedLang.KOTLIN to KotlinWorker(context),
SupportedLang.TYPESCRIPT to TypescriptWorker(context),
)
private val workers: Map<SupportedLang, LangWorker> = SupportedLang.all().map {
it to when (it) {
SupportedLang.JAVA -> JavaWorker(context)
SupportedLang.KOTLIN -> KotlinWorker(context)
SupportedLang.TYPESCRIPT -> TypescriptWorker(context)
SupportedLang.RUST -> RustWorker(context)
}
}.toMap()

private val thresholdChecker: ThresholdChecker = ThresholdChecker(context)

Expand Down Expand Up @@ -79,6 +83,27 @@ class WorkerManager(private val context: WorkerContext) {
return true
}

/**
* Executes all workers and returns a list of instructions.
*
* This method starts all the workers asynchronously and collects the results. If any worker encounters an exception
* during execution, it will be caught and an empty list will be returned for that worker. The results from all workers
* are then flattened into a single list.
*
* The method then filters the output based on a threshold. Each instruction is checked against the threshold checker,
* and if it meets the threshold, it is added to the final list. Instructions that do not meet the threshold are skipped.
*
* The final list is a map that groups instructions by their completion builder type. Each type has a list of instructions
* associated with it. The final list is created using an EnumMap, where the keys are the completion builder types.
*
* If any instructions were skipped due to not meeting the threshold, a log message is printed indicating the number of
* skipped instructions and the total number of instructions.
*
* Finally, the final list is shuffled and truncated to the desired completion type size for each type. The shuffled and
* truncated lists are then flattened into a single list and returned.
*
* @return a list of instructions after executing all workers and applying the threshold filter
*/
suspend fun runAll(): List<Instruction> {
val results = workers.map { (_, worker) ->
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cc.unitmesh.pick.worker.lang

import cc.unitmesh.pick.ext.CodeDataStructUtil
import cc.unitmesh.pick.worker.WorkerContext
import cc.unitmesh.pick.worker.base.LangWorker
import cc.unitmesh.pick.worker.job.InstructionFileJob
import chapi.ast.rustast.RustAnalyser
import org.slf4j.Logger

class RustWorker(override val context: WorkerContext) : LangWorker {
override val jobs: MutableList<InstructionFileJob> = mutableListOf()
override val fileTree: HashMap<String, InstructionFileJob> = hashMapOf()
override val logger: Logger = org.slf4j.LoggerFactory.getLogger(RustWorker::class.java)

override fun prepareJob(job: InstructionFileJob) {
this.jobs.add(job)
try {
val container = RustAnalyser().analysis(job.code, job.fileSummary.location)
job.codeLines = job.code.lines()
container.DataStructures.map { ds ->
ds.Imports = container.Imports

ds.Content = CodeDataStructUtil.contentByPosition(job.codeLines, ds.Position)
ds.Functions.map {
it.apply {
it.Content = CodeDataStructUtil.contentByPosition(job.codeLines, it.Position)
}
}
}

job.container = container
} catch (e: Exception) {
e.printStackTrace()
}
}
}

0 comments on commit b30b1dd

Please sign in to comment.