Add ability to extend fileReference completion for particular references

This commit is contained in:
Alexander Zolotov
2016-02-12 19:39:48 +03:00
parent 90731c6674
commit d7788db34a
3 changed files with 43 additions and 4 deletions
@@ -1,10 +1,29 @@
/*
* Copyright 2000-2016 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.psi.impl.source.resolve.reference.impl.providers;
import com.intellij.psi.PsiFileSystemItem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
public interface FileReferenceResolver {
@Nullable
PsiFileSystemItem resolveFileReference(@NotNull FileReference reference, @NotNull String name);
Collection<Object> getVariants(@NotNull FileReference reference);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -26,6 +26,7 @@ import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CommonProcessors;
import com.intellij.util.FilteringProcessor;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;
@@ -76,17 +77,23 @@ public class FileReferenceCompletionImpl extends FileReferenceCompletion {
FileReference.getOriginalFile(fileSystemItem));
}
};
List<Object> additionalItems = ContainerUtil.newArrayList();
for (PsiFileSystemItem context : reference.getContexts()) {
for (final PsiElement child : context.getChildren()) {
if (child instanceof PsiFileSystemItem) {
processor.execute((PsiFileSystemItem)child);
}
}
if (context instanceof FileReferenceResolver) {
additionalItems.addAll(((FileReferenceResolver)context).getVariants(reference));
}
}
final THashSet<PsiElement> set = new THashSet<PsiElement>(collector.getResults(), VARIANTS_HASHING_STRATEGY);
final PsiElement[] candidates = PsiUtilCore.toPsiElementArray(set);
final Object[] variants = new Object[candidates.length];
final Object[] variants = new Object[candidates.length + additionalItems.size()];
for (int i = 0; i < candidates.length; i++) {
PsiElement candidate = candidates[i];
Object item = reference.createLookupItem(candidate);
@@ -95,10 +102,14 @@ public class FileReferenceCompletionImpl extends FileReferenceCompletion {
}
variants[i] = item;
}
for (int i = 0; i < additionalItems.size(); i++) {
variants[i + candidates.length] = additionalItems.get(i);
}
if (!reference.getFileReferenceSet().isUrlEncoded()) {
return variants;
}
List<Object> encodedVariants = new ArrayList<Object>(variants.length);
List<Object> encodedVariants = new ArrayList<Object>(variants.length + additionalItems.size());
for (int i = 0; i < candidates.length; i++) {
final PsiElement element = candidates[i];
if (element instanceof PsiNamedElement) {
@@ -116,6 +127,7 @@ public class FileReferenceCompletionImpl extends FileReferenceCompletion {
}
}
}
encodedVariants.addAll(additionalItems);
return ArrayUtil.toObjectArray(encodedVariants);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -31,6 +31,9 @@ import com.intellij.psi.xml.XmlElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
/**
* @author maxim
*/
@@ -91,4 +94,9 @@ public class HtmlFileImpl extends XmlFileImpl implements FileReferenceResolver {
return getManager().findFile(childFile);
}
}
@Override
public Collection<Object> getVariants(@NotNull FileReference reference) {
return Collections.emptyList();
}
}