KT-1426 Implement "Optimize Imports" in plugin - tests and small fix for aliases.
This commit is contained in:
@@ -119,7 +119,6 @@ public class JetImportOptimizer implements ImportOptimizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Set<FqName> extractUsedQualifiedNames(JetFile jetFile) {
|
public static Set<FqName> extractUsedQualifiedNames(JetFile jetFile) {
|
||||||
|
|
||||||
final Set<FqName> usedQualifiedNames = new HashSet<FqName>();
|
final Set<FqName> usedQualifiedNames = new HashSet<FqName>();
|
||||||
jetFile.accept(new JetVisitorVoid() {
|
jetFile.accept(new JetVisitorVoid() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
|||||||
return new SpacingBuilder(settings)
|
return new SpacingBuilder(settings)
|
||||||
// ============ Line breaks ==============
|
// ============ Line breaks ==============
|
||||||
.after(NAMESPACE_HEADER).blankLines(1)
|
.after(NAMESPACE_HEADER).blankLines(1)
|
||||||
|
|
||||||
.before(IMPORT_DIRECTIVE).lineBreakInCode()
|
.before(IMPORT_DIRECTIVE).lineBreakInCode()
|
||||||
|
|
||||||
.between(IMPORT_DIRECTIVE, CLASS).blankLines(1)
|
.between(IMPORT_DIRECTIVE, CLASS).blankLines(1)
|
||||||
.between(IMPORT_DIRECTIVE, FUN).blankLines(1)
|
.between(IMPORT_DIRECTIVE, FUN).blankLines(1)
|
||||||
.between(IMPORT_DIRECTIVE, PROPERTY).blankLines(1)
|
.between(IMPORT_DIRECTIVE, PROPERTY).blankLines(1)
|
||||||
@@ -77,7 +77,7 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
|||||||
.around(TokenSet.create(EQEQ, EXCLEQ, EQEQEQ, EXCLEQEQEQ)).spaceIf(jetCommonSettings.SPACE_AROUND_EQUALITY_OPERATORS)
|
.around(TokenSet.create(EQEQ, EXCLEQ, EQEQEQ, EXCLEQEQEQ)).spaceIf(jetCommonSettings.SPACE_AROUND_EQUALITY_OPERATORS)
|
||||||
.aroundInside(TokenSet.create(LT, GT, LTEQ, GTEQ), BINARY_EXPRESSION).spaceIf(jetCommonSettings.SPACE_AROUND_RELATIONAL_OPERATORS)
|
.aroundInside(TokenSet.create(LT, GT, LTEQ, GTEQ), BINARY_EXPRESSION).spaceIf(jetCommonSettings.SPACE_AROUND_RELATIONAL_OPERATORS)
|
||||||
.around(TokenSet.create(PLUS, MINUS)).spaceIf(jetCommonSettings.SPACE_AROUND_ADDITIVE_OPERATORS)
|
.around(TokenSet.create(PLUS, MINUS)).spaceIf(jetCommonSettings.SPACE_AROUND_ADDITIVE_OPERATORS)
|
||||||
.around(TokenSet.create(MUL, DIV, PERC)).spaceIf(jetCommonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS)
|
.aroundInside(TokenSet.create(MUL, DIV, PERC), BINARY_EXPRESSION).spaceIf(jetCommonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS)
|
||||||
.around(TokenSet.create(PLUSPLUS, MINUSMINUS, MINUS, PLUS, EXCL)).spaceIf(jetCommonSettings.SPACE_AROUND_UNARY_OPERATOR)
|
.around(TokenSet.create(PLUSPLUS, MINUSMINUS, MINUS, PLUS, EXCL)).spaceIf(jetCommonSettings.SPACE_AROUND_UNARY_OPERATOR)
|
||||||
.around(RANGE).spaceIf(jetSettings.SPACE_AROUND_RANGE)
|
.around(RANGE).spaceIf(jetSettings.SPACE_AROUND_RANGE)
|
||||||
|
|
||||||
|
|||||||
@@ -89,10 +89,10 @@ public class ImportInsertHelper {
|
|||||||
// Check if import is already present
|
// Check if import is already present
|
||||||
for (JetImportDirective directive : importDirectives) {
|
for (JetImportDirective directive : importDirectives) {
|
||||||
ImportPath existentImportPath = JetPsiUtil.getImportPath(directive);
|
ImportPath existentImportPath = JetPsiUtil.getImportPath(directive);
|
||||||
if (existentImportPath != null &&
|
if (directive.getAliasName() == null && aliasName == null) {
|
||||||
directive.getAliasName() == null &&
|
if (existentImportPath != null && QualifiedNamesUtil.isImported(existentImportPath, importPath)) {
|
||||||
QualifiedNamesUtil.isImported(existentImportPath, importPath)) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package sometest
|
||||||
|
|
||||||
|
|
||||||
|
import java.io as JavaIO
|
||||||
|
import java.text.Annotation as TextAnnotation
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.HashSet
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash : HashSet<Int>) {
|
||||||
|
val some : TextAnnotation? = null
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
JavaIO.File(StringBuilder().append("Hello").toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package sometest
|
||||||
|
|
||||||
|
|
||||||
|
import java.io as JavaIO
|
||||||
|
import java.text.Annotation as TextAnnotation
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.HashSet
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash : HashSet<Int>) {
|
||||||
|
val some : TextAnnotation? = null
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
JavaIO.File(StringBuilder().append("Hello").toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import java.util.HashSet
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash: HashSet<Int>) {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash: HashSet<Int>) {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package sometest
|
||||||
|
|
||||||
|
import java.util.HashSet
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash: HashSet<Int>) {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package sometest
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.HashSet
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test(hash: HashSet<Int>) {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import com.sun.corba.se.impl.util.JDKBridge
|
||||||
|
import com.sun.org.apache.xpath.internal.operations.And
|
||||||
|
import java.lang.StringBuilder
|
||||||
|
import java.net.HttpRetryException
|
||||||
|
import java.util.ArrayList
|
||||||
|
import kotlin.util.measureTimeMillis
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test() {
|
||||||
|
measureTimeMillis({ println("Some")})
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import java.util.ArrayList
|
||||||
|
import kotlin.util.measureTimeMillis
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test() {
|
||||||
|
measureTimeMillis({ println("Some")})
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.ArrayList as SomeThing
|
||||||
|
import java.io as JavaIO
|
||||||
|
import java.io.*
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test() {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import java.io as JavaIO
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.ArrayList as SomeThing
|
||||||
|
|
||||||
|
class Action {
|
||||||
|
fun test() {
|
||||||
|
val test : ArrayList<Int>? = null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -146,22 +146,6 @@ public abstract class AbstractJetFormatterTest extends LightIdeaTestCase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void doMethodTest(@NonNls final String before, @NonNls final String after) throws Exception {
|
|
||||||
// doTextTest(
|
|
||||||
// Action.REFORMAT,
|
|
||||||
// "class Foo{\n" + " void foo() {\n" + before + '\n' + " }\n" + "}",
|
|
||||||
// "class Foo {\n" + " void foo() {\n" + StringUtil.shiftIndentInside(after, 8, false) + '\n' + " }\n" + "}"
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public void doClassTest(@NonNls final String before, @NonNls final String after) throws Exception {
|
|
||||||
// doTextTest(
|
|
||||||
// Action.REFORMAT,
|
|
||||||
// "class Foo{\n" + before + '\n' + "}",
|
|
||||||
// "class Foo {\n" + StringUtil.shiftIndentInside(after, 4, false) + '\n' + "}"
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
private static String prepareText(String actual) {
|
private static String prepareText(String actual) {
|
||||||
if (actual.startsWith("\n")) {
|
if (actual.startsWith("\n")) {
|
||||||
actual = actual.substring(1);
|
actual = actual.substring(1);
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-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 org.jetbrains.jet.plugin.importOptimizer;
|
||||||
|
|
||||||
|
import com.intellij.openapi.command.CommandProcessor;
|
||||||
|
import com.intellij.openapi.command.UndoConfirmationPolicy;
|
||||||
|
import com.intellij.openapi.projectRoots.JavaSdk;
|
||||||
|
import com.intellij.openapi.projectRoots.Sdk;
|
||||||
|
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||||
|
import org.apache.commons.lang.SystemUtils;
|
||||||
|
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||||
|
import org.jetbrains.jet.plugin.editor.importOptimizer.JetImportOptimizer;
|
||||||
|
import org.jetbrains.jet.testing.ConfigRuntimeUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nikolay Krasko
|
||||||
|
*/
|
||||||
|
public class OptimizeImportsTest extends LightCodeInsightTestCase {
|
||||||
|
|
||||||
|
public void testAlreadyOptimized() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRemoveImportsIfGeneral() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSortImports() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testUnusedImports() throws Exception {
|
||||||
|
doTestWithKotlinRuntime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWithAliases() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doTest() throws Exception {
|
||||||
|
configureByFile(fileName());
|
||||||
|
invokeFormatFile();
|
||||||
|
checkResultByFile(null, checkFileName(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doTestWithKotlinRuntime() {
|
||||||
|
try {
|
||||||
|
ConfigRuntimeUtil.configureKotlinRuntime(getModule(), getFullJavaJDK());
|
||||||
|
|
||||||
|
configureByFile(fileName());
|
||||||
|
invokeFormatFile();
|
||||||
|
|
||||||
|
checkResultByFile(null, checkFileName(), false);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ConfigRuntimeUtil.unConfigureKotlinRuntime(getModule(), getProjectJDK());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTestDataPath() {
|
||||||
|
return new File(PluginTestCaseBase.getTestDataPathBase(), "/editor/optimizeImports").getPath() +
|
||||||
|
File.separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Sdk getProjectJDK() {
|
||||||
|
return PluginTestCaseBase.jdkFromIdeaHome();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static Sdk getFullJavaJDK() {
|
||||||
|
return JavaSdk.getInstance().createJdk("JDK", SystemUtils.getJavaHome().getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String fileName() {
|
||||||
|
return getTestName(false) + ".kt";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String checkFileName() {
|
||||||
|
return getTestName(false) + "_after.kt";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void invokeFormatFile() {
|
||||||
|
CommandProcessor.getInstance().executeCommand(
|
||||||
|
getProject(), new JetImportOptimizer().processFile(getFile()),
|
||||||
|
"Optimize Imports", null, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user