Skip to content

Commit 54838dd

Browse files
authored
feat: kotlin clf (#130) (APP-149) (#148)
* wip: add kotlin extractor * wip: kotlin clf tests * wip: fix pr
1 parent 84d2948 commit 54838dd

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

src/main/kotlin/app/extractors/Extractor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Extractor : ExtractorInterface {
2828
in GoExtractor.FILE_EXTS -> GoExtractor()
2929
in ObjectiveCExtractor.FILE_EXTS -> ObjectiveCExtractor()
3030
in SwiftExtractor.FILE_EXTS -> SwiftExtractor()
31+
in KotlinExtractor.FILE_EXTS -> KotlinExtractor()
3132
else -> CommonExtractor()
3233
}
3334
}

src/main/kotlin/app/extractors/JavaExtractor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class JavaExtractor : ExtractorInterface {
7979
override fun tokenize(line: String): List<String> {
8080
val importRegex = Regex("""^(.*import)\s[^\n]*""")
8181
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
82+
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
8283
var newLine = importRegex.replace(line, "")
8384
newLine = commentRegex.replace(newLine, "")
85+
newLine = packageRegex.replace(newLine, "")
8486
return super.tokenize(newLine)
8587
}
8688

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2017 Sourcerer Inc. All Rights Reserved.
2+
// Author: Liubov Yaronskaya ([email protected])
3+
4+
package app.extractors
5+
6+
import app.model.CommitStats
7+
import app.model.DiffFile
8+
9+
class KotlinExtractor : ExtractorInterface {
10+
companion object {
11+
val LANGUAGE_NAME = "kotlin"
12+
val FILE_EXTS = listOf("kt")
13+
val LIBRARIES = ExtractorInterface.getLibraries(LANGUAGE_NAME)
14+
val evaluator by lazy {
15+
ExtractorInterface.getLibraryClassifier(LANGUAGE_NAME)
16+
}
17+
}
18+
19+
override fun extract(files: List<DiffFile>): List<CommitStats> {
20+
files.map { file -> file.language = LANGUAGE_NAME }
21+
return super.extract(files)
22+
}
23+
24+
override fun extractImports(fileContent: List<String>): List<String> {
25+
val imports = mutableSetOf<String>()
26+
27+
val regex = Regex("""import\s+(\w+[.\w+]*)""")
28+
fileContent.forEach {
29+
val res = regex.find(it)
30+
if (res != null) {
31+
val importedName = res.groupValues[1]
32+
LIBRARIES.forEach { library ->
33+
if (importedName.startsWith(library)) {
34+
imports.add(library)
35+
}
36+
}
37+
}
38+
}
39+
40+
return imports.toList()
41+
}
42+
43+
override fun tokenize(line: String): List<String> {
44+
val importRegex = Regex("""^(.*import)\s[^\n]*""")
45+
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
46+
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
47+
var newLine = importRegex.replace(line, "")
48+
newLine = commentRegex.replace(newLine, "")
49+
newLine = packageRegex.replace(newLine, "")
50+
return super.tokenize(newLine)
51+
}
52+
53+
override fun getLineLibraries(line: String,
54+
fileLibraries: List<String>): List<String> {
55+
56+
return super.getLineLibraries(line, fileLibraries, evaluator, LANGUAGE_NAME)
57+
}
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
com.winterbe.expekt
2+
com.github.vassilibykov.adventkt
3+
me.lazmaid.kraph
4+
com.github.kittinunf.fuel
5+
imgui
6+
org.mapdb
7+
uy.kohesive.kovert
8+
io.reactivex.rxkotlin
9+
com.compass.snail
10+
io.kweb
11+
com.github.andrewoma.kwery
12+
com.github.pgutkowski.kgraphql
13+
io.vertx
14+
org.spekframework.spek2
15+
com.oneeyedmen.konsent
16+
com.github.salomonbrys.kotson
17+
org.jire.kton
18+
io.kotlintest
19+
ktx
20+
org.kotlinprimavera
21+
org.kottpd
22+
com.almasb.fxgl
23+
org.wasabifx.wasabi
24+
com.fboldog.ext4klaxon
25+
net.yested
26+
ua.com.lavi.komock
27+
kotlinx.nosql
28+
spark
29+
com.github.kittinunf.result
30+
com.nivabit.kuery
31+
io.thelandscape.krawler
32+
io.tekniq
33+
com.beust.klaxon
34+
com.github.shyiko.levelkt
35+
io.javalin
36+
kategory
37+
tornadofx
38+
kotliquery
39+
khttp
40+
ovr
41+
com.hexagonkt
42+
com.nhaarman.mockito_kotlin
43+
io.ktor
44+
com.codepoetics.klenses
45+
io.polymorphicpanda.kspec
46+
com.natpryce.hamkrest
47+
org.http4k
48+
com.vaadin
49+
io.mockk
50+
kara
51+
org.funktionale
52+
com.github.fluidsonic.fluid.json
53+
com.squareup.sqldelight
54+
org.amshove.kluent
55+
com.danneu.kog
56+
org.jetbrains.exposed

src/test/kotlin/test/tests/extractors/ExtractorTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ class ExtractorTest : Spek({
104104
assertExtractsLineLibraries("grpc",
105105
line, CExtractor())
106106
}
107+
108+
it("kotlin extractor extracts the library") {
109+
val line = "FuelManager.instance.apply {"
110+
assertExtractsLineLibraries("com.github.kittinunf.fuel",
111+
line, KotlinExtractor())
112+
}
107113
}
108114

109115
given("code line doesn't use libraries" ) {
@@ -161,6 +167,11 @@ class ExtractorTest : Spek({
161167
val line = "int main(int argc, char **argv) {"
162168
assertExtractsNoLibraries(line, CExtractor())
163169
}
170+
171+
it("kotlin extractor returns empty list") {
172+
val line = "val password = \"P@\$\\\$vv0|2|)\""
173+
assertExtractsNoLibraries(line, KotlinExtractor())
174+
}
164175
}
165176

166177
given("import name.h") {
@@ -179,6 +190,12 @@ class ExtractorTest : Spek({
179190
}
180191
}
181192

193+
given("line contains import") {
194+
it("kotlin extractor extracts import") {
195+
val line = "import kategory.optics.*"
196+
val lib = "kategory"
197+
assertExtractsImport(lib, line, KotlinExtractor())
198+
182199
given("import cv2 or cv") {
183200
it("imports opencv") {
184201
val lib = "opencv"

0 commit comments

Comments
 (0)