[java, completion] do not add references on the top level of module. Add handling of new PSI of type elements on top level

GitOrigin-RevId: 0d8a89d044da022412573e01ae6700a38fc70e29
This commit is contained in:
Roman Ivanov
2023-08-27 21:24:21 +00:00
committed by intellij-monorepo-bot
parent c81004ac51
commit 412d0c53fa
2 changed files with 4 additions and 1 deletions
@@ -134,6 +134,8 @@ public final class JavaCompletionContributor extends CompletionContributor imple
private static final ElementPattern<PsiElement> IN_VARIABLE_TYPE = psiElement()
.withParents(PsiJavaCodeReferenceElement.class, PsiTypeElement.class, PsiDeclarationStatement.class)
.afterLeaf(psiElement().inside(psiAnnotation()));
private static final ElementPattern<PsiElement> TOP_LEVEL_VAR_IN_MODULE = psiElement().withSuperParent(3, PsiJavaFile.class)
.inVirtualFile(virtualFile().withName("module-info.java"));
/**
* @param position completion invocation position
@@ -740,6 +742,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
Set<? extends ExpectedTypeInfo> expectedTypes,
Condition<? super String> nameCondition) {
PsiElement position = parameters.getPosition();
if (TOP_LEVEL_VAR_IN_MODULE.accepts(position)) return Collections.emptyList();
ElementFilter filter = getReferenceFilter(position);
if (filter == null) return Collections.emptyList();
if (parameters.getInvocationCount() <= 1 && JavaClassNameCompletionContributor.AFTER_NEW.accepts(position)) {
@@ -1200,7 +1200,7 @@ public class JavaKeywordCompletion {
}
private void addModuleKeywords() {
PsiElement context = PsiTreeUtil.skipParentsOfType(myPosition.getParent(), PsiErrorElement.class);
PsiElement context = PsiTreeUtil.skipParentsOfType(myPosition.getParent(), PsiErrorElement.class, PsiJavaCodeReferenceElement.class, PsiTypeElement.class);
PsiElement prevElement = PsiTreeUtil.skipWhitespacesAndCommentsBackward(myPosition.getParent());
if (context instanceof PsiJavaFile && !(prevElement instanceof PsiJavaModule) || context instanceof PsiImportList) {