From 141d3c55c659092468c1636c3a815812ecef9c3e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 20 May 2015 13:35:22 +0200 Subject: [PATCH] groovy: package dependency visitor implementation --- plugins/groovy/src/META-INF/plugin.xml | 3 + .../GrDependencyVisitorFactory.groovy | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index c8c8a5c5a7cf..ab9435bd6635 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -1105,6 +1105,9 @@ level="WARNING" implementationClass="org.jetbrains.plugins.groovy.codeInspection.bugs.GrEqualsBetweenInconvertibleTypesInspection"/> + + diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy new file mode 100644 index 000000000000..cccaa3954c67 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy @@ -0,0 +1,85 @@ +/* + * Copyright 2000-2015 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. + */ +package org.jetbrains.plugins.groovy.codeInspection.dependencies + +import com.intellij.openapi.progress.ProgressIndicatorProvider +import com.intellij.packageDependencies.DependenciesBuilder +import com.intellij.packageDependencies.DependencyVisitorFactory +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiElementVisitor +import com.intellij.psi.PsiFile +import org.jetbrains.annotations.NotNull +import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment +import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement +import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral +import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement + +class GrDependencyVisitorFactory extends DependencyVisitorFactory { + @NotNull + @Override + PsiElementVisitor getVisitor(@NotNull DependenciesBuilder.DependencyProcessor processor, + @NotNull DependencyVisitorFactory.VisitorOptions options) { + GroovyElementVisitor visitor = new MyVisitor(processor, options) + new PsiElementVisitor() { + @Override + void visitFile(PsiFile file) { + if (file instanceof GroovyFile) { + file.accept(visitor) + } + } + } + } + + private static class MyVisitor extends GroovyRecursiveElementVisitor { + private final DependenciesBuilder.DependencyProcessor myProcessor + private final DependencyVisitorFactory.VisitorOptions myOptions + + MyVisitor(DependenciesBuilder.DependencyProcessor processor, DependencyVisitorFactory.VisitorOptions options) { + myOptions = options + myProcessor = processor + } + + @Override + void visitElement(GroovyPsiElement element) { + ProgressIndicatorProvider.checkCanceled() + + super.visitElement(element) + + element.references.each { + PsiElement resolved = it.resolve(); + if (resolved != null) { + myProcessor.process(it.element, resolved); + } + } + } + + @Override + void visitImportStatement(GrImportStatement statement) { + if (!myOptions.skipImports()) { + visitElement(statement); + } + } + + @Override + void visitDocComment(GrDocComment comment) {} + + @Override + void visitLiteralExpression(GrLiteral literal) {} + } +}