Skip to content

Commit bef9653

Browse files
committed
support k2 mode
1 parent 85105d3 commit bef9653

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
kotlin.stdlib.default.dependency=false
22
pluginSinceBuild=232
3-
version=1.3-SNAPSHOT
3+
version=1.31-SNAPSHOT
44
kotlin.code.style=official
55
kotlin.experimental.tryK2=true
66
kapt.use.k2=true

src/main/kotlin/org/tabooproject/development/Utils.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ import com.intellij.psi.search.GlobalSearchScope
77
import com.intellij.psi.util.PsiTreeUtil
88
import okhttp3.OkHttpClient
99
import okhttp3.Request
10-
import org.jetbrains.kotlin.descriptors.ClassDescriptor
11-
import org.jetbrains.kotlin.idea.caches.resolve.analyze
10+
import org.jetbrains.kotlin.name.FqName
1211
import org.jetbrains.kotlin.psi.*
13-
import org.jetbrains.kotlin.resolve.BindingContext
1412
import org.jetbrains.kotlin.resolve.ImportPath
15-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
16-
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
1713
import java.io.IOException
1814
import java.net.InetSocketAddress
1915
import java.net.Proxy
@@ -121,12 +117,15 @@ private fun isPackageInProject(file: PsiFile, packageName: String): Boolean {
121117
return orderEnumerator.urls.any { it.contains(packageName.replace('.', '/')) }
122118
}
123119

120+
124121
fun KtFile.checkAndImportPackage(path: String) {
122+
val fqName = FqName(path)
123+
125124
// 检查和引入info包
126125
val import =
127126
PsiTreeUtil.findChildrenOfType(this, KtImportDirective::class.java)
128127
val hasImport =
129-
import.any { it.importPath?.pathStr == path }
128+
import.any { it.importedFqName == fqName }
130129
if (!hasImport) {
131130
val factory = KtPsiFactory(project)
132131
val importDirective =
@@ -137,11 +136,7 @@ fun KtFile.checkAndImportPackage(path: String) {
137136

138137
val KtDotQualifiedExpression.fqName: String?
139138
get() {
140-
val receiverExpression = receiverExpression
141-
val bindingContext = receiverExpression.analyze(BodyResolveMode.PARTIAL)
142-
val type = bindingContext.get(BindingContext.EXPRESSION_TYPE_INFO, receiverExpression)?.type
143-
val classDescriptor = type?.constructor?.declarationDescriptor as? ClassDescriptor
144-
return classDescriptor?.fqNameSafe?.asString() ?: return null
139+
return text
145140
}
146141

147142
fun KtDotQualifiedExpression.getPsiClass(): PsiClass? {

src/main/kotlin/org/tabooproject/development/suppressor/ExpansionUnusedSuppressor.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import com.intellij.codeInspection.InspectionSuppressor
44
import com.intellij.codeInspection.SuppressQuickFix
55
import com.intellij.psi.PsiElement
66
import com.intellij.psi.util.PsiTreeUtil
7-
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
7+
import org.jetbrains.kotlin.analysis.api.analyze
88
import org.jetbrains.kotlin.psi.KtClassOrObject
99
import org.jetbrains.kotlin.psi.KtTypeReference
10-
import org.jetbrains.kotlin.resolve.BindingContext
11-
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
12-
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
1310

1411
private const val INSPECTION = "unused"
1512

@@ -34,34 +31,38 @@ class ExpansionUnusedSuppressor: InspectionSuppressor {
3431

3532
private fun checkIfClassImplementsOrExtends(element: PsiElement, className: String): Boolean {
3633
val ktClass = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java) ?: return false
37-
val context = ktClass.getResolutionFacade().analyze(ktClass, BodyResolveMode.FULL)
38-
return ktClass.implementsInterface(className, context) || ktClass.isSubclassOf(className, context)
34+
return analyze(ktClass) {
35+
ktClass.implementsInterface(className) || ktClass.isSubclassOf(className)
36+
}
3937
}
4038

41-
private fun KtClassOrObject.isSubclassOf(className: String, context: BindingContext): Boolean {
39+
private fun KtClassOrObject.isSubclassOf(className: String): Boolean {
4240
if (fqName?.asString() == className) return true
4341

4442
val superTypes = this.superTypeListEntries
4543
return superTypes.any { typeEntry ->
4644
val typeReference = typeEntry.typeReference
47-
val typeFqName = typeReference?.getFqName(context)
45+
val typeFqName = typeReference?.getFqName()
4846
typeFqName == className
4947
}
5048
}
5149

52-
private fun KtClassOrObject.implementsInterface(interfaceName: String, context: BindingContext): Boolean {
53-
if (isSubclassOf(interfaceName, context)) return true
50+
private fun KtClassOrObject.implementsInterface(interfaceName: String): Boolean {
51+
if (isSubclassOf(interfaceName)) return true
5452

5553
val superTypes = this.superTypeListEntries
5654
return superTypes.any { typeEntry ->
5755
val typeReference = typeEntry.typeReference
58-
val typeFqName = typeReference?.getFqName(context)
56+
val typeFqName = typeReference?.getFqName()
5957
typeFqName == interfaceName
6058
}
6159
}
6260

63-
private fun KtTypeReference.getFqName(context: BindingContext): String? {
64-
val type = context[BindingContext.TYPE, this]
65-
return type?.constructor?.declarationDescriptor?.fqNameSafe?.asString()
61+
private fun KtTypeReference.getFqName(): String? {
62+
return analyze(this) {
63+
val type = type
64+
val symbol = type.expandedSymbol
65+
symbol?.classId?.asSingleFqName()?.asString()
66+
}
6667
}
6768
}

0 commit comments

Comments
 (0)