From 7bd0eb398eac8bdfd8944b094ca9dee3a55ff8ae Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Tue, 19 Mar 2013 15:50:27 +0400 Subject: [PATCH] IDEA-101900 Groovy 2.1: Custom type checkers --- plugins/groovy/src/META-INF/plugin.xml | 5 + .../GroovyInspectionBundle.properties | 8 +- .../resources/TypeCustomizerInspection.java | 136 ++++++++++++++++++ 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/resources/TypeCustomizerInspection.java diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 8dc02ddce040..fd33c8bb0dfc 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -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"/> + 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() { + @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); + } + } + } +}