From a292f8aaed0408e852114fbb9f6c4996568d06e3 Mon Sep 17 00:00:00 2001 From: Adam Malek Date: Tue, 30 Apr 2024 15:45:32 +0200 Subject: [PATCH] ML in SE: Replace deprecated getPackageNameByDirectory (IJPL-61980) GitOrigin-RevId: 1c38546c38babac8e2402abca1de5353775a5ede --- .idea/modules.xml | 1 + ...tellij.searchEverywhereMl.ranking.core.iml | 1 + ...chEverywhereClassOrFileFeaturesProvider.kt | 72 +---- ...erywhereClassOrFileFeaturesProviderTest.kt | 248 ------------------ ...tellij.searchEverywhereMl.ranking.java.iml | 16 ++ ...tellij.searchEverywhereMl.ranking.java.xml | 11 + ...EverywhereJavaPsiElementFeatureProvider.kt | 130 +++++++++ .../resources/META-INF/plugin.xml | 1 + 8 files changed, 162 insertions(+), 318 deletions(-) create mode 100644 plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml create mode 100644 plugins/search-everywhere-ml/ranking/java/resources/intellij.searchEverywhereMl.ranking.java.xml create mode 100644 plugins/search-everywhere-ml/ranking/java/src/com/intellij/searchEverywhereMl/ranking/java/features/SearchEverywhereJavaPsiElementFeatureProvider.kt diff --git a/.idea/modules.xml b/.idea/modules.xml index 9d7608b36f23..29ff528dbdb6 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -1186,6 +1186,7 @@ + diff --git a/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml b/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml index 1ec4f2b87125..551741cbbd96 100644 --- a/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml +++ b/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml @@ -148,6 +148,7 @@ + diff --git a/plugins/search-everywhere-ml/ranking/core/src/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProvider.kt b/plugins/search-everywhere-ml/ranking/core/src/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProvider.kt index 8fe84e4c0683..d181e2deaca5 100644 --- a/plugins/search-everywhere-ml/ranking/core/src/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProvider.kt +++ b/plugins/search-everywhere-ml/ranking/core/src/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProvider.kt @@ -42,8 +42,6 @@ import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereCla import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.IS_OPENED_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.IS_SAME_FILETYPE_AS_OPENED_FILE_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.IS_SAME_MODULE_DATA_KEY -import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.PACKAGE_DISTANCE_DATA_KEY -import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.PACKAGE_DISTANCE_NORMALIZED_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.PREDICTION_SCORE_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.RECENT_INDEX_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.TIME_SINCE_LAST_FILETYPE_USAGE_DATA_KEY @@ -67,8 +65,7 @@ class SearchEverywhereClassOrFileFeaturesProvider : SearchEverywhereElementFeatu internal val IS_ACCESSIBLE_FROM_MODULE = EventFields.Boolean("isAccessibleFromModule") internal val IS_SAME_MODULE_DATA_KEY = EventFields.Boolean("isSameModule") - internal val PACKAGE_DISTANCE_DATA_KEY = EventFields.Int("packageDistance") - internal val PACKAGE_DISTANCE_NORMALIZED_DATA_KEY = EventFields.Double("packageDistanceNorm") + internal val DIRECTORY_DEPTH_DATA_KEY = EventFields.Int("directoryDepth") internal val IS_SAME_FILETYPE_AS_OPENED_FILE_DATA_KEY = EventFields.Boolean("isSameFileTypeAsOpenedFile") @@ -103,8 +100,7 @@ class SearchEverywhereClassOrFileFeaturesProvider : SearchEverywhereElementFeatu override fun getFeaturesDeclarations(): List> { return arrayListOf( IS_ACCESSIBLE_FROM_MODULE, - IS_SAME_MODULE_DATA_KEY, PACKAGE_DISTANCE_DATA_KEY, - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY, DIRECTORY_DEPTH_DATA_KEY, + IS_SAME_MODULE_DATA_KEY, DIRECTORY_DEPTH_DATA_KEY, IS_SAME_FILETYPE_AS_OPENED_FILE_DATA_KEY, IS_IN_SOURCE_DATA_KEY, IS_IN_TEST_SOURCES_DATA_KEY, IS_IN_LIBRARY_DATA_KEY, IS_EXCLUDED_DATA_KEY, FILETYPE_USAGE_RATIO_DATA_KEY, @@ -196,11 +192,6 @@ class SearchEverywhereClassOrFileFeaturesProvider : SearchEverywhereElementFeatu data.addAll(getModificationTimeStats(file, currentTime)) data.add(IS_OPENED_DATA_KEY.with(isOpened(file, project))) - calculatePackageDistance(file, project, cache.currentlyOpenedFile)?.let { (packageDistance, packageDistanceNorm) -> - data.add(PACKAGE_DISTANCE_DATA_KEY.with(packageDistance)) - data.add(PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(packageDistanceNorm)) - } - calculateRootDistance(file, project)?.let { data.add(DIRECTORY_DEPTH_DATA_KEY.with(it)) } @@ -251,65 +242,6 @@ class SearchEverywhereClassOrFileFeaturesProvider : SearchEverywhereElementFeatu return openedFileModule == itemModule } - /** - * Calculates the package distance of the found [file] relative to the [openedFile]. - * - * The distance can be considered the number of steps/changes to reach the other package, - * for instance the distance to a parent or a child of a package is equal to 1, - * and the distance from package a.b.c.d to package a.b.x.y is equal to 4. - * - * @return Pair of distance and normalized distance, or null if it could not be calculated. - */ - private fun calculatePackageDistance(file: VirtualFile, project: Project, openedFile: VirtualFile?): Pair? { - if (openedFile == null) { - return null - } - - val (openedFilePackage, foundFilePackage) = ReadAction.compute, Nothing> { - val fileIndex = ProjectRootManager.getInstance(project).fileIndex - - // Parents of some files may still not be directories - val openedFileDirectory = openedFile.parent?.takeIf { it.isDirectory } - val foundFileDirectory = if (file.isDirectory) file else file.parent?.takeIf { it.isDirectory } - - val openedFilePackageName = openedFileDirectory?.let { fileIndex.getPackageNameByDirectory(it) } - val foundFilePackageName = foundFileDirectory?.let { fileIndex.getPackageNameByDirectory(it) } - - Pair(openedFilePackageName, foundFilePackageName) - }.run { - fun splitPackage(s: String?) = if (s == null) { - null - } - else if (s.isBlank()) { - // In case the file is under a source root (src/testSrc/resource) and the package prefix is blank - emptyList() - } - else { - s.split('.') - } - - Pair(splitPackage(first), splitPackage(second)) - } - - if (openedFilePackage == null || foundFilePackage == null) { - return null - } - - val maxDistance = openedFilePackage.size + foundFilePackage.size - var common = 0 - for ((index, value) in openedFilePackage.withIndex()) { - if (foundFilePackage.size == index || foundFilePackage[index] != value) { - // Stop counting if the found file package is a parent or it no longer matches the opened file package - break - } - - common++ - } - - val distance = maxDistance - 2 * common - val normalizedDistance = roundDouble(if (maxDistance != 0) (distance.toDouble() / maxDistance) else 0.0) - return Pair(distance, normalizedDistance) - } private fun calculateRootDistance(file: VirtualFile, project: Project): Int? { val contentRoot = project.guessProjectDir() ?: return null diff --git a/plugins/search-everywhere-ml/ranking/core/test/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProviderTest.kt b/plugins/search-everywhere-ml/ranking/core/test/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProviderTest.kt index c472bb355911..e022e56a083a 100644 --- a/plugins/search-everywhere-ml/ranking/core/test/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProviderTest.kt +++ b/plugins/search-everywhere-ml/ranking/core/test/com/intellij/searchEverywhereMl/ranking/core/features/SearchEverywhereClassOrFileFeaturesProviderTest.kt @@ -19,8 +19,6 @@ import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereCla import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.FILETYPE_USED_IN_LAST_MONTH_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.IS_OPENED_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.IS_SAME_MODULE_DATA_KEY -import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.PACKAGE_DISTANCE_DATA_KEY -import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.PACKAGE_DISTANCE_NORMALIZED_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.RECENT_INDEX_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.TIME_SINCE_LAST_FILETYPE_USAGE_DATA_KEY import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereClassOrFileFeaturesProvider.Fields.TIME_SINCE_LAST_MODIFICATION_DATA_KEY @@ -228,252 +226,6 @@ internal class SearchEverywhereClassOrFileFeaturesProviderTest } - fun `test package distance is reported for directories`() { - var openedFile: VirtualFile? = null - var foundDirectory: VirtualFile? = null - - module { - source { - createPackage("a.b.c") { - file("fileA.txt") { openedFile = it } - directory("foundDirectory") { foundDirectory = it } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(1), // The found directory is considered a subpackage, hence the distance should be 1 - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(1 / (3 + 4).toDouble())), - ) - - val psiFile = foundDirectory!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance is 0 when same package`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b.c") { - file("fileA.txt") { openedFile = it } - file("fileB.txt") { foundFile = it } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(0), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(0.0), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance is 1 when in child package`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b.c.d") { - file("fileA.txt") { openedFile = it } - createPackage("e") { - file("fileB.txt") { foundFile = it } - } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(1), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(1 / (4 + 5).toDouble())), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance is 1 when in parent package`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b.c") { - file("fileB.txt") { foundFile = it } - createPackage("d") { - file("fileA.txt") { openedFile = it } - } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(1), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(1 / (4 + 3).toDouble())), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance on a parent of different group`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b") { - createPackage("c.d") { - file("fileA.txt") { openedFile = it } - } - createPackage("x") { - file("fileB.txt") { foundFile = it } - } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(3), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(3 / (4 + 3).toDouble())), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance on a child of different group`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b") { - createPackage("c.d") { - file("fileA.txt") { openedFile = it } - } - createPackage("x.y") { - file("fileB.txt") { foundFile = it } - } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(4), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(4 / (4 + 4).toDouble())) - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance when root is different`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b.c.d") { - file("fileA.txt") { openedFile = it } - } - createPackage("x.y") { - file("fileB.txt") { foundFile = it } - } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(6), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(6 / (4 + 2).toDouble())), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance is 0 when files are not in packages`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - file("fileA.txt") { openedFile = it } - file("fileB.txt") { foundFile = it } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(0), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(0.0), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - - fun `test package distance when one file is not in a package`() { - var openedFile: VirtualFile? = null - var foundFile: VirtualFile? = null - - module { - source { - createPackage("a.b.c.d") { - file("fileA.txt") { openedFile = it } - } - file("fileB.txt") { foundFile = it } - } - } - - FileEditorManager.getInstance(project).openFile(openedFile!!, true) - - val expected = listOf( - PACKAGE_DISTANCE_DATA_KEY.with(4), - PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(roundDouble(4 / (4 + 0).toDouble())), - ) - - val psiFile = foundFile!!.toPsi() - checkThatFeatures() - .ofElement(psiFile) - .isEqualTo(expected) - } - fun `test root distance for file in package`() { var foundFile: VirtualFile? = null diff --git a/plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml b/plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml new file mode 100644 index 000000000000..3980f72ea1c1 --- /dev/null +++ b/plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/search-everywhere-ml/ranking/java/resources/intellij.searchEverywhereMl.ranking.java.xml b/plugins/search-everywhere-ml/ranking/java/resources/intellij.searchEverywhereMl.ranking.java.xml new file mode 100644 index 000000000000..6ec3206aca8b --- /dev/null +++ b/plugins/search-everywhere-ml/ranking/java/resources/intellij.searchEverywhereMl.ranking.java.xml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/plugins/search-everywhere-ml/ranking/java/src/com/intellij/searchEverywhereMl/ranking/java/features/SearchEverywhereJavaPsiElementFeatureProvider.kt b/plugins/search-everywhere-ml/ranking/java/src/com/intellij/searchEverywhereMl/ranking/java/features/SearchEverywhereJavaPsiElementFeatureProvider.kt new file mode 100644 index 000000000000..9d3de80e43f7 --- /dev/null +++ b/plugins/search-everywhere-ml/ranking/java/src/com/intellij/searchEverywhereMl/ranking/java/features/SearchEverywhereJavaPsiElementFeatureProvider.kt @@ -0,0 +1,130 @@ +package com.intellij.searchEverywhereMl.ranking.java.features + +import com.intellij.ide.actions.searcheverywhere.ClassSearchEverywhereContributor +import com.intellij.ide.actions.searcheverywhere.FileSearchEverywhereContributor +import com.intellij.ide.actions.searcheverywhere.RecentFilesSEContributor +import com.intellij.internal.statistic.eventLog.events.EventField +import com.intellij.internal.statistic.eventLog.events.EventFields +import com.intellij.internal.statistic.eventLog.events.EventPair +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.PackageIndex +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFileSystemItem +import com.intellij.psi.PsiInvalidElementAccessException +import com.intellij.searchEverywhereMl.ranking.core.features.FeaturesProviderCache +import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywhereElementFeaturesProvider +import com.intellij.searchEverywhereMl.ranking.core.features.SearchEverywherePsiElementFeaturesProviderUtils +import com.intellij.searchEverywhereMl.ranking.java.features.SearchEverywhereJavaPsiElementFeatureProvider.Fields.PACKAGE_DISTANCE_DATA_KEY +import com.intellij.searchEverywhereMl.ranking.java.features.SearchEverywhereJavaPsiElementFeatureProvider.Fields.PACKAGE_DISTANCE_NORMALIZED_DATA_KEY +import kotlin.math.round + +internal class SearchEverywhereJavaPsiElementFeatureProvider : SearchEverywhereElementFeaturesProvider( + ClassSearchEverywhereContributor::class.java, + FileSearchEverywhereContributor::class.java, + RecentFilesSEContributor::class.java, +) { + override fun getFeaturesDeclarations(): List> = listOf( + PACKAGE_DISTANCE_DATA_KEY, PACKAGE_DISTANCE_NORMALIZED_DATA_KEY + ) + + override fun getElementFeatures(element: Any, currentTime: Long, searchQuery: String, elementPriority: Int, cache: FeaturesProviderCache?): List> { + val psiElement = SearchEverywherePsiElementFeaturesProviderUtils.getPsiElement(element) ?: return emptyList() + + val file = getContainingFile(psiElement) ?: return emptyList() + + val openedFile = cache?.currentlyOpenedFile + val (packageDistance, normalizedPackageDistance) = calculatePackageDistance(file, psiElement.project, openedFile) ?: return emptyList() + + return listOf( + PACKAGE_DISTANCE_DATA_KEY.with(packageDistance), + PACKAGE_DISTANCE_NORMALIZED_DATA_KEY.with(normalizedPackageDistance), + ) + } + + /** + * Calculates the package distance of the found [file] relative to the [openedFile]. + * + * The distance can be considered the number of steps/changes to reach the other package, + * for instance the distance to a parent or a child of a package is equal to 1, + * and the distance from package a.b.c.d to package a.b.x.y is equal to 4. + * + * @return Pair of distance and normalized distance, or null if it could not be calculated. + */ + private fun calculatePackageDistance(file: VirtualFile, project: Project, openedFile: VirtualFile?): Pair? { + if (openedFile == null) { + return null + } + + val (openedFilePackage, foundFilePackage) = ReadAction.compute, Nothing> { + val packageIndex = PackageIndex.getInstance(project) + + // Parents of some files may still not be directories + val openedFileDirectory = openedFile.parent?.takeIf { it.isDirectory } + val foundFileDirectory = if (file.isDirectory) file else file.parent?.takeIf { it.isDirectory } + + val openedFilePackageName = openedFileDirectory?.let { packageIndex.getPackageNameByDirectory(it) } + val foundFilePackageName = foundFileDirectory?.let { packageIndex.getPackageNameByDirectory(it) } + + Pair(openedFilePackageName, foundFilePackageName) + }.run { + fun splitPackage(s: String?) = if (s == null) { + null + } + else if (s.isBlank()) { + // In case the file is under a source root (src/testSrc/resource) and the package prefix is blank + emptyList() + } + else { + s.split('.') + } + + Pair(splitPackage(first), splitPackage(second)) + } + + if (openedFilePackage == null || foundFilePackage == null) { + return null + } + + val maxDistance = openedFilePackage.size + foundFilePackage.size + var common = 0 + for ((index, value) in openedFilePackage.withIndex()) { + if (foundFilePackage.size == index || foundFilePackage[index] != value) { + // Stop counting if the found file package is a parent or it no longer matches the opened file package + break + } + + common++ + } + + val distance = maxDistance - 2 * common + val normalizedDistance = roundDouble(if (maxDistance != 0) (distance.toDouble() / maxDistance) else 0.0) + return Pair(distance, normalizedDistance) + } + + private fun getContainingFile(item: PsiElement): VirtualFile? { + if (item is PsiFileSystemItem) { + return item.virtualFile + } + + return ReadAction.compute { + try { + item.containingFile?.virtualFile + } + catch (ex: PsiInvalidElementAccessException) { + null + } + } + } + + private fun roundDouble(value: Double): Double { + if (!value.isFinite()) return -1.0 + return round(value * 100000) / 100000 + } + + internal object Fields { + val PACKAGE_DISTANCE_DATA_KEY = EventFields.Int("packageDistance") + val PACKAGE_DISTANCE_NORMALIZED_DATA_KEY = EventFields.Double("packageDistanceNorm") + } +} diff --git a/plugins/search-everywhere-ml/resources/META-INF/plugin.xml b/plugins/search-everywhere-ml/resources/META-INF/plugin.xml index 3b3df21f9322..c449ed1c69aa 100644 --- a/plugins/search-everywhere-ml/resources/META-INF/plugin.xml +++ b/plugins/search-everywhere-ml/resources/META-INF/plugin.xml @@ -17,6 +17,7 @@ +