[groovy] allow to plug into package name detection
authorDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Thu, 2 Jun 2016 12:58:16 +0000 (14:58 +0200)
committerDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Thu, 2 Jun 2016 14:28:42 +0000 (16:28 +0200)
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/codeInspection/confusing/GrPackageInspection.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/ExpectedPackageNameProvider.kt [new file with mode: 0644]
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/ResolveUtil.java
plugins/groovy/src/META-INF/plugin.xml

index 8a1a6007f3148156910b434d739979baaf8029f7..476f40d0c72d76cc3c8c2df53aecf5b378bb9743 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2016 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.
@@ -38,7 +38,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
 import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
 import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
-import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
+import org.jetbrains.plugins.groovy.lang.resolve.ExpectedPackageNameProviderKt;
 
 import javax.swing.*;
 
@@ -89,7 +89,7 @@ public class GrPackageInspection extends BaseInspection {
 
         if (!myCheckScripts && file.isScript()) return;
 
-        String expectedPackage = ResolveUtil.inferExpectedPackageName(file);
+        String expectedPackage = ExpectedPackageNameProviderKt.inferExpectedPackageName((GroovyFile)file);
         String actual = file.getPackageName();
         if (!expectedPackage.equals(actual)) {
 
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/ExpectedPackageNameProvider.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/resolve/ExpectedPackageNameProvider.kt
new file mode 100644 (file)
index 0000000..af07f7c
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2000-2016 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.lang.resolve
+
+import com.intellij.openapi.extensions.ExtensionPointName
+import com.intellij.psi.JavaDirectoryService
+import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
+
+interface ExpectedPackageNameProvider {
+
+  fun inferPackageName(file: GroovyFile): String?
+}
+
+class DefaultExpectedPackageNameProvider : ExpectedPackageNameProvider {
+
+  override fun inferPackageName(file: GroovyFile) = file.containingDirectory?.let {
+    JavaDirectoryService.getInstance().getPackage(it)?.qualifiedName
+  }
+}
+
+private val EP_NAME = ExtensionPointName.create<ExpectedPackageNameProvider>("org.intellij.groovy.expectedPackageNameProvider")
+
+fun inferExpectedPackageName(file: GroovyFile): String {
+  for (ext in EP_NAME.extensions) {
+    val name = ext.inferPackageName(file) ?: continue
+    return name
+  }
+  return ""
+}
\ No newline at end of file
index ee90d21b244e33c49587c897e09403b8eb9b93b8..02fca05c7f61eaf90849730f3483e3f03a4ea297 100644 (file)
@@ -925,19 +925,6 @@ public class ResolveUtil {
     return null;
   }
 
-  @NotNull
-  public static String inferExpectedPackageName(PsiElement place) {
-    PsiFile file = place.getContainingFile();
-    PsiDirectory psiDirectory = file.getContainingDirectory();
-    if (psiDirectory != null && file instanceof GroovyFile) {
-      PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
-      if (aPackage != null) {
-        return aPackage.getQualifiedName();
-      }
-    }
-    return "";
-  }
-
   public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
     if (isScriptField(variable)) {
       final String name = variable.getName();
index 75056aeb3f5b41f026abadf0d214a5bf006b63ff..0188b03d4c7e525556b3875c3d0337cb6ebfe42b 100644 (file)
@@ -84,6 +84,7 @@
     <extensionPoint name="signatureHintProcessor" interface="org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.SignatureHintProcessor"/>
     <extensionPoint name="mvc.runCommandHandler" interface="org.jetbrains.plugins.groovy.mvc.actions.MvcRunCommandActionHandler"/>
     <extensionPoint name="gdslScriptProvider" interface="org.jetbrains.plugins.groovy.dsl.GdslScriptProvider"/>
+    <extensionPoint name="expectedPackageNameProvider" interface="org.jetbrains.plugins.groovy.lang.resolve.ExpectedPackageNameProvider"/>
   </extensionPoints>
 
   <extensions defaultExtensionNs="org.intellij.groovy">
     <customAnnotationChecker implementation="org.jetbrains.plugins.groovy.annotator.checkers.TypeCheckedAnnotationChecker"/>
     <mvc.runCommandHandler implementation="org.jetbrains.plugins.groovy.mvc.actions.DefaultMvcRunCommandActionHandler" order="last"/>
     <methodComparator implementation="org.jetbrains.plugins.groovy.lang.resolve.GrDefaultMethodComparator" order="last"/>
+    <expectedPackageNameProvider implementation="org.jetbrains.plugins.groovy.lang.resolve.DefaultExpectedPackageNameProvider"
+                                 order="last"/>
   </extensions>
 
   <extensions defaultExtensionNs="com.intellij.properties">