diff options
author | Tor Norbye <tnorbye@google.com> | 2012-12-05 16:27:16 -0800 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2012-12-05 16:27:16 -0800 |
commit | 4c98d876bb9160de6df30dd580b39bb1973eb945 (patch) | |
tree | a8a45496e854e32bf4eae0ab3f805bf828b75d88 /eclipse | |
parent | 408c81cecd6d1bd19fb9e727b9e3fc9a37e614e5 (diff) | |
parent | ba7270b2b13b437bd9b384a47672d6b7820e6d81 (diff) | |
download | sdk-4c98d876bb9160de6df30dd580b39bb1973eb945.zip sdk-4c98d876bb9160de6df30dd580b39bb1973eb945.tar.gz sdk-4c98d876bb9160de6df30dd580b39bb1973eb945.tar.bz2 |
Merge "Add namespace elements from code completion lazily"
Diffstat (limited to 'eclipse')
4 files changed, 61 insertions, 11 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java index c98e94f..223e5e5 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java @@ -753,11 +753,11 @@ public class AdtUtils { editor.wrapUndoEditXmlModel(description, new Runnable() { @Override public void run() { - String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, null); + String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, null, true); if (prefix == null) { // Add in new prefix... prefix = XmlUtils.lookupNamespacePrefix(element, - TOOLS_URI, TOOLS_PREFIX); + TOOLS_URI, TOOLS_PREFIX, true /*create*/); if (value != null) { // ...and ensure that the header is formatted such that // the XML namespace declaration is placed in the right @@ -880,11 +880,11 @@ public class AdtUtils { Document doc = domModel.getDocument(); if (doc != null && element.getOwnerDocument() == doc) { String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, - null); + null, true); if (prefix == null) { // Add in new prefix... prefix = XmlUtils.lookupNamespacePrefix(element, - TOOLS_URI, TOOLS_PREFIX); + TOOLS_URI, TOOLS_PREFIX, true); } String v = value; diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidContentAssist.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidContentAssist.java index e620fc3..5aac51f 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidContentAssist.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidContentAssist.java @@ -572,6 +572,7 @@ public abstract class AndroidContentAssist implements IContentAssistProcessor { for (Object choice : choices) { String keyword = null; String nsPrefix = null; + String nsUri = null; Image icon = null; String tooltip = null; if (choice instanceof ElementDescriptor) { @@ -589,11 +590,11 @@ public abstract class AndroidContentAssist implements IContentAssistProcessor { // Get the namespace URI for the attribute. Note that some attributes // do not have a namespace and thus return null here. - String nsUri = ((AttributeDescriptor)choice).getNamespaceUri(); + nsUri = ((AttributeDescriptor)choice).getNamespaceUri(); if (nsUri != null) { nsPrefix = nsUriMap.get(nsUri); if (nsPrefix == null) { - nsPrefix = XmlUtils.lookupNamespacePrefix(currentNode, nsUri); + nsPrefix = XmlUtils.lookupNamespacePrefix(currentNode, nsUri, false); nsUriMap.put(nsUri, nsPrefix); } } @@ -687,7 +688,9 @@ public abstract class AndroidContentAssist implements IContentAssistProcessor { icon, // Image image displayString, // displayString null, // IContextInformation contextInformation - tooltip // String additionalProposalInfo + tooltip, // String additionalProposalInfo + nsPrefix, + nsUri )); } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/CompletionProposal.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/CompletionProposal.java index b52f4db..2d44677 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/CompletionProposal.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/CompletionProposal.java @@ -15,6 +15,8 @@ */ package com.android.ide.eclipse.adt.internal.editors; +import static com.android.SdkConstants.XMLNS; + import com.android.ide.common.api.IAttributeInfo; import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor; @@ -22,7 +24,9 @@ import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor; import com.android.ide.eclipse.adt.internal.editors.descriptors.IDescriptorProvider; import com.android.ide.eclipse.adt.internal.editors.descriptors.TextAttributeDescriptor; +import com.android.ide.eclipse.adt.internal.editors.layout.gle2.DomUtilities; import com.android.ide.eclipse.adt.internal.sdk.AndroidTargetData; +import com.android.utils.XmlUtils; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.ISourceRange; @@ -30,10 +34,15 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.Position; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -59,18 +68,21 @@ class CompletionProposal implements ICompletionProposal { private final AndroidContentAssist mAssist; private final Object mChoice; private final int mCursorPosition; - private final int mReplacementOffset; + private int mReplacementOffset; private final int mReplacementLength; private final String mReplacementString; private final Image mImage; private final String mDisplayString; private final IContextInformation mContextInformation; + private final String mNsPrefix; + private final String mNsUri; private String mAdditionalProposalInfo; CompletionProposal(AndroidContentAssist assist, Object choice, String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, - IContextInformation contextInformation, String additionalProposalInfo) { + IContextInformation contextInformation, String additionalProposalInfo, + String nsPrefix, String nsUri) { assert replacementString != null; assert replacementOffset >= 0; assert replacementLength >= 0; @@ -86,6 +98,8 @@ class CompletionProposal implements ICompletionProposal { mDisplayString = displayString; mContextInformation = contextInformation; mAdditionalProposalInfo = additionalProposalInfo; + mNsPrefix = nsPrefix; + mNsUri = nsUri; } @Override @@ -174,6 +188,39 @@ class CompletionProposal implements ICompletionProposal { @Override public void apply(IDocument document) { try { + Position position = new Position(mReplacementOffset); + document.addPosition(position); + + // Ensure that the namespace is defined in the document + String prefix = mNsPrefix; + if (mNsUri != null && prefix != null) { + Document dom = DomUtilities.getDocument(mAssist.getEditor()); + if (dom != null) { + Element root = dom.getDocumentElement(); + if (root != null) { + // Is the namespace already defined? + boolean found = false; + NamedNodeMap attributes = root.getAttributes(); + for (int i = 0, n = attributes.getLength(); i < n; i++) { + Attr attribute = (Attr) attributes.item(i); + String name = attribute.getName(); + if (name.startsWith(XMLNS) && mNsUri.equals(attribute.getValue())) { + found = true; + break; + } + } + if (!found) { + if (prefix.endsWith(":")) { //$NON-NLS-1$ + prefix = prefix.substring(0, prefix.length() - 1); + } + XmlUtils.lookupNamespacePrefix(root, mNsUri, prefix, true); + } + } + } + } + + mReplacementOffset = position.getOffset(); + document.removePosition(position); document.replace(mReplacementOffset, mReplacementLength, mReplacementString); } catch (BadLocationException x) { // ignore diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutMetadataTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutMetadataTest.java index fa9e18f..c71064e 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutMetadataTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/LayoutMetadataTest.java @@ -53,11 +53,11 @@ public class LayoutMetadataTest extends AdtProjectTest { assertNull(LayoutMetadata.getProperty(node, "foo")); Element element = (Element) node; - String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, null); + String prefix = XmlUtils.lookupNamespacePrefix(element, TOOLS_URI, null, false); if (prefix == null) { // Add in new prefix... prefix = XmlUtils.lookupNamespacePrefix(element, - TOOLS_URI, TOOLS_PREFIX); + TOOLS_URI, TOOLS_PREFIX, true); } element.setAttribute(prefix + ':' + "foo", "bar"); } |