mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-197050 IntelliJ doesn't find my namespaces when using autocompletion in an XML Schema file
This commit is contained in:
+14
-6
@@ -34,12 +34,14 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import com.intellij.xml.XmlNamespaceHelper;
|
||||
import com.intellij.xml.XmlSchemaProvider;
|
||||
import com.intellij.xml.index.XmlNamespaceIndex;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
@@ -81,11 +83,6 @@ public class XmlLocationCompletionContributor extends CompletionContributor {
|
||||
private static Object[] completeNamespace(PsiElement myElement) {
|
||||
final XmlFile file = (XmlFile)myElement.getContainingFile();
|
||||
PsiElement parent = myElement.getParent();
|
||||
final Set<Object> preferred = new HashSet<>();
|
||||
if (parent instanceof XmlAttribute && "xmlns".equals(((XmlAttribute)parent).getName())) {
|
||||
XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
|
||||
preferred.addAll(helper.guessUnboundNamespaces(parent.getParent(), file));
|
||||
}
|
||||
Set<String> list = new HashSet<>();
|
||||
for (XmlSchemaProvider provider : Extensions.getExtensions(XmlSchemaProvider.EP_NAME)) {
|
||||
if (provider.isAvailable(file)) {
|
||||
@@ -95,7 +92,13 @@ public class XmlLocationCompletionContributor extends CompletionContributor {
|
||||
if (!list.isEmpty()) {
|
||||
return ArrayUtil.toObjectArray(list);
|
||||
}
|
||||
Object[] resourceUrls = ExternalResourceManagerEx.getInstanceEx().getUrlsByNamespace(myElement.getProject()).keySet().toArray();
|
||||
Set<String> set = new HashSet<>(ExternalResourceManagerEx.getInstanceEx().getUrlsByNamespace(myElement.getProject()).keySet());
|
||||
Set<String> fromIndex =
|
||||
XmlNamespaceIndex.getAllResources(null, myElement.getProject(), null).stream()
|
||||
.filter(resource -> "xsd".equals(resource.getFile().getExtension())).map(resource -> resource.getValue().getNamespace())
|
||||
.collect(Collectors.toSet());
|
||||
ContainerUtil.addAllNotNull(set, fromIndex);
|
||||
Object[] resourceUrls = set.toArray();
|
||||
final XmlDocument document = file.getDocument();
|
||||
assert document != null;
|
||||
XmlTag rootTag = document.getRootTag();
|
||||
@@ -107,6 +110,11 @@ public class XmlLocationCompletionContributor extends CompletionContributor {
|
||||
});
|
||||
resourceUrls = ArrayUtil.mergeArrays(resourceUrls, ArrayUtil.toStringArray(additionalNs));
|
||||
|
||||
final Set<Object> preferred = new HashSet<>();
|
||||
if (parent instanceof XmlAttribute && "xmlns".equals(((XmlAttribute)parent).getName())) {
|
||||
XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
|
||||
preferred.addAll(helper.guessUnboundNamespaces(parent.getParent(), file));
|
||||
}
|
||||
return ContainerUtil.map2Array(resourceUrls, o -> {
|
||||
LookupElementBuilder builder = LookupElementBuilder.create(o);
|
||||
return preferred.contains(o) ? PrioritizedLookupElement.withPriority(builder, 100) : builder;
|
||||
|
||||
@@ -34,6 +34,7 @@ import com.intellij.psi.statistics.impl.StatisticsManagerImpl;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.xml.util.XmlUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -755,6 +756,12 @@ public class XmlCompletionTest extends LightCodeInsightFixtureTestCase {
|
||||
assertTrue(myFixture.getLookupElementStrings().size() > 3); // all standard schemas actually
|
||||
}
|
||||
|
||||
public void testCustomNamespaceCompletion() {
|
||||
myFixture.configureByFiles("main.xsd", "sub.xsd");
|
||||
LookupElement[] elements = myFixture.completeBasic();
|
||||
assertTrue(Arrays.stream(elements).anyMatch(element -> "http://www.test.com/sub".equals(element.getLookupString())));
|
||||
}
|
||||
|
||||
public void testRootTagCompletion() {
|
||||
boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION;
|
||||
CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.test.com/main"
|
||||
xmlns:tns="http://www.test.com/main"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<import schemaLocation="sub.xsd" namespace="<caret>"/>
|
||||
</schema>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.test.com/sub"
|
||||
xmlns:tns="http://www.test.com/sub"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
</schema>
|
||||
Reference in New Issue
Block a user