void (@NotNull PsiElement element, @NotNull PsiFile origFile, @NotNull PsiDirectory directory) { PsiFile file = element.getContainingFile(); PsiPackage myTargetPackage = JavaDirectoryService.getInstance().getPackage(directory); if (myTargetPackage == null) return; String myTargetPackageName = myTargetPackage.getQualifiedName(); SingleFileSourcesTracker singleFileSourcesTracker = SingleFileSourcesTracker.getInstance(file.getProject()); String singleFileSourcePackageName = singleFileSourcesTracker.getPackageNameForSingleFileSource(origFile.getVirtualFile()); if (singleFileSourcePackageName != null) myTargetPackageName = singleFileSourcePackageName; PsiPackageStatement statement = ((PsiJavaFile)file).getPackageStatement(); if (myTargetPackageName.isEmpty()) { if (statement != null) { statement.delete(); } } else { PsiElementFactory factory = JavaPsiFacade.getElementFactory(file.getProject()); final PsiPackageStatement packageStatement = factory.createPackageStatement(myTargetPackageName); if (statement != null) { statement.getPackageReference().replace(packageStatement.getPackageReference()); } else { file.addAfter(packageStatement, null); } } }