OSDN Git Service

Make Go To Declaration work for <fragment> names.
authorTor Norbye <tnorbye@google.com>
Thu, 28 Apr 2011 22:51:38 +0000 (15:51 -0700)
committerTor Norbye <tnorbye@google.com>
Sat, 14 May 2011 02:17:36 +0000 (19:17 -0700)
Change-Id: Ib2e8d8b93cdb52717719296dec0cbcf9780802ff

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/xml/Hyperlinks.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/AdtProjectTest.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/TestFragment.java.txt [new file with mode: 0644]
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate13.txt [new file with mode: 0644]
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate14.txt [new file with mode: 0644]
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout.xml [new file with mode: 0644]
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/xml/HyperlinksTest.java

index bb04498..c520ab9 100644 (file)
@@ -32,6 +32,9 @@ public class LayoutConstants {
     /** The element name in a {@code <view class="...">} element. */
     public static final String VIEW = "view";                           //$NON-NLS-1$
 
+    /** The element name in a <code>&lt;fragment android:name="..."&gt;</code> element. */
+    public static final String FRAGMENT = "fragment";                   //$NON-NLS-1$
+
     /** The attribute name in a {@code <view class="...">} element. */
     public static final String ATTR_CLASS = "class";                    //$NON-NLS-1$
     public static final String ATTR_ON_CLICK = "onClick";               //$NON-NLS-1$
@@ -99,6 +102,7 @@ public class LayoutConstants {
 
     public static final String ATTR_LAYOUT_Y = "layout_y";                      //$NON-NLS-1$
     public static final String ATTR_LAYOUT_X = "layout_x";                      //$NON-NLS-1$
+    public static final String ATTR_NAME = "name";                              //$NON-NLS-1$
 
     public static final String VALUE_WRAP_CONTENT = "wrap_content";             //$NON-NLS-1$
     public static final String VALUE_FILL_PARENT = "fill_parent";               //$NON-NLS-1$
index 83e9bc4..c30b78a 100644 (file)
@@ -18,7 +18,9 @@ package com.android.ide.eclipse.adt.internal.editors.xml;
 
 import static com.android.ide.common.layout.LayoutConstants.ANDROID_URI;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_CLASS;
+import static com.android.ide.common.layout.LayoutConstants.ATTR_NAME;
 import static com.android.ide.common.layout.LayoutConstants.ATTR_ON_CLICK;
+import static com.android.ide.common.layout.LayoutConstants.FRAGMENT;
 import static com.android.ide.common.layout.LayoutConstants.NEW_ID_PREFIX;
 import static com.android.ide.common.layout.LayoutConstants.VIEW;
 import static com.android.ide.common.resources.ResourceResolver.PREFIX_ANDROID_RESOURCE_REF;
@@ -321,20 +323,25 @@ public class Hyperlinks {
         return false;
     }
 
-    /** Returns true if this represents a {@code <view class="foo.bar.Baz">} class attribute */
+    /** Returns true if this represents a style attribute */
     private static boolean isStyleAttribute(XmlContext context) {
         String tag = context.getElement().getTagName();
         return STYLE_ELEMENT.equals(tag);
     }
 
-    /** Returns true if this represents a {@code <view class="foo.bar.Baz">} class attribute */
+    /**
+     * Returns true if this represents a {@code <view class="foo.bar.Baz">} class
+     * attribute, or a {@code <fragment android:name="foo.bar.Baz">} class attribute
+     */
     private static boolean isClassAttribute(XmlContext context) {
         Attr attribute = context.getAttribute();
         if (attribute == null) {
             return false;
         }
         String tag = context.getElement().getTagName();
-        return ATTR_CLASS.equals(attribute.getLocalName()) && VIEW.equals(tag);
+        String attributeName = attribute.getLocalName();
+        return ATTR_CLASS.equals(attributeName) && (VIEW.equals(tag) || FRAGMENT.equals(tag))
+                || ATTR_NAME.equals(attributeName) && FRAGMENT.equals(tag);
     }
 
     /** Returns true if this represents an onClick attribute specifying a method handler */
index 3b83bd7..0caeef3 100644 (file)
@@ -67,6 +67,8 @@ import java.util.Map;
 @SuppressWarnings("restriction")
 public class AdtProjectTest extends SdkTestCase {
     private static final int TARGET_API_LEVEL = 11;
+    public static final String TEST_PROJECT_PACKAGE = "com.android.eclipse.tests"; //$NON-NLS-1$
+
     /** Update golden files if different from the actual results */
     private static final boolean UPDATE_DIFFERENT_FILES = false;
     /** Create golden files if missing */
@@ -689,7 +691,7 @@ public class AdtProjectTest extends SdkTestCase {
                 }
 
                 public String getPackageName() {
-                    return "com.android.eclipse.tests";
+                    return TEST_PROJECT_PACKAGE;
                 }
 
                 public String getActivityName() {
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/TestFragment.java.txt b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/TestFragment.java.txt
new file mode 100644 (file)
index 0000000..0942a19
--- /dev/null
@@ -0,0 +1,15 @@
+package com.android.eclipse.tests;
+
+import android.app.Fragment;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+public class TestFragment extends Fragment {
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        return null;
+    }
+}
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate13.txt b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate13.txt
new file mode 100644 (file)
index 0000000..ae30d58
--- /dev/null
@@ -0,0 +1,12 @@
+Go To Declaration in fragmentlayout.xml for android:name="com.android.ecl^ipse.tests.TestFragment":
+Open XML Declaration :  [com.android.eclipse.tests.TestFragment]
+
+
+After open, the selected text is:
+[^public class TestFragment extends Fragment {
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        return null;
+    }
+}]
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate14.txt b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout-expected-navigate14.txt
new file mode 100644 (file)
index 0000000..0046483
--- /dev/null
@@ -0,0 +1,12 @@
+Go To Declaration in fragmentlayout.xml for class="com.and^roid.eclipse.tests.TestFragment":
+Open XML Declaration :  [com.android.eclipse.tests.TestFragment]
+
+
+After open, the selected text is:
+[^public class TestFragment extends Fragment {
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        return null;
+    }
+}]
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/fragmentlayout.xml
new file mode 100644 (file)
index 0000000..758599c
--- /dev/null
@@ -0,0 +1,19 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/home_root"
+    android:orientation="horizontal"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent">
+
+    <LinearLayout android:orientation="vertical"
+        android:layout_width="wrap_content"
+        android:layout_height="fill_parent">
+        <fragment android:name="com.android.eclipse.tests.TestFragment"
+            android:id="@+id/test_fragment"
+            android:layout_marginLeft="20dp"
+            android:layout_weight="1"
+            android:layout_width="fill_parent"
+            android:layout_height="0dp" />
+        <fragment class="com.android.eclipse.tests.TestFragment"
+             android:id="@+id/test_fragment2" />
+    </LinearLayout>
+</LinearLayout>
index 57ca80f..afe6f3b 100644 (file)
@@ -15,6 +15,8 @@
  */
 package com.android.ide.eclipse.adt.internal.editors.xml;
 
+import static com.android.sdklib.SdkConstants.FD_SOURCES;
+
 import com.android.ide.common.resources.ResourceFile;
 import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor;
 import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest;
@@ -22,6 +24,7 @@ import com.android.ide.eclipse.adt.internal.editors.xml.Hyperlinks.ResourceLink;
 import com.android.ide.eclipse.adt.internal.editors.xml.Hyperlinks.XmlResolver;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.Region;
@@ -157,6 +160,24 @@ public class HyperlinksTest extends AdtProjectTest {
                 "<my.Cust^omView></my.CustomView>");
     }
 
