From bde6311c8c3152a991470da51105a2fe6f147da8 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Tue, 21 Apr 2015 16:23:36 +0300 Subject: [PATCH] IDEA-42074 Code completion for 's attributes should base on the attribute --- .../codeInsight/completion/XmlCompletionTest.java | 10 ++++++++++ .../reference/impl/providers/URLReference.java | 11 ++++++++++- .../com/intellij/xml/DefaultXmlNamespaceHelper.java | 2 -- .../src/com/intellij/xml/XmlNamespaceHelper.java | 0 4 files changed, 20 insertions(+), 3 deletions(-) rename xml/{impl => xml-psi-impl}/src/com/intellij/xml/DefaultXmlNamespaceHelper.java (99%) rename xml/{impl => xml-psi-impl}/src/com/intellij/xml/XmlNamespaceHelper.java (100%) diff --git a/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java b/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java index 68df40c14942..fdc3ab745b86 100644 --- a/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java +++ b/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java @@ -719,5 +719,15 @@ public class XmlCompletionTest extends LightCodeInsightFixtureTestCase { "http://www.w3.org/2001/XMLSchema ", "http://www.w3.org/2001/XMLSchema-instance "); myFixture.testCompletionVariants("SchemaLocation2.xml", "http://www.w3.org/2001/XMLSchema.xsd"); } + + public void testNamespaceCompletion() throws Exception { + myFixture.configureByText("foo.xml", "\"/>"); + myFixture.completeBasic(); + assertSameElements(myFixture.getLookupElementStrings(), "http://www.w3.org/2001/XMLSchema"); + + myFixture.configureByText("unknown.xml", "\"/>"); + myFixture.completeBasic(); + assertTrue(myFixture.getLookupElementStrings().size() > 3); // all standard schemas actually + } } diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/URLReference.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/URLReference.java index 3ae8bc8d24dd..54424e6847bf 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/URLReference.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/URLReference.java @@ -33,6 +33,7 @@ import com.intellij.util.ArrayUtil; import com.intellij.util.IncorrectOperationException; import com.intellij.util.Processor; import com.intellij.xml.XmlNSDescriptor; +import com.intellij.xml.XmlNamespaceHelper; import com.intellij.xml.XmlSchemaProvider; import com.intellij.xml.util.XmlUtil; import org.jetbrains.annotations.NonNls; @@ -206,6 +207,14 @@ public class URLReference implements PsiReference, EmptyResolveMessageProvider { @NotNull public Object[] getVariants() { final XmlFile file = (XmlFile)myElement.getContainingFile(); + PsiElement parent = myElement.getParent(); + if (parent instanceof XmlAttribute && "xmlns".equals(((XmlAttribute)parent).getName())) { + XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file); + Set strings = helper.guessUnboundNamespaces(parent.getParent(), file); + if (!strings.isEmpty()) { + return strings.toArray(); + } + } Set list = new HashSet(); for (XmlSchemaProvider provider : Extensions.getExtensions(XmlSchemaProvider.EP_NAME)) { if (provider.isAvailable(file)) { @@ -215,7 +224,7 @@ public class URLReference implements PsiReference, EmptyResolveMessageProvider { if (!list.isEmpty()) { return ArrayUtil.toObjectArray(list); } - String[] resourceUrls = ExternalResourceManager.getInstance().getResourceUrls(null, true); + Object[] resourceUrls = ExternalResourceManagerEx.getInstanceEx().getUrlsByNamespace(myElement.getProject()).keySet().toArray(); final XmlDocument document = file.getDocument(); assert document != null; XmlTag rootTag = document.getRootTag(); diff --git a/xml/impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java b/xml/xml-psi-impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java similarity index 99% rename from xml/impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java rename to xml/xml-psi-impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java index e351c7bfa309..7eb86750d2d6 100644 --- a/xml/impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java +++ b/xml/xml-psi-impl/src/com/intellij/xml/DefaultXmlNamespaceHelper.java @@ -231,6 +231,4 @@ public class DefaultXmlNamespaceHelper extends XmlNamespaceHelper { } return set; } - - } diff --git a/xml/impl/src/com/intellij/xml/XmlNamespaceHelper.java b/xml/xml-psi-impl/src/com/intellij/xml/XmlNamespaceHelper.java similarity index 100% rename from xml/impl/src/com/intellij/xml/XmlNamespaceHelper.java rename to xml/xml-psi-impl/src/com/intellij/xml/XmlNamespaceHelper.java