|
| 1 | +// Copyright 2018 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Tuomas Hietanen |
| 3 | +// Author: Liubov Yaronskaya ([email protected]) |
| 4 | + |
| 5 | +package app.extractors |
| 6 | + |
| 7 | +import app.model.CommitStats |
| 8 | +import app.model.DiffFile |
| 9 | + |
| 10 | +class FSharpExtractor : ExtractorInterface { |
| 11 | + companion object { |
| 12 | + val LANGUAGE_NAME = "fsharp" |
| 13 | + val FILE_EXTS = listOf("fs", "fsx") |
| 14 | + // The behaviour of csharp library classifier is the same as for csharp. |
| 15 | + val LIBRARIES = ExtractorInterface.getLibraries("cs") |
| 16 | + val evaluator by lazy { |
| 17 | + ExtractorInterface.getLibraryClassifier("csharp") |
| 18 | + } |
| 19 | + val importRegex = Regex("""^.*open\s+(\w+[.\w+]*)""") |
| 20 | + val commentRegex = Regex("""^([^\n]*//)[^\n]*""") |
| 21 | + val extractImportRegex = Regex("""open\s+(\w+[.\w+]*)""") |
| 22 | + } |
| 23 | + |
| 24 | + override fun extract(files: List<DiffFile>): List<CommitStats> { |
| 25 | + files.map { file -> file.language = LANGUAGE_NAME } |
| 26 | + return super.extract(files) |
| 27 | + } |
| 28 | + |
| 29 | + override fun extractImports(fileContent: List<String>): List<String> { |
| 30 | + val imports = mutableSetOf<String>() |
| 31 | + |
| 32 | + fileContent.forEach { |
| 33 | + val res = extractImportRegex.find(it) |
| 34 | + if (res != null) { |
| 35 | + val importedName = res.groupValues[1] |
| 36 | + LIBRARIES.forEach { library -> |
| 37 | + if (importedName.startsWith(library)) { |
| 38 | + imports.add(library) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return imports.toList() |
| 45 | + } |
| 46 | + |
| 47 | + override fun tokenize(line: String): List<String> { |
| 48 | + val importRegex = Regex("""^.*open\s+(\w+[.\w+]*)""") |
| 49 | + val commentRegex = Regex("""^([^\n]*//)[^\n]*""") |
| 50 | + var newLine = importRegex.replace(line, "") |
| 51 | + newLine = commentRegex.replace(newLine, "") |
| 52 | + return super.tokenize(newLine) |
| 53 | + } |
| 54 | + |
| 55 | + override fun getLineLibraries(line: String, |
| 56 | + fileLibraries: List<String>): List<String> { |
| 57 | + |
| 58 | + return super.getLineLibraries(line, fileLibraries, evaluator, |
| 59 | + LANGUAGE_NAME) |
| 60 | + } |
| 61 | +} |
0 commit comments