[gradle] cleanup GradleRepositoriesContributor and add more tests

This commit is contained in:
Daniil Ovchinnikov
2019-03-21 18:18:51 +03:00
parent 1f7808ebcb
commit 7338d208b3
3 changed files with 75 additions and 44 deletions
@@ -1,12 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.gradle.service.resolve
import com.intellij.patterns.PsiJavaElementPattern
import com.intellij.patterns.PsiJavaPatterns.psiElement
import com.intellij.patterns.StandardPatterns.or
import com.intellij.psi.PsiElement
import com.intellij.psi.ResolveState
import com.intellij.psi.scope.PsiScopeProcessor
import groovy.lang.Closure
import org.jetbrains.plugins.gradle.service.resolve.GradleCommonClassNames.*
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock
@@ -29,33 +24,19 @@ import org.jetbrains.plugins.groovy.lang.resolve.delegatesTo.DelegatesToInfo
* @author Vladislav.Soroka
*/
class GradleRepositoriesContributor : GradleMethodContextContributor {
companion object {
val repositoriesClosure: GroovyClosurePattern = groovyClosure().inMethod(or(psiMethod(GRADLE_API_PROJECT, "repositories"),
psiMethod(GRADLE_API_SCRIPT_HANDLER, "repositories"),
psiMethod(GRADLE_API_PUBLISHING_EXTENSION, "repositories")))
val repositoryClosure: PsiJavaElementPattern.Capture<PsiElement> = psiElement().andOr(
groovyClosure().withAncestor(2, repositoriesClosure),
groovyClosure().inMethod(psiMethod(GRADLE_API_REPOSITORY_HANDLER, "maven")))
}
override fun process(methodCallInfo: List<String>,
processor: PsiScopeProcessor,
state: ResolveState,
place: PsiElement): Boolean {
if (methodCallInfo.isNotEmpty() && psiElement().inside(repositoryClosure).accepts(place)) {
if (!GradleResolverUtil.processDeclarations(
processor, state, place, GRADLE_API_ARTIFACTS_REPOSITORIES_MAVEN_ARTIFACT_REPOSITORY)) return false
}
return true
companion object {
val repositoriesClosure: GroovyClosurePattern = groovyClosure().inMethod(or(
psiMethod(GRADLE_API_PROJECT, "repositories"),
psiMethod(GRADLE_API_SCRIPT_HANDLER, "repositories"),
psiMethod(GRADLE_API_PUBLISHING_EXTENSION, "repositories")
))
}
override fun getDelegatesToInfo(closure: GrClosableBlock): DelegatesToInfo? {
if (repositoriesClosure.accepts(closure)) {
return DelegatesToInfo(TypesUtil.createType(GRADLE_API_REPOSITORY_HANDLER, closure), Closure.DELEGATE_FIRST)
}
// if (repositoryClosure.accepts(closure)) {
// return DelegatesToInfo(TypesUtil.createType(GRADLE_API_ARTIFACTS_REPOSITORIES_MAVEN_ARTIFACT_REPOSITORY, closure), DELEGATE_FIRST)
// }
return null
}
}
@@ -1,12 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.gradle.dsl
import com.intellij.psi.PsiMethod
import com.intellij.testFramework.RunAll
import groovy.transform.CompileStatic
import org.jetbrains.plugins.gradle.highlighting.GradleHighlightingBaseTest
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression
import org.jetbrains.plugins.groovy.util.ResolveTest
import org.junit.Test
import static org.jetbrains.plugins.gradle.service.resolve.GradleCommonClassNames.GRADLE_API_REPOSITORY_HANDLER
import static org.jetbrains.plugins.gradle.service.resolve.GradleCommonClassNames.*
@CompileStatic
class GradleRepositoriesTest extends GradleHighlightingBaseTest implements ResolveTest {
@@ -14,7 +17,21 @@ class GradleRepositoriesTest extends GradleHighlightingBaseTest implements Resol
@Test
void repositoriesTest() {
importProject("")
'repositories closure delegate'()
new RunAll().append {
'repositories closure delegate'()
} append {
'maven repository closure delegate'()
} append {
'ivy repository closure delegate'()
} append {
'flat repository closure delegate'()
} append {
'maven repository method setter'()
} append {
'ivy repository method setter'()
} append {
'flat repository method setter'()
} run()
}
@Override
@@ -27,4 +44,49 @@ class GradleRepositoriesTest extends GradleHighlightingBaseTest implements Resol
closureDelegateTest(GRADLE_API_REPOSITORY_HANDLER, 1)
}
}
void 'maven repository closure delegate'() {
doTest('repositories { maven { <caret> } }') {
closureDelegateTest(GRADLE_API_ARTIFACTS_REPOSITORIES_MAVEN_ARTIFACT_REPOSITORY, 1)
}
}
void 'ivy repository closure delegate'() {
doTest('repositories { ivy { <caret> } }') {
closureDelegateTest(GRADLE_API_ARTIFACTS_REPOSITORIES_IVY_ARTIFACT_REPOSITORY, 1)
}
}
void 'flat repository closure delegate'() {
doTest('repositories { flatDir { <caret> } }') {
closureDelegateTest(GRADLE_API_ARTIFACTS_REPOSITORIES_FLAT_DIRECTORY_ARTIFACT_REPOSITORY, 1)
}
}
void 'maven repository method setter'() {
doTest('repositories { maven { <caret>url(42) } }') {
def expression = elementUnderCaret(GrReferenceExpression)
def method = assertInstanceOf(expression.resolve(), PsiMethod)
assert method.name == 'setUrl'
assert method.containingClass.qualifiedName == GRADLE_API_ARTIFACTS_REPOSITORIES_MAVEN_ARTIFACT_REPOSITORY
}
}
void 'ivy repository method setter'() {
doTest('repositories { ivy { <caret>url("") } }') {
def expression = elementUnderCaret(GrReferenceExpression)
def method = assertInstanceOf(expression.resolve(), PsiMethod)
assert method.name == 'setUrl'
assert method.containingClass.qualifiedName == GRADLE_API_ARTIFACTS_REPOSITORIES_IVY_ARTIFACT_REPOSITORY
}
}
void 'flat repository method setter'() {
doTest('repositories { ivy { <caret>name("") } }') {
def expression = elementUnderCaret(GrReferenceExpression)
def method = assertInstanceOf(expression.resolve(), PsiMethod)
assert method.name == 'setName'
assert method.containingClass.qualifiedName == GRADLE_API_ARTIFACTS_REPOSITORIES_ARTIFACT_REPOSITORY
}
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.gradle.service.resolve;
import org.jetbrains.annotations.NonNls;
@@ -73,7 +59,6 @@ public final class GradleCommonClassNames {
@NonNls public static final String GRADLE_API_JUNIT_OPTIONS = "org.gradle.api.tasks.testing.junit.JUnitOptions";
@NonNls public static final String GRADLE_API_TEST_LOGGING_CONTAINER = "org.gradle.api.tasks.testing.logging.TestLoggingContainer";
@NonNls public static final String GRADLE_API_TASKS_UPLOAD = "org.gradle.api.tasks.Upload";
@NonNls public static final String GRADLE_API_ARTIFACTS_REPOSITORIES_FLAT_DIRECTORY_ARTIFACT_REPOSITORY = "org.gradle.api.artifacts.repositories.FlatDirectoryArtifactRepository";
@NonNls public static final String GRADLE_LANGUAGE_JVM_TASKS_PROCESS_RESOURCES = "org.gradle.language.jvm.tasks.ProcessResources";
@NonNls public static final String GRADLE_BUILDSETUP_TASKS_SETUP_BUILD = "org.gradle.buildsetup.tasks.SetupBuild";
@NonNls public static final String GRADLE_API_TASK_CONTAINER = "org.gradle.api.tasks.TaskContainer";
@@ -81,7 +66,10 @@ public final class GradleCommonClassNames {
@NonNls public static final String GRADLE_API_DOMAIN_OBJECT_COLLECTION = "org.gradle.api.DomainObjectCollection";
@NonNls public static final String GRADLE_API_NAMED_DOMAIN_OBJECT_COLLECTION = "org.gradle.api.NamedDomainObjectCollection";
@NonNls public static final String GRADLE_API_NAMED_DOMAIN_OBJECT_CONTAINER = "org.gradle.api.NamedDomainObjectContainer";
@NonNls public static final String GRADLE_API_ARTIFACTS_REPOSITORIES_ARTIFACT_REPOSITORY = "org.gradle.api.artifacts.repositories.ArtifactRepository";
@NonNls public static final String GRADLE_API_ARTIFACTS_REPOSITORIES_MAVEN_ARTIFACT_REPOSITORY = "org.gradle.api.artifacts.repositories.MavenArtifactRepository";
@NonNls public static final String GRADLE_API_ARTIFACTS_REPOSITORIES_IVY_ARTIFACT_REPOSITORY = "org.gradle.api.artifacts.repositories.IvyArtifactRepository";
@NonNls public static final String GRADLE_API_ARTIFACTS_REPOSITORIES_FLAT_DIRECTORY_ARTIFACT_REPOSITORY = "org.gradle.api.artifacts.repositories.FlatDirectoryArtifactRepository";
@NonNls public static final String GRADLE_API_ARTIFACTS_MAVEN_MAVEN_DEPLOYER = "org.gradle.api.artifacts.maven.MavenDeployer";
@NonNls public static final String GRADLE_API_PLUGINS_MAVEN_REPOSITORY_HANDLER_CONVENTION = "org.gradle.api.plugins.MavenRepositoryHandlerConvention";
@NonNls public static final String GRADLE_API_INITIALIZATION_SETTINGS = "org.gradle.api.initialization.Settings";