mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ML in SE: Replace deprecated getPackageNameByDirectory (IJPL-61980)
GitOrigin-RevId: 1c38546c38babac8e2402abca1de5353775a5ede
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d37eb95893
commit
a292f8aaed
Generated
+1
@@ -1186,6 +1186,7 @@
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/intellij.searchEverywhereMl.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/intellij.searchEverywhereMl.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/core/intellij.searchEverywhereMl.ranking.core.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/ext/intellij.searchEverywhereMl.ranking.ext.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/ext/intellij.searchEverywhereMl.ranking.ext.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/java/intellij.searchEverywhereMl.ranking.java.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/vcs/intellij.searchEverywhereMl.ranking.vcs.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/vcs/intellij.searchEverywhereMl.ranking.vcs.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/yaml/intellij.searchEverywhereMl.ranking.yaml.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/ranking/yaml/intellij.searchEverywhereMl.ranking.yaml.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/search-everywhere-ml/semantics/intellij.searchEverywhereMl.semantics.iml" filepath="$PROJECT_DIR$/plugins/search-everywhere-ml/semantics/intellij.searchEverywhereMl.semantics.iml" />
|
||||
|
||||
@@ -148,6 +148,7 @@
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.ranking.ext" />
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.ranking.java" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.ranking.vcs" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.ranking.yaml" scope="RUNTIME" />
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.typos" scope="RUNTIME" />
|
||||
|
||||
+2
-70
@@ -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<EventField<*>> {
|
||||
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<Int, Double>? {
|
||||
if (openedFile == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
val (openedFilePackage, foundFilePackage) = ReadAction.compute<Pair<String?, String?>, 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
|
||||
|
||||
-248
@@ -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
|
||||
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="kotlin-stdlib" level="project" />
|
||||
<orderEntry type="module" module-name="intellij.searchEverywhereMl.ranking.core" />
|
||||
<orderEntry type="module" module-name="intellij.platform.lang.impl" />
|
||||
<orderEntry type="module" module-name="intellij.java.psi" />
|
||||
</component>
|
||||
</module>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
<idea-plugin package="com.intellij.searchEverywhereMl.ranking.java">
|
||||
<dependencies>
|
||||
<module name="intellij.searchEverywhereMl.ranking.core"/>
|
||||
<plugin id="com.intellij.java"/>
|
||||
</dependencies>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij.searcheverywhere.ml">
|
||||
<searchEverywhereElementFeaturesProvider
|
||||
implementation="com.intellij.searchEverywhereMl.ranking.java.features.SearchEverywhereJavaPsiElementFeatureProvider"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
+130
@@ -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<EventField<*>> = listOf(
|
||||
PACKAGE_DISTANCE_DATA_KEY, PACKAGE_DISTANCE_NORMALIZED_DATA_KEY
|
||||
)
|
||||
|
||||
override fun getElementFeatures(element: Any, currentTime: Long, searchQuery: String, elementPriority: Int, cache: FeaturesProviderCache?): List<EventPair<*>> {
|
||||
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<Int, Double>? {
|
||||
if (openedFile == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
val (openedFilePackage, foundFilePackage) = ReadAction.compute<Pair<String?, String?>, 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<VirtualFile?, Nothing> {
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
<module name="intellij.searchEverywhereMl.typos"/>
|
||||
<module name="intellij.searchEverywhereMl.ranking.ext"/>
|
||||
<module name="intellij.searchEverywhereMl.ranking.core"/>
|
||||
<module name="intellij.searchEverywhereMl.ranking.java"/>
|
||||
<module name="intellij.searchEverywhereMl.ranking.yaml"/>
|
||||
<module name="intellij.searchEverywhereMl.ranking.vcs"/>
|
||||
<module name="intellij.searchEverywhereMl.semantics"/>
|
||||
|
||||
Reference in New Issue
Block a user