|
| 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 | +} |
0 commit comments