[java-completion] IDEA-354763 Don't propose protected/transient and so on the top file level

GitOrigin-RevId: b8bdcc7329a0a26ef6260c56097c5994ee4e94b6
This commit is contained in:
Mikhail Pyltsin
2024-06-21 14:54:38 +02:00
committed by intellij-monorepo-bot
parent f69745925e
commit 7fe1e933e6
7 changed files with 75 additions and 8 deletions

View File

@@ -37,6 +37,19 @@ public final class ModifierChooser {
{PsiKeyword.TRANSIENT}
};
private static final String[][] FILE_MEMBER_MODIFIERS = {
{PsiKeyword.PUBLIC},
{PsiKeyword.FINAL, PsiKeyword.ABSTRACT},
{PsiKeyword.STRICTFP},
};
private static final String[][] FILE_MEMBER_MODIFIERS_WITH_SEALED = {
{PsiKeyword.PUBLIC},
{PsiKeyword.FINAL, PsiKeyword.ABSTRACT},
{PsiKeyword.SEALED, PsiKeyword.NON_SEALED},
{PsiKeyword.STRICTFP},
};
private static final String[][] CLASS_MEMBER_MODIFIERS_WITH_SEALED = {
{PsiKeyword.PUBLIC, PsiKeyword.PROTECTED, PsiKeyword.PRIVATE},
{PsiKeyword.STATIC},
@@ -82,7 +95,7 @@ public final class ModifierChooser {
PsiElement scope = position.getParent();
while (scope != null) {
if (scope instanceof PsiJavaFile) {
return addMemberModifiers(list, false, position);
return addJavaFileMemberModifiers(list, position);
}
if (scope instanceof PsiClass) {
return addMemberModifiers(list, ((PsiClass)scope).isInterface(), scope);
@@ -131,6 +144,16 @@ public final class ModifierChooser {
}
}
public static String[] addJavaFileMemberModifiers(@Nullable PsiModifierList list, @NotNull PsiElement position) {
if (PsiUtil.isAvailable(JavaFeature.IMPLICIT_CLASSES, position) &&
(position.getContainingFile() instanceof PsiJavaFile javaFile && javaFile.getPackageStatement() == null)) {
return addMemberModifiers(list, false, position);
}
return addKeywords(list,
PsiUtil.isAvailable(JavaFeature.SEALED_CLASSES, position) ?
FILE_MEMBER_MODIFIERS_WITH_SEALED : FILE_MEMBER_MODIFIERS);
}
public static String[] addMemberModifiers(PsiModifierList list, final boolean inInterface, @NotNull PsiElement position) {
return addKeywords(list, inInterface ? getInterfaceMemberModifiers(position) : getClassMemberModifiers(position));
}

View File

@@ -0,0 +1,3 @@
package myPackage;
<caret>

View File

@@ -0,0 +1,18 @@
/*
* Copyright 2000-2012 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 myPackage;
<caret>

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.completion;
import com.intellij.JavaTestUtil;
@@ -7,6 +7,7 @@ import com.intellij.codeInsight.completion.LightCompletionTestCase;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.pom.java.JavaFeature;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiMethod;
import com.intellij.testFramework.NeedsIndex;
@@ -31,17 +32,38 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
return JavaTestUtil.getJavaTestDataPath();
}
public void testFileScope1() {
doTest(8, "package", "public", "import", "final", "class", "interface", "abstract", "enum");
public void testFileScopeWithoutPackage() {
setLanguageLevel(JavaFeature.IMPLICIT_CLASSES.getMinimumLevel());
doTest(16, "package", "public", "import", "final", "class", "interface", "abstract", "enum",
"transient", "static", "private", "protected", "volatile", "synchronized", "sealed", "non-sealed");
assertNotContainItems("default");
}
public void testFileScopeAfterComment() { doTest(5, "package", "class", "import", "public", "private"); }
public void testFileScopeAfterJavaDoc() { doTest(5, "package", "class", "import", "public", "private"); }
public void testFileScopeWithoutPackage2() {
setLanguageLevel(LanguageLevel.JDK_1_8);
doTest(8, "package", "public", "import", "final", "class", "interface", "abstract", "enum");
assertNotContainItems("default", "transient", "static", "private", "protected", "volatile", "synchronized", "sealed", "non-sealed");
}
public void testFileScopeAfterComment() { doTest(4, "package", "class", "import", "public"); }
public void testFileScopeAfterJavaDoc() { doTest(4, "package", "class", "import", "public"); }
public void testFileScopeAfterJavaDocInsideModifierList() { doTest(2, "class", "public"); }
public void testFileScope2() { doTestContains("public", "private", "protected", "import", "final", "class", "interface", "abstract", "enum", "record", "sealed", "non-sealed"); }
public void testFileScopeWithPackage() {
setLanguageLevel(LanguageLevel.JDK_1_8);
doTest(7, "public", "import", "final", "class", "interface", "abstract", "enum", "record");
assertNotContainItems("default", "transient", "static", "private", "protected", "volatile", "synchronized", "sealed", "non-sealed");
}
public void testFileScopeWithPackage2() {
setLanguageLevel(JavaFeature.IMPLICIT_CLASSES.getMinimumLevel());
doTest(10, "public", "import", "final", "class", "interface", "abstract", "enum", "record", "sealed", "non-sealed");
assertNotContainItems("default", "transient", "static", "private", "protected", "volatile", "synchronized");
}
public void testClassScope1() { doTestContains("final", "class", "interface", "abstract", "enum", "record", "sealed", "non-sealed"); }
public void testClassScope2() { doTestContains("public", "private", "protected", "class", "interface", "enum", "record", "sealed", "non-sealed"); }
public void testClassScope2() {
setLanguageLevel(JavaFeature.SEALED_CLASSES.getMinimumLevel());
doTestContains("public", "class", "interface", "enum", "record", "sealed", "non-sealed");
assertNotContainItems("default", "transient", "static", "private", "protected", "volatile", "synchronized");
}
public void testClassScope3() { doTest(0, CLASS_SCOPE_KEYWORDS); }
public void testClassScope4() { doTest(11, CLASS_SCOPE_KEYWORDS_2); }
public void testInnerClassScope() {