Skip to content

Commit 8eb5f23

Browse files
Thoriumyaronskaya
authored andcommitted
feat: add fsharp support (#223) (#267)
* Added fsharp support, #223 * wip: fsharp libraries classifier to be the same as csharp
1 parent 40f7e4a commit 8eb5f23

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Extractor : ExtractorInterface {
2323
CppExtractor.FILE_EXTS +
2424
CSharpExtractor.FILE_EXTS +
2525
CssExtractor.FILE_EXTS +
26+
FSharpExtractor.FILE_EXTS +
2627
GoExtractor.FILE_EXTS +
2728
JavaExtractor.FILE_EXTS +
2829
JavascriptExtractor.FILE_EXTS +
@@ -47,6 +48,7 @@ class Extractor : ExtractorInterface {
4748
in CExtractor.FILE_EXTS -> CExtractor()
4849
in CppExtractor.FILE_EXTS -> CppExtractor()
4950
in CSharpExtractor.FILE_EXTS -> CSharpExtractor()
51+
in FSharpExtractor.FILE_EXTS -> FSharpExtractor()
5052
in GoExtractor.FILE_EXTS -> GoExtractor()
5153
in ObjectiveCExtractor.FILE_EXTS -> ObjectiveCExtractor()
5254
in SwiftExtractor.FILE_EXTS -> SwiftExtractor()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class ExtractorTest : Spek({
9393
line, CSharpExtractor())
9494
}
9595

96+
it("fsharp extractor extracts the library") {
97+
val line = "Algorithm = fun (h, v, i) -> ContrastiveDivergenceLearning(h, v)"
98+
assertExtractsLineLibraries("Accord",
99+
line, FSharpExtractor())
100+
}
101+
96102
it("php extractor extracts the library") {
97103
val line = "public function listRepos(string \$user, int \$limit): Call;"
98104
assertExtractsLineLibraries("Tebru\\Retrofit",
@@ -164,6 +170,11 @@ class ExtractorTest : Spek({
164170
assertExtractsNoLibraries(line, CSharpExtractor())
165171
}
166172

173+
it("fsharp extractor returns empty list") {
174+
val line = "let main()"
175+
assertExtractsNoLibraries(line, FSharpExtractor())
176+
}
177+
167178
it("cpp extractor returns empty list") {
168179
val line = "std::cerr << status.ToString() << std::endl;"
169180
assertExtractsNoLibraries(line, CppExtractor())

0 commit comments

Comments
 (0)