Converted to Kotlin (step 1)

This commit is contained in:
Valentin Kipyatkov
2015-01-21 20:56:45 +03:00
parent ca2a46e42f
commit 98be4566d2
2 changed files with 81 additions and 92 deletions
@@ -14,32 +14,32 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.quickfix; package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.ServiceManager
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetFile; import org.jetbrains.kotlin.psi.JetImportDirective
import org.jetbrains.kotlin.psi.JetImportDirective; import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.ImportPath;
import java.util.List;
public abstract class ImportInsertHelper { public abstract class ImportInsertHelper {
public static ImportInsertHelper getInstance() {
return ServiceManager.getService(ImportInsertHelper.class); public abstract fun addImportDirectiveIfNeeded(importFqn: FqName, file: JetFile)
public abstract fun optimizeImportsOnTheFly(file: JetFile): Boolean
public abstract fun isImportedWithDefault(importPath: ImportPath, contextFile: JetFile): Boolean
public abstract fun needImport(fqName: FqName, file: JetFile): Boolean
public abstract fun needImport(importPath: ImportPath, file: JetFile): Boolean
public abstract fun needImport(importPath: ImportPath, file: JetFile, importDirectives: List<JetImportDirective>): Boolean
public abstract fun writeImportToFile(importPath: ImportPath, file: JetFile)
class object {
public fun getInstance(): ImportInsertHelper {
return ServiceManager.getService<ImportInsertHelper>(javaClass<ImportInsertHelper>())
}
} }
public abstract void addImportDirectiveIfNeeded(@NotNull FqName importFqn, @NotNull JetFile file);
public abstract boolean optimizeImportsOnTheFly(JetFile file);
public abstract boolean isImportedWithDefault(@NotNull ImportPath importPath, @NotNull JetFile contextFile);
public abstract boolean needImport(@NotNull FqName fqName, @NotNull JetFile file);
public abstract boolean needImport(@NotNull ImportPath importPath, @NotNull JetFile file);
public abstract boolean needImport(@NotNull ImportPath importPath, @NotNull JetFile file, List<JetImportDirective> importDirectives);
public abstract void writeImportToFile(@NotNull ImportPath importPath, @NotNull JetFile file);
} }
@@ -14,136 +14,125 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.quickfix; package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.codeInsight.actions.OptimizeImportsProcessor; import com.intellij.codeInsight.actions.OptimizeImportsProcessor
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil; import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS; import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.*
import org.jetbrains.kotlin.name.NamePackage; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.resolve.ImportPath; import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM
import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM;
import java.util.List; public class ImportInsertHelperImpl : ImportInsertHelper() {
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
public class ImportInsertHelperImpl extends ImportInsertHelper {
/** /**
* Add import directive into the PSI tree for the given package. * Add import directive into the PSI tree for the given package.
* *
* @param importFqn full name of the import * @param importFqn full name of the import
* @param file File where directive should be added. * @param file File where directive should be added.
*/ */
@Override override fun addImportDirectiveIfNeeded(importFqn: FqName, file: JetFile) {
public void addImportDirectiveIfNeeded(@NotNull FqName importFqn, @NotNull JetFile file) { val importPath = ImportPath(importFqn, false)
ImportPath importPath = new ImportPath(importFqn, false);
optimizeImportsOnTheFly(file); optimizeImportsOnTheFly(file)
if (needImport(importPath, file)) { if (needImport(importPath, file)) {
writeImportToFile(importPath, file); writeImportToFile(importPath, file)
} }
} }
@Override override fun optimizeImportsOnTheFly(file: JetFile): Boolean {
public boolean optimizeImportsOnTheFly(JetFile file) {
if (CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) { if (CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) {
new OptimizeImportsProcessor(file.getProject(), file).runWithoutProgress(); OptimizeImportsProcessor(file.getProject(), file).runWithoutProgress()
return true; return true
} }
else { else {
return false; return false
} }
} }
@Override override fun writeImportToFile(importPath: ImportPath, file: JetFile) {
public void writeImportToFile(@NotNull ImportPath importPath, @NotNull JetFile file) { val psiFactory = JetPsiFactory(file.getProject())
JetPsiFactory psiFactory = JetPsiFactory(file.getProject()); if (file is JetCodeFragment) {
if (file instanceof JetCodeFragment) { val newDirective = psiFactory.createImportDirective(importPath)
JetImportDirective newDirective = psiFactory.createImportDirective(importPath); (file as JetCodeFragment).addImportsFromString(newDirective.getText())
((JetCodeFragment) file).addImportsFromString(newDirective.getText()); return
return;
} }
JetImportList importList = file.getImportList(); val importList = file.getImportList()
if (importList != null) { if (importList != null) {
JetImportDirective newDirective = psiFactory.createImportDirective(importPath); val newDirective = psiFactory.createImportDirective(importPath)
importList.add(psiFactory.createNewLine()); importList.add(psiFactory.createNewLine())
importList.add(newDirective); importList.add(newDirective)
} }
else { else {
JetImportList newDirective = psiFactory.createImportDirectiveWithImportList(importPath); val newDirective = psiFactory.createImportDirectiveWithImportList(importPath)
JetPackageDirective packageDirective = file.getPackageDirective(); val packageDirective = file.getPackageDirective()
if (packageDirective == null) { if (packageDirective == null) {
throw new IllegalStateException("Scripts are not supported: " + file.getName()); throw IllegalStateException("Scripts are not supported: " + file.getName())
} }
packageDirective.getParent().addAfter(newDirective, packageDirective); packageDirective.getParent().addAfter(newDirective, packageDirective)
} }
} }
/** /**
* Check that import is useless. * Check that import is useless.
*/ */
private boolean isImportedByDefault(@NotNull ImportPath importPath, @NotNull JetFile jetFile) { private fun isImportedByDefault(importPath: ImportPath, jetFile: JetFile): Boolean {
if (importPath.fqnPart().isRoot()) { if (importPath.fqnPart().isRoot()) {
return true; return true
} }
if (!importPath.isAllUnder() && !importPath.hasAlias()) { if (!importPath.isAllUnder() && !importPath.hasAlias()) {
// Single element import without .* and alias is useless // Single element import without .* and alias is useless
if (NamePackage.isOneSegmentFQN(importPath.fqnPart())) { if (importPath.fqnPart().isOneSegmentFQN()) {
return true; return true
} }
// There's no need to import a declaration from the package of current file // There's no need to import a declaration from the package of current file
if (jetFile.getPackageFqName().equals(importPath.fqnPart().parent())) { if (jetFile.getPackageFqName() == importPath.fqnPart().parent()) {
return true; return true
} }
} }
return isImportedWithDefault(importPath, jetFile); return isImportedWithDefault(importPath, jetFile)
} }
@Override override fun isImportedWithDefault(importPath: ImportPath, contextFile: JetFile): Boolean {
public boolean isImportedWithDefault(@NotNull ImportPath importPath, @NotNull JetFile contextFile) {
List<ImportPath> defaultImports = ProjectStructureUtil.isJsKotlinModule(contextFile) val defaultImports = if (ProjectStructureUtil.isJsKotlinModule(contextFile))
? TopDownAnalyzerFacadeForJS.DEFAULT_IMPORTS TopDownAnalyzerFacadeForJS.DEFAULT_IMPORTS
: TopDownAnalyzerFacadeForJVM.DEFAULT_IMPORTS; else
return NamePackage.isImported(importPath, defaultImports); TopDownAnalyzerFacadeForJVM.DEFAULT_IMPORTS
return importPath.isImported(defaultImports)
} }
@Override override fun needImport(fqName: FqName, file: JetFile): Boolean {
public boolean needImport(@NotNull FqName fqName, @NotNull JetFile file) { return needImport(ImportPath(fqName, false), file)
return needImport(new ImportPath(fqName, false), file);
} }
@Override override fun needImport(importPath: ImportPath, file: JetFile): Boolean {
public boolean needImport(@NotNull ImportPath importPath, @NotNull JetFile file) { return needImport(importPath, file, file.getImportDirectives())
return needImport(importPath, file, file.getImportDirectives());
} }
@Override override fun needImport(importPath: ImportPath, file: JetFile, importDirectives: List<JetImportDirective>): Boolean {
public boolean needImport(@NotNull ImportPath importPath, @NotNull JetFile file, List<JetImportDirective> importDirectives) {
if (isImportedByDefault(importPath, file)) { if (isImportedByDefault(importPath, file)) {
return false; return false
} }
if (!importDirectives.isEmpty()) { if (!importDirectives.isEmpty()) {
// Check if import is already present // Check if import is already present
for (JetImportDirective directive : importDirectives) { for (directive in importDirectives) {
ImportPath existentImportPath = directive.getImportPath(); val existentImportPath = directive.getImportPath()
if (existentImportPath != null && NamePackage.isImported(importPath, existentImportPath)) { if (existentImportPath != null && importPath.isImported(existentImportPath)) {
return false; return false
} }
} }
} }
return true; return true
} }
} }