KT-1426 Implement "Optimize Imports" in plugin - a slightly better formatting
This commit is contained in:
@@ -83,7 +83,9 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
// Remove imports
|
||||
List<JetImportDirective> imports = jetFile.getImportDirectives();
|
||||
if (!imports.isEmpty()) {
|
||||
jetFile.deleteChildRange(imports.get(0), imports.get(imports.size() - 1));
|
||||
jetFile.deleteChildRange(
|
||||
getWithPreviousWhitespaces(imports.get(0)),
|
||||
getWithFollowedWhitespaces(imports.get(imports.size() - 1)));
|
||||
}
|
||||
|
||||
// Insert back only necessary imports in correct order
|
||||
@@ -201,4 +203,40 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PsiElement getWithPreviousWhitespaces(PsiElement element) {
|
||||
PsiElement result = element;
|
||||
|
||||
PsiElement siblingIterator = element.getPrevSibling();
|
||||
while (siblingIterator != null) {
|
||||
if (siblingIterator.getNode().getElementType() != TokenType.WHITE_SPACE) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
result = siblingIterator;
|
||||
}
|
||||
|
||||
siblingIterator = siblingIterator.getPrevSibling();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static PsiElement getWithFollowedWhitespaces(PsiElement element) {
|
||||
PsiElement result = element;
|
||||
|
||||
PsiElement siblingIterator = element.getNextSibling();
|
||||
while (siblingIterator != null) {
|
||||
if (siblingIterator.getNode().getElementType() != TokenType.WHITE_SPACE) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
result = siblingIterator;
|
||||
}
|
||||
|
||||
siblingIterator = siblingIterator.getNextSibling();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,12 +52,9 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
||||
return new SpacingBuilder(settings)
|
||||
// ============ Line breaks ==============
|
||||
.after(NAMESPACE_HEADER).blankLines(1)
|
||||
.before(IMPORT_DIRECTIVE).lineBreakInCode()
|
||||
|
||||
.between(IMPORT_DIRECTIVE, CLASS).blankLines(1)
|
||||
.between(IMPORT_DIRECTIVE, FUN).blankLines(1)
|
||||
.between(IMPORT_DIRECTIVE, PROPERTY).blankLines(1)
|
||||
.between(IMPORT_DIRECTIVE, OBJECT_DECLARATION).blankLines(1)
|
||||
.between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode()
|
||||
.after(IMPORT_DIRECTIVE).blankLines(1)
|
||||
|
||||
.before(FUN).lineBreakInCode()
|
||||
.before(PROPERTY).lineBreakInCode()
|
||||
|
||||
@@ -20,7 +20,10 @@ import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.DefaultModuleConfiguration;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetImportDirective;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.ImportPath;
|
||||
@@ -100,15 +103,7 @@ public class ImportInsertHelper {
|
||||
lastDirective.getParent().addAfter(newDirective, lastDirective);
|
||||
}
|
||||
else {
|
||||
List<JetDeclaration> declarations = file.getDeclarations();
|
||||
|
||||
if (!declarations.isEmpty()) {
|
||||
JetDeclaration firstDeclaration = declarations.iterator().next();
|
||||
firstDeclaration.getParent().addBefore(newDirective, firstDeclaration);
|
||||
}
|
||||
else {
|
||||
file.getNamespaceHeader().getParent().addAfter(newDirective, file.getNamespaceHeader());
|
||||
}
|
||||
file.getNamespaceHeader().getParent().addAfter(newDirective, file.getNamespaceHeader());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/**
|
||||
Comment 1
|
||||
*/
|
||||
package sometest
|
||||
|
||||
|
||||
@@ -6,6 +9,9 @@ import java.text.Annotation as TextAnnotation
|
||||
import java.util.ArrayList
|
||||
import java.util.HashSet
|
||||
|
||||
/**
|
||||
Comment 2
|
||||
*/
|
||||
class Action {
|
||||
fun test(hash : HashSet<Int>) {
|
||||
val some : TextAnnotation? = null
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
/**
|
||||
Comment 1
|
||||
*/
|
||||
package sometest
|
||||
|
||||
|
||||
import java.io as JavaIO
|
||||
import java.text.Annotation as TextAnnotation
|
||||
import java.util.ArrayList
|
||||
import java.util.HashSet
|
||||
|
||||
/**
|
||||
Comment 2
|
||||
*/
|
||||
class Action {
|
||||
fun test(hash : HashSet<Int>) {
|
||||
val some : TextAnnotation? = null
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sometest
|
||||
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.HashSet
|
||||
|
||||
|
||||
Reference in New Issue
Block a user