Do not expand imports after optimizing
This commit is contained in:
@@ -67,23 +67,7 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Remove imports
|
||||
List<JetImportDirective> imports = jetFile.getImportDirectives();
|
||||
if (!imports.isEmpty()) {
|
||||
PsiElement firstForDelete = getWithPreviousWhitespaces(imports.get(0));
|
||||
PsiElement lastForDelete = getWithFollowedWhitespaces(imports.get(imports.size() - 1));
|
||||
|
||||
// Should be found before deletion
|
||||
PsiElement elementBeforeImports = firstForDelete.getPrevSibling();
|
||||
|
||||
jetFile.deleteChildRange(firstForDelete, lastForDelete);
|
||||
|
||||
if (elementBeforeImports != null) {
|
||||
jetFile.addAfter(JetPsiFactory.createNewLine(jetFile.getProject()), elementBeforeImports);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert back only necessary imports in correct order
|
||||
// Remove only unnecessary imports
|
||||
for (JetImportDirective anImport : directives) {
|
||||
directivesAfterCurrent.remove(anImport);
|
||||
|
||||
@@ -95,9 +79,11 @@ public class JetImportOptimizer implements ImportOptimizer {
|
||||
if (isUseful(importPath, usedQualifiedNames) &&
|
||||
doNeedImport(importPath, jetFile, directivesBeforeCurrent) &&
|
||||
doNeedImport(importPath, jetFile, directivesAfterCurrent)) {
|
||||
ImportInsertHelper.writeImportToFile(importPath, jetFile);
|
||||
directivesBeforeCurrent.add(anImport);
|
||||
}
|
||||
else {
|
||||
anImport.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,7 +3,6 @@ Comment 1
|
||||
*/
|
||||
package sometest
|
||||
|
||||
|
||||
import java.io as JavaIO
|
||||
import java.text.Annotation as TextAnnotation
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -1 +1 @@
|
||||
import java.util.HashSet
|
||||
import java.util.HashSet
|
||||
@@ -0,0 +1,12 @@
|
||||
package sometest
|
||||
|
||||
import java.util.HashSet
|
||||
import java.util.HashMap
|
||||
import java.util.ArrayList
|
||||
|
||||
val some: HashSet<Int>? = null
|
||||
val some2: HashMap<Int, Int>? = null
|
||||
|
||||
// SET_TRUE: setCollapseImports
|
||||
// REGION BEFORE: 25:94
|
||||
// REGION AFTER: 25:67
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.folding;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.intellij.codeInsight.folding.JavaCodeFoldingSettings;
|
||||
import com.intellij.codeInsight.folding.impl.JavaCodeFoldingSettingsImpl;
|
||||
import com.intellij.openapi.editor.FoldRegion;
|
||||
@@ -30,6 +31,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
|
||||
import org.jetbrains.jet.testing.SettingsConfigurator;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -48,37 +50,50 @@ public abstract class AbstractKotlinFoldingTest extends LightCodeInsightFixtureT
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String directText = fileText.replaceAll("~true~", "true").replaceAll("~false~", "false");
|
||||
directText += "\n\n// Generated from: " + path;
|
||||
|
||||
Function<String, Void> doExpandSettingsTestFunction = new Function<String, Void>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public Void apply(@Nullable String fileText) {
|
||||
doExpandSettingsTest(fileText);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
doTestWithSettings(directText, doExpandSettingsTestFunction);
|
||||
|
||||
// Clean all regions in model to force IDEA treat all regions as new ones
|
||||
cleanAllFoldedRegions();
|
||||
|
||||
String invertedText = fileText
|
||||
.replaceAll("~false~", "true").replaceAll("~true~", "false")
|
||||
.replaceAll(SettingsConfigurator.SET_TRUE_DIRECTIVE, "~TEMP_TRUE_DIRECTIVE~")
|
||||
.replaceAll(SettingsConfigurator.SET_FALSE_DIRECTIVE, SettingsConfigurator.SET_TRUE_DIRECTIVE)
|
||||
.replaceAll("~TEMP_TRUE_DIRECTIVE~", SettingsConfigurator.SET_FALSE_DIRECTIVE);
|
||||
invertedText += "\n\n// Generated from: " + path + " with !INVERTED! settings";
|
||||
|
||||
doTestWithSettings(invertedText, doExpandSettingsTestFunction);
|
||||
}
|
||||
|
||||
protected static void doTestWithSettings(@NotNull String fileText, @NotNull Function<String, Void> runnable) {
|
||||
JavaCodeFoldingSettings settings = JavaCodeFoldingSettings.getInstance();
|
||||
JavaCodeFoldingSettingsImpl restoreSettings = new JavaCodeFoldingSettingsImpl();
|
||||
restoreSettings.loadState((JavaCodeFoldingSettingsImpl) settings);
|
||||
|
||||
try {
|
||||
String directText = fileText.replaceAll("~true~", "true").replaceAll("~false~", "false");
|
||||
directText += "\n\n// Generated from: " + path;
|
||||
SettingsConfigurator configurator = new SettingsConfigurator(fileText, settings);
|
||||
configurator.configureSettings();
|
||||
|
||||
doExpandSettingsTest(directText, settings);
|
||||
|
||||
// Clean all regions in model to force IDEA treat all regions as new ones
|
||||
cleanAllFoldedRegions();
|
||||
|
||||
String invertedText = fileText
|
||||
.replaceAll("~false~", "true").replaceAll("~true~", "false")
|
||||
.replaceAll(SettingsConfigurator.SET_TRUE_DIRECTIVE, "~TEMP_TRUE_DIRECTIVE~")
|
||||
.replaceAll(SettingsConfigurator.SET_FALSE_DIRECTIVE, SettingsConfigurator.SET_TRUE_DIRECTIVE)
|
||||
.replaceAll("~TEMP_TRUE_DIRECTIVE~", SettingsConfigurator.SET_FALSE_DIRECTIVE);
|
||||
invertedText += "\n\n// Generated from: " + path + " with !INVERTED! settings";
|
||||
|
||||
doExpandSettingsTest(invertedText, settings);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
runnable.apply(fileText);
|
||||
}
|
||||
finally {
|
||||
((JavaCodeFoldingSettingsImpl) JavaCodeFoldingSettings.getInstance()).loadState(restoreSettings);
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanAllFoldedRegions() {
|
||||
protected void cleanAllFoldedRegions() {
|
||||
final FoldingModelEx model = (FoldingModelEx) myFixture.getEditor().getFoldingModel();
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
@@ -91,12 +106,14 @@ public abstract class AbstractKotlinFoldingTest extends LightCodeInsightFixtureT
|
||||
model.runBatchFoldingOperation(runnable);
|
||||
}
|
||||
|
||||
private void doExpandSettingsTest(String fileText, JavaCodeFoldingSettings settings) throws IOException {
|
||||
SettingsConfigurator configurator = new SettingsConfigurator(fileText, settings);
|
||||
configurator.configureSettings();
|
||||
|
||||
VirtualFile tempFile = PlatformTestCase.createTempFile("kt", null, fileText, Charset.defaultCharset());
|
||||
myFixture.testFoldingWithCollapseStatus(tempFile.getPath());
|
||||
private void doExpandSettingsTest(String fileText) {
|
||||
try {
|
||||
VirtualFile tempFile = PlatformTestCase.createTempFile("kt", null, fileText, Charset.defaultCharset());
|
||||
myFixture.testFoldingWithCollapseStatus(tempFile.getPath());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.folding
|
||||
|
||||
import com.intellij.codeInsight.folding.CodeFoldingManager
|
||||
import com.intellij.openapi.command.CommandProcessor
|
||||
import com.intellij.openapi.command.UndoConfirmationPolicy
|
||||
import com.intellij.openapi.editor.FoldRegion
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase
|
||||
import org.jetbrains.jet.plugin.editor.importOptimizer.JetImportOptimizer
|
||||
import java.io.File
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
|
||||
import org.jetbrains.jet.InTextDirectivesUtils
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.plugin.folding.AbstractKotlinFoldingTest.doTestWithSettings
|
||||
|
||||
class FoldingAfterOptimizeImportsTest : AbstractKotlinFoldingTest() {
|
||||
private val fixture: JavaCodeInsightTestFixture
|
||||
get() = myFixture!!
|
||||
|
||||
private val editor: Editor
|
||||
get() = fixture.getEditor()!!
|
||||
|
||||
private val fileText: String
|
||||
get() = fixture.getFile()!!.getText()!!
|
||||
|
||||
fun testFoldingAfterOptimizeImports() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
private fun doTest() {
|
||||
fixture.configureByFile(getTestName(true) + ".kt")
|
||||
|
||||
doTestWithSettings(fileText) {
|
||||
fileText ->
|
||||
CodeFoldingManager.getInstance(fixture.getProject())!!.buildInitialFoldings(editor)
|
||||
getFoldingRegion(0).checkRegion(false, findStringWithPrefixes("// REGION BEFORE: "))
|
||||
|
||||
CommandProcessor.getInstance()?.executeCommand(fixture.getProject(),
|
||||
JetImportOptimizer().processFile(fixture.getFile()),
|
||||
"Optimize Imports", null,
|
||||
UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION)
|
||||
getFoldingRegion(0).checkRegion(false, findStringWithPrefixes("// REGION AFTER: "))
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun getFoldingRegion(number: Int): FoldRegion {
|
||||
fixture.doHighlighting()
|
||||
val model = editor.getFoldingModel()
|
||||
val foldingRegions = model.getAllFoldRegions()
|
||||
assert(foldingRegions.size >= number) { "There is no enough folding regions in file: in file - ${foldingRegions.size} , expected = ${number}" }
|
||||
return foldingRegions[number]
|
||||
}
|
||||
|
||||
override fun getTestDataPath() = File(PluginTestCaseBase.getTestDataPathBase(), "/folding/afterOptimizeImports/").getPath() + File.separator
|
||||
|
||||
private fun findStringWithPrefixes(prefix: String) = InTextDirectivesUtils.findStringWithPrefixes(fileText, prefix)
|
||||
?: throw AssertionError("Couldn't find line with prefix $prefix")
|
||||
|
||||
private fun FoldRegion.getPosition() = "${getStartOffset()}:${getEndOffset()}"
|
||||
|
||||
private fun FoldRegion.checkRegion(isExpanded: Boolean, position: String): Unit {
|
||||
assert(isValid()) { "Region should be valid: $this" }
|
||||
assert(isExpanded == isExpanded()) { "isExpanded should be $isExpanded. Actual = ${isExpanded()}" }
|
||||
assert(position == getPosition()) { "Region position is wrong: expected = $position, actual = ${getPosition()}" }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user