-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsImplIndex.kt
55 lines (48 loc) · 2.16 KB
/
RsImplIndex.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.lang.core.resolve.indexes
import com.intellij.openapi.project.Project
import com.intellij.psi.stubs.AbstractStubIndex
import com.intellij.psi.stubs.IndexSink
import com.intellij.psi.stubs.StubIndexKey
import com.intellij.util.io.KeyDescriptor
import org.rust.ide.search.RsWithMacrosProjectScope
import org.rust.lang.core.psi.RsImplItem
import org.rust.lang.core.psi.ext.typeParameters
import org.rust.lang.core.resolve.RsCachedImplItem
import org.rust.lang.core.stubs.RsFileStub
import org.rust.lang.core.stubs.RsImplItemStub
import org.rust.lang.core.types.TyFingerprint
import org.rust.openapiext.checkCommitIsNotInProgress
import org.rust.openapiext.getElements
class RsImplIndex : AbstractStubIndex<TyFingerprint, RsImplItem>() {
override fun getVersion(): Int = RsFileStub.Type.stubVersion
override fun getKey(): StubIndexKey<TyFingerprint, RsImplItem> = KEY
override fun getKeyDescriptor(): KeyDescriptor<TyFingerprint> = TyFingerprint.KeyDescriptor
companion object {
/**
* Note this method may return false positives
* @see TyFingerprint
*/
fun findPotentialImpls(
project: Project,
tyf: TyFingerprint,
): List<RsCachedImplItem> {
checkCommitIsNotInProgress(project)
val impls = getElements(KEY, tyf, project, RsWithMacrosProjectScope(project))
// Note that `getElements` is intentionally used with intermediate collection instead of
// `StubIndex.processElements` in order to simplify profiling
return impls.map { RsCachedImplItem.forImpl(it) }
}
fun index(stub: RsImplItemStub, sink: IndexSink) {
val impl = stub.psi
val typeRef = impl.typeReference ?: return
TyFingerprint.create(typeRef, impl.typeParameters.mapNotNull { it.name })
.forEach { sink.occurrence(KEY, it) }
}
private val KEY: StubIndexKey<TyFingerprint, RsImplItem> =
StubIndexKey.createIndexKey("org.rust.lang.core.stubs.index.RustImplIndex.TraitImpls")
}
}