Add common JetElement for imports: JetImportList
This commit is contained in:
@@ -31,6 +31,7 @@ import org.jetbrains.jet.JetNodeTypes;
|
||||
import org.jetbrains.jet.kdoc.lexer.KDocTokens;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetImportDirective;
|
||||
import org.jetbrains.jet.lang.psi.JetImportList;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -46,14 +47,16 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware {
|
||||
List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
|
||||
JetFile file = (JetFile) root;
|
||||
|
||||
List<JetImportDirective> importList = file.getImportDirectives();
|
||||
if (importList != null && importList.size() > 1) {
|
||||
JetImportDirective firstImport = importList.get(0);
|
||||
PsiElement importKeyword = firstImport.getFirstChild();
|
||||
List<JetImportDirective> imports = file.getImportDirectives();
|
||||
if (imports.size() > 1) {
|
||||
PsiElement importKeyword = imports.get(0).getFirstChild();
|
||||
int startOffset = importKeyword.getTextRange().getEndOffset() + 1;
|
||||
int endOffset = importList.get(importList.size() - 1).getTextRange().getEndOffset();
|
||||
|
||||
JetImportList importList = file.getImportList();
|
||||
int endOffset = importList.getTextRange().getEndOffset();
|
||||
|
||||
TextRange range = new TextRange(startOffset, endOffset);
|
||||
descriptors.add(new FoldingDescriptor(firstImport, range));
|
||||
descriptors.add(new FoldingDescriptor(importList, range));
|
||||
}
|
||||
|
||||
appendDescriptors(root.getNode(), document, descriptors);
|
||||
@@ -86,7 +89,7 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware {
|
||||
if (node.getElementType() == KDocTokens.KDOC) {
|
||||
return "/**...*/";
|
||||
}
|
||||
if (node.getPsi() instanceof JetImportDirective) {
|
||||
if (node.getPsi() instanceof JetImportList) {
|
||||
return "...";
|
||||
}
|
||||
return "{...}";
|
||||
@@ -96,7 +99,7 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware {
|
||||
public boolean isCollapsedByDefault(@NotNull ASTNode astNode) {
|
||||
JavaCodeFoldingSettings settings = JavaCodeFoldingSettings.getInstance();
|
||||
|
||||
if (astNode.getPsi() instanceof JetImportDirective) {
|
||||
if (astNode.getPsi() instanceof JetImportList) {
|
||||
return settings.isCollapseImports();
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ public class JetFormattingModelBuilder implements FormattingModelBuilder {
|
||||
.after(NAMESPACE_HEADER).blankLines(1)
|
||||
|
||||
.between(IMPORT_DIRECTIVE, IMPORT_DIRECTIVE).lineBreakInCode()
|
||||
.after(IMPORT_DIRECTIVE).blankLines(1)
|
||||
.after(IMPORT_LIST).blankLines(1)
|
||||
|
||||
.before(DOC_COMMENT).lineBreakInCode()
|
||||
.before(FUN).lineBreakInCode()
|
||||
|
||||
@@ -125,21 +125,20 @@ public class ImportInsertHelper {
|
||||
writeImportToFile(importPath, file);
|
||||
}
|
||||
|
||||
public static void writeImportToFile(ImportPath importPath, JetFile file) {
|
||||
JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath);
|
||||
List<JetImportDirective> importDirectives = file.getImportDirectives();
|
||||
|
||||
if (!importDirectives.isEmpty()) {
|
||||
JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
|
||||
lastDirective.getParent().addAfter(newDirective, lastDirective);
|
||||
public static void writeImportToFile(@NotNull ImportPath importPath, @NotNull JetFile file) {
|
||||
JetImportList importList = file.getImportList();
|
||||
if (importList != null) {
|
||||
JetImportDirective newDirective = JetPsiFactory.createImportDirective(file.getProject(), importPath);
|
||||
importList.add(newDirective);
|
||||
}
|
||||
else {
|
||||
JetImportList newDirective = JetPsiFactory.createImportDirectiveWithImportList(file.getProject(), importPath);
|
||||
JetNamespaceHeader header = file.getNamespaceHeader();
|
||||
if (header == null) {
|
||||
throw new IllegalStateException("Scripts are not supported: " + file.getName());
|
||||
}
|
||||
|
||||
header.getParent().addAfter(newDirective, file.getNamespaceHeader());
|
||||
header.getParent().addAfter(newDirective, header);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package sometest
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.HashSet
|
||||
import java.util.HashMap
|
||||
|
||||
val some: HashSet<Int>? = null
|
||||
val some2: HashMap<Int, Int>? = null
|
||||
|
||||
// SET_TRUE: setCollapseImports
|
||||
// REGION BEFORE: 25:94
|
||||
// REGION AFTER: 25:67
|
||||
@@ -2,6 +2,7 @@
|
||||
// ERROR: 'f' overrides nothing
|
||||
// ERROR: 'f' overrides nothing
|
||||
import a.B
|
||||
|
||||
class BB : B() {
|
||||
<caret>override fun f() {}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,10 @@
|
||||
package a
|
||||
|
||||
import java.util.Collections
|
||||
|
||||
package b {
|
||||
|
||||
import java.util.List
|
||||
|
||||
class M {
|
||||
trait A {
|
||||
val l: jet.List<Int><caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,10 @@
|
||||
package a
|
||||
|
||||
import java.util.Collections
|
||||
|
||||
package b {
|
||||
|
||||
import java.util.List
|
||||
|
||||
class M {
|
||||
trait A {
|
||||
val l = <caret>Collections.emptyList<Int>()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,10 @@ class FoldingAfterOptimizeImportsTest : AbstractKotlinFoldingTest() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testFoldingAfterOptimizeImportsRemoveFirst() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
private fun doTest() {
|
||||
fixture.configureByFile(getTestName(true) + ".kt")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user