Skip to content

Commit 0db0c16

Browse files
authored
Merge pull request #50 from shiraji/49_fix_AddPMDependencies
Fix addPMDependencies
2 parents 6794276 + dbd193a commit 0db0c16

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.0.6'
2+
ext.kotlin_version = '1.1.2'
33
repositories {
44
mavenCentral()
55
}

src/main/kotlin/com/github/shiraji/permissionsdispatcherplugin/actions/AddPMDependencies.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.intellij.psi.PsiFile
1616
import com.intellij.psi.PsiManager
1717
import com.intellij.psi.search.FilenameIndex
1818
import com.intellij.psi.search.GlobalSearchScope
19+
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
1920
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
2021
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
2122
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory
@@ -33,13 +34,18 @@ class AddPMDependencies : CodeInsightAction() {
3334
e ?: return
3435
super.update(e)
3536
val project = e.getData(CommonDataKeys.PROJECT) ?: return
36-
FilenameIndex.getAllFilesByExt(project, "gradle", GlobalSearchScope.projectScope(project)).forEach {
37-
val groovyFile = PsiManager.getInstance(project).findFile(it) as? GroovyFile ?: return@forEach
38-
val dependenciesBlock = groovyFile.findDescendantOfType<GrMethodCallExpression> { it.invokedExpression.text == "dependencies" } ?: return@forEach
39-
if (dependenciesBlock.findDescendantOfType<GrCommandArgumentList> { it.text.contains("com.github.hotchemi:permissionsdispatcher:") } != null) {
40-
e.presentation.isEnabledAndVisible = false
41-
return
42-
}
37+
val dependenciesBlocks = FilenameIndex.getAllFilesByExt(project, "gradle", GlobalSearchScope.projectScope(project)).map {
38+
PsiManager.getInstance(project).findFile(it) as? GroovyFile
39+
}.filterNotNull().filter {
40+
it.findDescendantOfType<GrCommandArgumentList> { it.text.contains("com.android.application") } != null
41+
}.flatMap {
42+
it.collectDescendantsOfType<GrMethodCallExpression> { it.invokedExpression.text == "dependencies" }
43+
}
44+
45+
if (dependenciesBlocks.firstOrNull {
46+
it.findDescendantOfType<GrCommandArgumentList> { it.text.contains("com.github.hotchemi:permissionsdispatcher:") } != null
47+
} != null) {
48+
e.presentation.isEnabledAndVisible = false
4349
}
4450
}
4551

@@ -51,11 +57,13 @@ class AddPMDependencies : CodeInsightAction() {
5157
var hasAndroidApt = false
5258
var useKapt = false
5359
var androidGradleVersion: AndroidGradleVersion? = null
60+
var targetFile: GroovyFile? = null
5461

5562
FilenameIndex.getAllFilesByExt(project, "gradle", GlobalSearchScope.projectScope(project)).forEach {
5663
val groovyFile = PsiManager.getInstance(project).findFile(it) as? GroovyFile ?: return@forEach
5764
if (groovyFile.findDescendantOfType<GrApplicationStatement> { it.text.contains("\'android-apt\'") } != null) hasAndroidApt = true
5865
if (groovyFile.findDescendantOfType<GrApplicationStatement> { it.text.contains("\'kotlin-android\'") } != null) useKapt = true
66+
if (groovyFile.findDescendantOfType<GrCommandArgumentList> { it.text.contains("com.android.application") } != null) targetFile = groovyFile
5967

6068
val androidGradleBuildLine = groovyFile.findDescendantOfType<GrCommandArgumentList> {
6169
it.text.contains("com.android.tools.build:gradle:")
@@ -69,8 +77,6 @@ class AddPMDependencies : CodeInsightAction() {
6977
}
7078
}
7179

72-
if (file !is GroovyFile) return
73-
7480
val version = androidGradleVersion
7581
when {
7682
version == null ->
@@ -86,7 +92,7 @@ class AddPMDependencies : CodeInsightAction() {
8692
"No annotation processing settings found. Use 'android gradle plugin version >= 2.2' or 'android-apt'",
8793
NotificationType.WARNING))
8894
else -> {
89-
val dependenciesBlock = file.findDescendantOfType<GrMethodCallExpression> { it.invokedExpression.text == "dependencies" } ?: return
95+
val dependenciesBlock = targetFile?.findDescendantOfType<GrMethodCallExpression> { it.invokedExpression.text == "dependencies" } ?: return
9096
val factory = GroovyPsiElementFactory.getInstance(project)
9197
val aptRef = when {
9298
useKapt -> "kapt"
@@ -95,9 +101,10 @@ class AddPMDependencies : CodeInsightAction() {
95101
}
96102
val compileExpression = factory.createExpressionFromText("compile 'com.github.hotchemi:permissionsdispatcher:${PdVersion.latestVersion}'")
97103
val annotationProcessorExpression = factory.createExpressionFromText("$aptRef 'com.github.hotchemi:permissionsdispatcher-processor:${PdVersion.latestVersion}'")
98-
dependenciesBlock.closureArguments[0]?.let {
99-
it.addBefore(compileExpression, it.rBrace)
100-
it.addBefore(annotationProcessorExpression, it.rBrace)
104+
dependenciesBlock.closureArguments[0]?.run {
105+
val applicationStatement = addBefore(compileExpression, rBrace) as? GrApplicationStatement
106+
addBefore(annotationProcessorExpression, rBrace)
107+
applicationStatement?.navigate(true)
101108
}
102109
}
103110
}

0 commit comments

Comments
 (0)