Skip to content

Commit

Permalink
testing completed
Browse files Browse the repository at this point in the history
  • Loading branch information
yonidavidson committed Nov 8, 2023
1 parent 6493816 commit b466e56
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiManager
import com.intellij.util.indexing.FileBasedIndex
import com.tabnineCommon.binary.requests.fileMetadata.FileMetadataRequest
import com.tabnineCommon.chat.commandHandlers.ChatMessageHandler
Expand Down Expand Up @@ -66,26 +65,104 @@ public fun getPredominantWorkspaceLanguage(
): String? {
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return null
val fileIndex = FileBasedIndex.getInstance()
val maxFilesToConsider = 50

val languageCount = mutableMapOf<String, Int>()
val extensionCount = mutableMapOf<String, Int>()
var filesProcessed = 0

// Using the FileBasedIndex to iterate through files
fileIndex.iterateIndexableFiles(
{ virtualFile ->
if (filesProcessed >= maxFilesToConsider) {
// If we've hit the limit, stop the iteration by returning false
return@iterateIndexableFiles false
}

if (!virtualFile.isDirectory && virtualFile.isValid && includeFilePredicate(virtualFile.path)) {
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)
val language = psiFile?.language?.id ?: return@iterateIndexableFiles true
val fileExtension = virtualFile.extension?.toLowerCase() // Get the file extension

languageCount[language] = languageCount.getOrDefault(language, 0) + 1
if (fileExtension != null) {
extensionCount[fileExtension] = extensionCount.getOrDefault(fileExtension, 0) + 1
filesProcessed++ // Increment the counter
}
}
true
true // Continue iteration
},
project, null
)

// Sorting languages by frequency
val sortedLanguages = languageCount.toList().sortedByDescending { (_, count) -> count }
val sortedExtensions = extensionCount.toList().sortedByDescending { (_, count) -> count }

// Returning the most frequent language or null if no files are found
return sortedLanguages.firstOrNull()?.first
return sortedExtensions.firstOrNull()?.first?.let { getLanguageFromExtension(it) }
}

val extensionToLanguageMap = mapOf(
"abap" to "abap",
"bat" to "bat",
"bibtex" to "bib",
"c" to "c",
"clojure" to "clj",
"coffeescript" to "coffee",
"cpp" to "cpp",
"csharp" to "cs",
"css" to "css",
"cuda-cpp" to "cu",
"dart" to "dart",
"diff" to "diff",
"dockerfile" to "dockerfile",
"fsharp" to "fs",
"go" to "go",
"groovy" to "groovy",
"haml" to "haml",
"handlebars" to "handlebars",
"hlsl" to "hlsl",
"html" to "html",
"ini" to "ini",
"jade" to "jade",
"java" to "java",
"javascript" to "js",
"javascriptreact" to "jsx",
"json" to "json",
"julia" to "jl",
"latex" to "tex",
"less" to "less",
"lua" to "lua",
"makefile" to "make",
"markdown" to "md",
"objective-c" to "m",
"objective-cpp" to "mm",
"perl" to "pl",
"perl6" to "6pl",
"php" to "php",
"plaintext" to "txt",
"powershell" to "ps1",
"pug" to "pug",
"python" to "py",
"r" to "r",
"razor" to "cshtml",
"ruby" to "rb",
"rust" to "rs",
"sass" to "sass",
"scss" to "scss",
"shaderlab" to "shader",
"shellscript" to "sh",
"slim" to "slim",
"sql" to "sql",
"stylus" to "styl",
"swift" to "swift",
"tex" to "tex",
"typescript" to "ts",
"typescriptreact" to "tsx",
"vb" to "vb",
"vue" to "vue",
"xml" to "xml",
"xsl" to "xsl",
"yaml" to "yaml"
)

val languageFromExtension = extensionToLanguageMap.entries.associate { (k, v) -> v to k }
fun getLanguageFromExtension(extension: String): String? {
return languageFromExtension[extension]
}
30 changes: 22 additions & 8 deletions Tabnine/src/test/java/com/tabnine/plugin/WorkspaceLanguageTests.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@

import com.intellij.psi.PsiManager
import com.tabnine.MockedBinaryCompletionTestCase
import com.tabnineCommon.capabilities.SuggestionsMode
import com.tabnineCommon.chat.commandHandlers.context.getPredominantWorkspaceLanguage
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
import java.sql.Date
import java.text.SimpleDateFormat

class PredominantWorkspaceLanguageTest : MockedBinaryCompletionTestCase() {
@Before
fun init() {
Mockito.`when`(suggestionsModeServiceMock.getSuggestionMode()).thenReturn(SuggestionsMode.HYBRID)
}
@Test
fun `test getPredominantWorkspaceLanguage with various languages`() {
val formatter = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
val date = java.util.Date()
val directoryName = formatter.format(date)
createFileInProject("$directoryName/a.java")
createFileInProject("$directoryName/b.java")
createFileInProject("$directoryName/a.go")
createFileInProject("$directoryName/b.go")
createFileInProject("$directoryName/c.go")
createFileInProject("$directoryName/d.java")
createFileInProject("$directoryName/e.java")
createFileInProject("$directoryName/f.kotlin")
createFileInProject("$directoryName/g.kt")

val language = getPredominantWorkspaceLanguage { it.contains(directoryName) }
assertEquals("Kotlin", language)
assertEquals("go", language)
}

@Test
fun `test getPredominantWorkspaceLanguage with unknown language`() {
val formatter = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
val date = java.util.Date()
val directoryName = formatter.format(date)
createFileInProject("$directoryName/a.bla")
createFileInProject("$directoryName/b.bla")
createFileInProject("$directoryName/c.bla")

val language = getPredominantWorkspaceLanguage { it.contains(directoryName) }
assertNull(language)
}

@Test
Expand All @@ -33,7 +47,7 @@ class PredominantWorkspaceLanguageTest : MockedBinaryCompletionTestCase() {
}

private fun createFileInProject(filePath: String) {
val file = myFixture.addFileToProject(filePath, "")
val file = myFixture.addFileToProject(filePath, "println(\"Hello, world!\")\n")
val f = PsiManager.getInstance(project).findFile(file.virtualFile)
assertNotNull(f)
}
Expand Down

0 comments on commit b466e56

Please sign in to comment.