+    public void testNavigate13() throws Exception {
+        // Check jumping to classes pointed to by fragments
+
+        getTestDataFile(getProject(), "TestFragment.java.txt",
+                FD_SOURCES + "/" + TEST_PROJECT_PACKAGE.replace('.', '/') + "/TestFragment.java");
+        checkXmlNavigation("fragmentlayout.xml", "res/layout/fragmentlayout.xml",
+                "android:name=\"com.android.ecl^ipse.tests.TestFragment\"");
+    }
+
+    public void testNavigate14() throws Exception {
+        // Check jumping to classes pointed to by fragments
+
+        getTestDataFile(getProject(), "TestFragment.java.txt",
+                FD_SOURCES + "/" + TEST_PROJECT_PACKAGE.replace('.', '/') + "/TestFragment.java");
+        checkXmlNavigation("fragmentlayout.xml", "res/layout/fragmentlayout.xml",
+                "class=\"com.and^roid.eclipse.tests.TestFragment\"");
+    }
+
     // Left to test:
     // onClick handling
     // class attributes
@@ -248,6 +269,11 @@ public class HyperlinksTest extends AdtProjectTest {
             sb.append("  ");
             sb.append(initialUrl);
             sb.append("\n");
+        } else if (newEditor instanceof JavaEditor) {
+            JavaEditor javaEditor = (JavaEditor) newEditor;
+            document = javaEditor.getDocumentProvider().getDocument(javaEditor.getEditorInput());
+            IRegion range = javaEditor.getHighlightRange();
+            selection = new Point(range.getOffset(), range.getLength());
         } else {
             fail("Unhandled editor type: " + newEditor.getClass().getName());
             return;