fixed source root detection for java files in default package (IDEA-74616)
authornik <Nikolay.Chashnikov@jetbrains.com>
Mon, 10 Oct 2011 07:44:13 +0000 (11:44 +0400)
committernik <Nikolay.Chashnikov@jetbrains.com>
Mon, 10 Oct 2011 07:44:41 +0000 (11:44 +0400)
java/java-impl/src/com/intellij/ide/util/JavaUtil.java
java/java-tests/testData/ide/sourceRootDetection/defaultPackage/src/MyClass.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/defaultPackageWithImport/src/MyClass.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/garbage/EmptyClass.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/garbage/Garbage.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/simple/src/pack/MyClass.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/twoRoots/src1/abc/MyClass.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/twoRoots/src2/Class2.java [new file with mode: 0644]
java/java-tests/testData/ide/sourceRootDetection/withPrefix/MyClass.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/ide/util/JavaSourceRootDetectionTest.java [new file with mode: 0644]

index 4f2dc833ead98aba1e2d603fd3fefb4680ce7f47..433697e4cd8ae67759996c1ef4ec2428f51be1c6 100644 (file)
@@ -29,6 +29,8 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.pom.java.LanguageLevel;
 import com.intellij.psi.JavaTokenType;
 import com.intellij.psi.impl.source.tree.ElementType;
+import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.tree.TokenSet;
 import com.intellij.util.StringBuilderSpinAllocator;
 import com.intellij.util.text.CharArrayCharSequence;
 import org.jetbrains.annotations.Nullable;
@@ -40,6 +42,12 @@ import java.util.List;
 
 
 public class JavaUtil {
+  private static final TokenSet JAVA_FILE_FIRST_TOKEN_SET = TokenSet.orSet(
+    ElementType.MODIFIER_BIT_SET,
+    ElementType.CLASS_KEYWORD_BIT_SET,
+    TokenSet.create(JavaTokenType.AT, JavaTokenType.IMPORT_KEYWORD)
+  );
+
   private JavaUtil() { }
 
   public static List<Pair<File,String>> suggestRoots(File dir, LanguageFileType fileType) {
@@ -173,11 +181,17 @@ public class JavaUtil {
   }
 
   @Nullable
-  public static String getPackageStatement(CharSequence text){
+  public static String getPackageStatement(CharSequence text) {
     Lexer lexer = new JavaLexer(LanguageLevel.JDK_1_3);
     lexer.start(text);
     skipWhiteSpaceAndComments(lexer);
-    if (lexer.getTokenType() != JavaTokenType.PACKAGE_KEYWORD) return null;
+    final IElementType firstToken = lexer.getTokenType();
+    if (firstToken != JavaTokenType.PACKAGE_KEYWORD) {
+      if (JAVA_FILE_FIRST_TOKEN_SET.contains(firstToken)) {
+        return "";
+      }
+      return null;
+    }
     lexer.advance();
     skipWhiteSpaceAndComments(lexer);
 
diff --git a/java/java-tests/testData/ide/sourceRootDetection/defaultPackage/src/MyClass.java b/java/java-tests/testData/ide/sourceRootDetection/defaultPackage/src/MyClass.java
new file mode 100644 (file)
index 0000000..f717ea4
--- /dev/null
@@ -0,0 +1,2 @@
+public class MyClass {
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/defaultPackageWithImport/src/MyClass.java b/java/java-tests/testData/ide/sourceRootDetection/defaultPackageWithImport/src/MyClass.java
new file mode 100644 (file)
index 0000000..a1f8b59
--- /dev/null
@@ -0,0 +1,9 @@
+import java.util.*;
+//aaa
+import java.io.OutputStream;
+
+/**
+ * my class
+ */
+public class MyClass {
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/garbage/EmptyClass.java b/java/java-tests/testData/ide/sourceRootDetection/garbage/EmptyClass.java
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/java/java-tests/testData/ide/sourceRootDetection/garbage/Garbage.java b/java/java-tests/testData/ide/sourceRootDetection/garbage/Garbage.java
new file mode 100644 (file)
index 0000000..d58adb0
--- /dev/null
@@ -0,0 +1 @@
+garbage
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/simple/src/pack/MyClass.java b/java/java-tests/testData/ide/sourceRootDetection/simple/src/pack/MyClass.java
new file mode 100644 (file)
index 0000000..9578e46
--- /dev/null
@@ -0,0 +1,5 @@
+//comment
+package pack;
+
+public class MyClass {
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/twoRoots/src1/abc/MyClass.java b/java/java-tests/testData/ide/sourceRootDetection/twoRoots/src1/abc/MyClass.java
new file mode 100644 (file)
index 0000000..6581f68
--- /dev/null
@@ -0,0 +1,3 @@
+package abc;
+
+class MyClass{}
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/twoRoots/src2/Class2.java b/java/java-tests/testData/ide/sourceRootDetection/twoRoots/src2/Class2.java
new file mode 100644 (file)
index 0000000..3109a36
--- /dev/null
@@ -0,0 +1,3 @@
+package xyz;
+
+public class Class2{}
\ No newline at end of file
diff --git a/java/java-tests/testData/ide/sourceRootDetection/withPrefix/MyClass.java b/java/java-tests/testData/ide/sourceRootDetection/withPrefix/MyClass.java
new file mode 100644 (file)
index 0000000..12b26e5
--- /dev/null
@@ -0,0 +1,4 @@
+package xxx.yyy;
+
+public class MyClass {
+}
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/ide/util/JavaSourceRootDetectionTest.java b/java/java-tests/testSrc/com/intellij/ide/util/JavaSourceRootDetectionTest.java
new file mode 100644 (file)
index 0000000..75c7795
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2000-2011 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 com.intellij.ide.util;
+
+import com.intellij.JavaTestUtil;
+import com.intellij.ide.highlighter.JavaFileType;
+import com.intellij.openapi.util.Pair;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.testFramework.PlatformTestCase;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author nik
+ */
+public class JavaSourceRootDetectionTest extends PlatformTestCase {
+  public void testSimple() {
+    doTest("src", "");
+  }
+
+  public void testWithPrefix() {
+    doTest("", "xxx.yyy");
+  }
+
+  public void testTwoRoots() {
+    doTest("src1", "", "src2", "xyz");
+  }
+
+  public void testDefaultPackage() {
+    doTest("src", "");
+  }
+
+  public void testDefaultPackageWithImport() {
+    doTest("src", "");
+  }
+
+  public void testGarbage() {
+    doTest();
+  }
+
+  private void doTest(String... expected) {
+    final String dirPath = JavaTestUtil.getJavaTestDataPath() + FileUtil.toSystemDependentName("/ide/sourceRootDetection/" + getTestName(true));
+    final File dir = new File(dirPath);
+    assertTrue(dir.isDirectory());
+    final List<Pair<File,String>> actual = JavaUtil.suggestRoots(dir, JavaFileType.INSTANCE);
+    List<Pair<File, String>> expectedList = new ArrayList<Pair<File, String>>();
+    for (int i = 0; i < expected.length / 2; i++) {
+      expectedList.add(Pair.create(new File(dir, expected[2 * i]), expected[2 * i + 1]));
+    }
+    assertSameElements(actual, expectedList);
+  }
+}