Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,16 @@ private void ColorType(@NotNull PsiElement ident, @NotNull AnnotationHolder hold
String type = ident.getText();
TextRange range = ident.getTextRange();
boolean bare = false;
TLSchemaLcIdentNs combinator = TLSchemaSearchUtils.findCombinator(ident.getProject(), type);
TLSchemaLcIdentNs combinator = TLSchemaSearchUtils.findCombinator(
ident.getProject(),
ident.getContainingFile(),
type
);
if (combinator != null) {
bare = true;
type = combinator.getDeclaration().getResultType().getBoxedTypeIdent().getText();
}
List<TLSchemaResultType> cons = TLSchemaSearchUtils.findType(ident.getProject(), type);
List<TLSchemaResultType> cons = TLSchemaSearchUtils.findType(ident.getProject(), ident.getContainingFile(), type);
boolean is_simple_type = cons.size() == 1;
if (is_simple_type) {
TLSchemaResultType resultType = cons.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElementResolveResult;
import com.intellij.psi.PsiFile;
import com.intellij.psi.ResolveResult;
import com.vk.tlschema.TLSchemaIcons;
import com.vk.tlschema.psi.TLSchemaLcIdentNs;
Expand All @@ -30,7 +31,8 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
return resultsArray;
}
Project project = myElement.getProject();
List<TLSchemaLcIdentNs> idents = TLSchemaSearchUtils.findLcIdents(project, myName);
PsiFile file = myElement.getContainingFile();
List<TLSchemaLcIdentNs> idents = TLSchemaSearchUtils.findLcIdents(project, file, myName);
List<ResolveResult> results = new ArrayList<>();
for (TLSchemaLcIdentNs ident : idents) {
results.add(new PsiElementResolveResult(ident));
Expand All @@ -42,7 +44,8 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
@Override
public Object[] getVariants() {
Project project = myElement.getProject();
List<TLSchemaLcIdentNs> idents = TLSchemaSearchUtils.findLcIdents(project);
PsiFile file = myElement.getContainingFile();
List<TLSchemaLcIdentNs> idents = TLSchemaSearchUtils.findLcIdents(project, file);
List<Object> variants = new ArrayList<>(Arrays.asList(varIdentReference.getVariants()));
for (final TLSchemaLcIdentNs ident : idents) {
variants.add(LookupElementBuilder.create(ident).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElementResolveResult;
import com.intellij.psi.PsiFile;
import com.intellij.psi.ResolveResult;
import com.vk.tlschema.TLSchemaIcons;
import com.vk.tlschema.psi.TLSchemaUcIdentNs;
Expand All @@ -30,7 +31,8 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
return resultsArray;
}
Project project = myElement.getProject();
List<TLSchemaUcIdentNs> idents = TLSchemaSearchUtils.findUcIdents(project, myName);
PsiFile file = myElement.getContainingFile();
List<TLSchemaUcIdentNs> idents = TLSchemaSearchUtils.findUcIdents(project, file, myName);
List<ResolveResult> results = new ArrayList<>();
for (TLSchemaUcIdentNs ident : idents) {
results.add(new PsiElementResolveResult(ident));
Expand All @@ -42,7 +44,8 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
@Override
public Object[] getVariants() {
Project project = myElement.getProject();
List<TLSchemaUcIdentNs> idents = TLSchemaSearchUtils.findUcIdents(project);
PsiFile file = myElement.getContainingFile();
List<TLSchemaUcIdentNs> idents = TLSchemaSearchUtils.findUcIdents(project, file);
List<Object> variants = new ArrayList<>(Arrays.asList(varIdentReference.getVariants()));
for (final TLSchemaUcIdentNs ident : idents) {
variants.add(LookupElementBuilder.create(ident).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public class TLSchemaChooseByNameContributor implements ChooseByNameContributor
@Override
public String[] getNames(Project project, boolean includeNonProjectItems) {
List<String> result = new ArrayList<>();
result.addAll(TLSchemaSearchUtils.findUcIdents(project).stream().map(TLSchemaUcIdentNs::getName).collect(Collectors.toList()));
result.addAll(TLSchemaSearchUtils.findLcIdents(project).stream().map(TLSchemaLcIdentNs::getName).collect(Collectors.toList()));
result.addAll(TLSchemaSearchUtils.findVarIdents(project).stream().map(TLSchemaVarIdent::getName).collect(Collectors.toList()));
result.addAll(TLSchemaSearchUtils.findUcIdents(project, null).stream().map(TLSchemaUcIdentNs::getName).collect(Collectors.toList()));
result.addAll(TLSchemaSearchUtils.findLcIdents(project, null).stream().map(TLSchemaLcIdentNs::getName).collect(Collectors.toList()));
result.addAll(TLSchemaSearchUtils.findVarIdents(project, null).stream().map(TLSchemaVarIdent::getName).collect(Collectors.toList()));
return result.toArray(new String[result.size()]);
}

@NotNull
@Override
public NavigationItem[] getItemsByName(String name, String pattern, Project project, boolean includeNonProjectItems) {
List<NavigationItem> result = new ArrayList<>();
result.addAll(TLSchemaSearchUtils.findUcIdents(project, name));
result.addAll(TLSchemaSearchUtils.findLcIdents(project, name));
result.addAll(TLSchemaSearchUtils.findVarIdents(project, name));
result.addAll(TLSchemaSearchUtils.findUcIdents(project, null, name));
result.addAll(TLSchemaSearchUtils.findLcIdents(project, null, name));
result.addAll(TLSchemaSearchUtils.findVarIdents(project, null, name));
return result.toArray(new NavigationItem[result.size()]);
}
}
79 changes: 58 additions & 21 deletions src/main/java/com/vk/tlschema/search/TLSchemaSearchUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScope;
Expand All @@ -20,10 +21,9 @@
import java.util.stream.Collectors;

public class TLSchemaSearchUtils {
public static List<TLSchemaResultType> findType(Project project, String key) {
public static List<TLSchemaResultType> findType(Project project, PsiFile file, String key) {
List<TLSchemaResultType> result = null;
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project));
Collection<VirtualFile> virtualFiles = findFilesFromContext(project, file);
for (VirtualFile virtualFile : virtualFiles) {
TLSchemaFile simpleFile = (TLSchemaFile) PsiManager.getInstance(project).findFile(virtualFile);
if (simpleFile != null) {
Expand All @@ -46,9 +46,8 @@ public static List<TLSchemaResultType> findType(Project project, String key) {
return result != null ? result : Collections.<TLSchemaResultType>emptyList();
}

public static @Nullable TLSchemaLcIdentNs findCombinator(Project project, String key) {
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project));
public static @Nullable TLSchemaLcIdentNs findCombinator(Project project, PsiFile file, String key) {
Collection<VirtualFile> virtualFiles = findFilesFromContext(project, file);
for (VirtualFile virtualFile : virtualFiles) {
TLSchemaFile simpleFile = (TLSchemaFile) PsiManager.getInstance(project).findFile(virtualFile);
if (simpleFile != null) {
Expand All @@ -68,14 +67,14 @@ public static List<TLSchemaResultType> findType(Project project, String key) {
return null;
}

public static List<TLSchemaUcIdentNs> findUcIdents(Project project, @NotNull String name) {
return findUcIdents(project).stream()
public static List<TLSchemaUcIdentNs> findUcIdents(Project project, PsiFile file, @NotNull String name) {
return findUcIdents(project, file).stream()
.filter(ident -> name.equals(ident.getName()))
.collect(Collectors.toList());
}

public static List<TLSchemaLcIdentNs> findLcIdents(Project project, @NotNull String name) {
return findLcIdents(project).stream()
public static List<TLSchemaLcIdentNs> findLcIdents(Project project, PsiFile file, @NotNull String name) {
return findLcIdents(project, file).stream()
.filter(ident -> name.equals(ident.getName()))
.collect(Collectors.toList());
}
Expand All @@ -86,13 +85,13 @@ public static List<TLSchemaVarIdent> findVarIdents(TLSchemaDeclaration declarati
.collect(Collectors.toList());
}

public static List<TLSchemaVarIdent> findVarIdents(Project project, String name) {
return findVarIdents(project).stream()
public static List<TLSchemaVarIdent> findVarIdents(Project project, PsiFile file, String name) {
return findVarIdents(project, file).stream()
.filter(ident -> name.equals(ident.getName()))
.collect(Collectors.toList());
}

public static List<TLSchemaUcIdentNs> findUcIdents(Project project) {
public static List<TLSchemaUcIdentNs> findUcIdents(Project project, PsiFile file) {
final List<TLSchemaUcIdentNs> result = new ArrayList<>();
TLSchemaRecursiveVisitor tlSchemaVisitor = new TLSchemaRecursiveVisitor() {
@Override
Expand All @@ -115,8 +114,7 @@ public void visitResultType(@NotNull TLSchemaResultType o) {
result.add(o.getBoxedTypeIdent().getUcIdentNs());
}
};
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project));
Collection<VirtualFile> virtualFiles = findFilesFromContext(project, file);
for (VirtualFile virtualFile : virtualFiles) {
TLSchemaFile tlschemaFile = (TLSchemaFile) PsiManager.getInstance(project).findFile(virtualFile);
if (tlschemaFile != null) {
Expand All @@ -126,7 +124,7 @@ public void visitResultType(@NotNull TLSchemaResultType o) {
return result;
}

public static List<TLSchemaLcIdentNs> findLcIdents(Project project) {
public static List<TLSchemaLcIdentNs> findLcIdents(Project project, PsiFile file) {
final List<TLSchemaLcIdentNs> result = new ArrayList<>();
TLSchemaRecursiveVisitor tlSchemaVisitor = new TLSchemaRecursiveVisitor() {
@Override
Expand All @@ -146,8 +144,7 @@ public void visitFullCombinatorId(@NotNull TLSchemaFullCombinatorId o) {
}
}
};
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project));
Collection<VirtualFile> virtualFiles = findFilesFromContext(project, file);
for (VirtualFile virtualFile : virtualFiles) {
TLSchemaFile tlschemaFile = (TLSchemaFile) PsiManager.getInstance(project).findFile(virtualFile);
if (tlschemaFile != null) {
Expand Down Expand Up @@ -179,7 +176,7 @@ public void visitVarIdent(@NotNull TLSchemaVarIdent o) {
return result;
}

public static List<TLSchemaVarIdent> findVarIdents(Project project) {
public static List<TLSchemaVarIdent> findVarIdents(Project project, PsiFile file) {
final List<TLSchemaVarIdent> result = new ArrayList<>();
TLSchemaRecursiveVisitor tlSchemaVisitor = new TLSchemaRecursiveVisitor() {
@Override
Expand All @@ -192,8 +189,7 @@ public void visitVarIdent(@NotNull TLSchemaVarIdent o) {
result.add(o);
}
};
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project));
Collection<VirtualFile> virtualFiles = findFilesFromContext(project, file);
for (VirtualFile virtualFile : virtualFiles) {
TLSchemaFile tlschemaFile = (TLSchemaFile) PsiManager.getInstance(project).findFile(virtualFile);
if (tlschemaFile != null) {
Expand All @@ -203,4 +199,45 @@ public void visitVarIdent(@NotNull TLSchemaVarIdent o) {
return result;
}

private static Collection<VirtualFile> findFilesFromContext(@NotNull Project project, @Nullable PsiFile file) {
if (file == null) {
return FileBasedIndex.getInstance().getContainingFiles(
FileTypeIndex.NAME,
TLSchemaFileType.INSTANCE,
GlobalSearchScope.allScope(project)
);
}

if (!(file instanceof TLSchemaFile)) {
return List.of();
}

if (isCombinedFile(file.getName())) {
return List.of(file.getVirtualFile());
}

return findFilesInProject(project);
}

private static Collection<VirtualFile> findFilesInProject(@NotNull Project project) {
List<VirtualFile> result = new ArrayList<>();
FileTypeIndex.processFiles(
TLSchemaFileType.INSTANCE,
(file) -> {
if (isCombinedFile(file.getName())) {
return true;
}

result.add(file);
return true;
},
GlobalSearchScope.allScope(project)
);

return result;
}

private static boolean isCombinedFile(@NotNull String fileName) {
return fileName.startsWith("combined");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TLSchemaStructureViewElementType implements StructureViewTreeElemen

public TLSchemaStructureViewElementType(Project project, String type) {
this.type = type;
List<TLSchemaResultType> decls = TLSchemaSearchUtils.findType(project, type);
List<TLSchemaResultType> decls = TLSchemaSearchUtils.findType(project, null, type);
List<TLSchemaLcIdentNs> combinators = new ArrayList<>();
for (TLSchemaResultType decl : decls) {
if (decl.getDeclaration().getCombinator() != null) {
Expand Down