mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-101900 Groovy 2.1: Custom type checkers
This commit is contained in:
@@ -541,6 +541,11 @@
|
||||
bundle="org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle"
|
||||
key="no.return.display.name" groupKey="groovy.dfa.issues" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection"/>
|
||||
<localInspection language="Groovy" groupPath="Groovy" shortName="TypeCustomizer"
|
||||
displayName="Type customizer inspection"
|
||||
bundle="org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle"
|
||||
groupKey="other" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="org.jetbrains.plugins.groovy.codeInspection.resources.TypeCustomizerInspection"/>
|
||||
<localInspection language="Groovy" groupPath="Groovy" shortName="GroovyAssignabilityCheck" displayName="Incompatible type assignments"
|
||||
groupName="Assignment issues"
|
||||
enabledByDefault="true" level="WARNING"
|
||||
|
||||
+6
-2
@@ -31,6 +31,7 @@ second.unsafe.call=Second unsafe call
|
||||
call.can.throw.npe=Calling chain can throw NullPointerException
|
||||
|
||||
groovy.dfa.issues=Data flow issues
|
||||
other=Other
|
||||
|
||||
unused.assignment=Unused Assignment
|
||||
unused.assignment.tooltip=Assignment is not used
|
||||
@@ -68,7 +69,7 @@ access.to.inaccessible.element=Access to inaccessible element
|
||||
|
||||
type.doesnt.contain.method=Type ''{0}'' cannot be iterated in range because it does not have method ''{1}''
|
||||
incorrect.range.argument=Incorrect range arguments
|
||||
type.doesnt.implemnt.comparable=Type ''{0}'' doesnt implement Comparable
|
||||
type.doesnt.implemnt.comparable=Type ''{0}'' doesn't implement Comparable
|
||||
add.method=Add method ''{0}()'' to class ''{1}''
|
||||
implement.class=Implement {0}
|
||||
fix.class=Fix class {0}
|
||||
@@ -98,4 +99,7 @@ highlight.if.groovy.object.methods.overridden=Highlight references from class ov
|
||||
highlight.if.missing.methods.declared=Highlight references from class declaring missingMethod() or missingProperty()
|
||||
gr.package.inspection.check.scripts=Check scripts
|
||||
gr.package=Package mismatch
|
||||
java.style.property.access=Java-style accessor invocation
|
||||
java.style.property.access=Java-style accessor invocation
|
||||
type.customizer.is.not.marked.as.a.resource.file=Type customizer script is not marked as compiler resources
|
||||
add.to.resources=Add to resources
|
||||
add.type.customizer.to.resources=Add type customizer script to resources
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.codeInspection.resources;
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.compiler.CompilerConfiguration;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
|
||||
import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author Max Medvedev
|
||||
*/
|
||||
public class TypeCustomizerInspection extends BaseInspection {
|
||||
@NotNull
|
||||
@Override
|
||||
protected BaseInspectionVisitor buildVisitor() {
|
||||
return new BaseInspectionVisitor() {
|
||||
@Override
|
||||
public void visitFile(GroovyFileBase file) {
|
||||
if (!CompilerConfiguration.getInstance(file.getProject()).isResourceFile(file.getVirtualFile())) {
|
||||
if (fileSeemsToBeTypeCustomizer(file)) {
|
||||
final LocalQuickFix[] fixes = {new AddToResourceFix(file)};
|
||||
final String message = GroovyInspectionBundle.message("type.customizer.is.not.marked.as.a.resource.file");
|
||||
registerError(file, message, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private static final HashSet<String> CUSTOMIZER_EVENT_NAMES = ContainerUtil
|
||||
.newHashSet("setup", "finish", "unresolvedVariable", "unresolvedProperty", "unresolvedAttribute", "beforeMethodCall", "afterMethodCall",
|
||||
"onMethodSelection", "methodNotFound", "beforeVisitMethod", "afterVisitMethod", "beforeVisitClass", "afterVisitClass",
|
||||
"incompatibleAssignment");
|
||||
|
||||
|
||||
public static boolean fileSeemsToBeTypeCustomizer(@NotNull final PsiFile file) {
|
||||
return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
|
||||
@Override
|
||||
public Boolean compute() {
|
||||
if (file instanceof GroovyFile && ((GroovyFile)file).isScript()) {
|
||||
for (GrStatement statement : ((GroovyFile)file).getStatements()) {
|
||||
if (statement instanceof GrMethodCall) {
|
||||
GrExpression invoked = ((GrMethodCall)statement).getInvokedExpression();
|
||||
if (invoked instanceof GrReferenceExpression &&
|
||||
!((GrReferenceExpression)invoked).isQualified() &&
|
||||
isCustomizerEvent(((GrReferenceExpression)invoked).getReferenceName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
private static boolean isCustomizerEvent(@Nullable String name) {
|
||||
return CUSTOMIZER_EVENT_NAMES.contains(name);
|
||||
}
|
||||
|
||||
private static class AddToResourceFix implements LocalQuickFix {
|
||||
private final PsiFile myFile;
|
||||
|
||||
public AddToResourceFix(PsiFile file) {
|
||||
myFile = file;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return GroovyInspectionBundle.message("add.to.resources");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return GroovyInspectionBundle.message("add.type.customizer.to.resources");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final VirtualFile virtualFile = myFile.getVirtualFile();
|
||||
if (virtualFile == null) return;
|
||||
|
||||
VirtualFile sourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(virtualFile);
|
||||
final VirtualFile projectRoot = project.getBaseDir();
|
||||
if (sourceRoot == null) {
|
||||
final String path = VfsUtilCore.getRelativePath(virtualFile, projectRoot, '/');
|
||||
CompilerConfiguration.getInstance(project).addResourceFilePattern(path);
|
||||
}
|
||||
else {
|
||||
final String path = VfsUtilCore.getRelativePath(virtualFile, sourceRoot, '/');
|
||||
final String sourceRootPath = VfsUtilCore.getRelativePath(sourceRoot, projectRoot, '/');
|
||||
CompilerConfiguration.getInstance(project).addResourceFilePattern(sourceRootPath + ':' + path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user