mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
fix inconsistency when adding a package statement into file with broken package
This commit is contained in:
@@ -120,6 +120,7 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
|
||||
}
|
||||
}
|
||||
else if (!packageName.isEmpty()) {
|
||||
cleanupBrokenPackageKeyword();
|
||||
PsiElement anchor = getFirstChild();
|
||||
if (PsiPackage.PACKAGE_INFO_FILE.equals(getName())) {
|
||||
// If javadoc is already present in a package-info.java file, position a new package statement after it,
|
||||
@@ -132,6 +133,17 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupBrokenPackageKeyword() {
|
||||
PsiElement child = getFirstChild();
|
||||
while (child instanceof PsiWhiteSpace || child instanceof PsiComment || child instanceof PsiErrorElement) {
|
||||
if (child instanceof PsiErrorElement && child.getFirstChild() != null && child.getFirstChild().textMatches(PsiKeyword.PACKAGE)) {
|
||||
child.delete();
|
||||
break;
|
||||
}
|
||||
child = child.getNextSibling();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiImportList getImportList() {
|
||||
StubElement<?> stub = getGreenStub();
|
||||
|
||||
@@ -5,6 +5,7 @@ package com.intellij.java.psi
|
||||
|
||||
import com.intellij.openapi.command.WriteCommandAction
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import com.intellij.testFramework.PsiTestUtil
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
||||
import com.intellij.util.ThrowableRunnable
|
||||
import groovy.transform.CompileStatic
|
||||
@@ -69,6 +70,13 @@ class JavaPsiTest extends LightCodeInsightFixtureTestCase {
|
||||
assert expr.text == "expr"
|
||||
}
|
||||
|
||||
void "test add package statement into file with broken package"() {
|
||||
def file = configureFile("package ;")
|
||||
runCommand { file.setPackageName('foo') }
|
||||
PsiTestUtil.checkFileStructure(file)
|
||||
assert myFixture.editor.document.text.startsWith('package foo;')
|
||||
}
|
||||
|
||||
private PsiJavaFile configureFile(String text) {
|
||||
myFixture.configureByText("a.java", text) as PsiJavaFile
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user