Put comments and whitespaces under body in script files

It's more consistent to normal function body for IDE.
Doc comments in file beginning are now sticks to declarations
correctly.
Moving declarations at the end of scripts is fixed
This commit is contained in:
Nikolay Krasko
2017-08-03 16:27:38 +03:00
parent 8de6017e53
commit 8d226594ff
11 changed files with 84 additions and 14 deletions
@@ -30,6 +30,8 @@ import org.jetbrains.kotlin.lexer.KtTokens;
import static org.jetbrains.kotlin.KtNodeTypes.*;
import static org.jetbrains.kotlin.lexer.KtTokens.*;
import static org.jetbrains.kotlin.parsing.KotlinParsing.AnnotationParsingMode.*;
import static org.jetbrains.kotlin.parsing.KotlinWhitespaceAndCommentsBindersKt.PRECEDING_ALL_BINDER;
import static org.jetbrains.kotlin.parsing.KotlinWhitespaceAndCommentsBindersKt.TRAILING_ALL_BINDER;
public class KotlinParsing extends AbstractKotlinParsing {
private static final Logger LOG = Logger.getInstance(KotlinParsing.class);
@@ -174,7 +176,11 @@ public class KotlinParsing extends AbstractKotlinParsing {
checkForUnexpectedSymbols();
blockMarker.done(BLOCK);
blockMarker.setCustomEdgeTokenBinders(PRECEDING_ALL_BINDER, TRAILING_ALL_BINDER);
scriptMarker.done(SCRIPT);
scriptMarker.setCustomEdgeTokenBinders(PRECEDING_ALL_BINDER, TRAILING_ALL_BINDER);
fileMarker.done(KT_FILE);
}
@@ -113,4 +113,17 @@ object DoNotBindAnything : WhitespacesAndCommentsBinder {
tokens: List<IElementType>, atStreamEdge: Boolean, getter: WhitespacesAndCommentsBinder.TokenTextGetter): Int {
return 0
}
}
}
class BindAll(val isTrailing: Boolean) : WhitespacesAndCommentsBinder {
override fun getEdgePosition(
tokens: List<IElementType>, atStreamEdge: Boolean, getter: WhitespacesAndCommentsBinder.TokenTextGetter): Int {
return if (!isTrailing) 0 else tokens.size
}
}
@JvmField
val PRECEDING_ALL_BINDER: WhitespacesAndCommentsBinder = BindAll(false)
@JvmField
val TRAILING_ALL_BINDER: WhitespacesAndCommentsBinder = BindAll(true)
+5
View File
@@ -0,0 +1,5 @@
/** Comment */
fun test() {}
// Some other comment
+26
View File
@@ -0,0 +1,26 @@
KtFile: FunctionComment.kts
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
SCRIPT
BLOCK
FUN
KDoc
PsiElement(KDOC_START)('/**')
KDOC_SECTION
PsiElement(KDOC_TEXT)(' Comment ')
PsiElement(KDOC_END)('*/')
PsiWhiteSpace('\n')
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('test')
VALUE_PARAMETER_LIST
PsiElement(LPAR)('(')
PsiElement(RPAR)(')')
PsiWhiteSpace(' ')
BLOCK
PsiElement(LBRACE)('{')
PsiElement(RBRACE)('}')
PsiWhiteSpace('\n\n')
PsiComment(EOL_COMMENT)('// Some other comment')
+1 -1
View File
@@ -23,9 +23,9 @@ KtFile: Import.kts
PsiElement(IDENTIFIER)('ddd')
PsiElement(DOT)('.')
PsiElement(MUL)('*')
PsiWhiteSpace('\n\n')
SCRIPT
BLOCK
PsiWhiteSpace('\n\n')
FUN
PsiElement(fun)('fun')
PsiWhiteSpace(' ')
+2 -2
View File
@@ -3,10 +3,10 @@ KtFile: Shebang.kts
<empty list>
IMPORT_LIST
<empty list>
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
SCRIPT
BLOCK
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
SCRIPT_INITIALIZER
CALL_EXPRESSION
REFERENCE_EXPRESSION
@@ -2689,6 +2689,12 @@ public class ParsingTestGenerated extends AbstractParsingTest {
doParsingTest(fileName);
}
@TestMetadata("FunctionComment.kts")
public void testFunctionComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/script/FunctionComment.kts");
doParsingTest(fileName);
}
@TestMetadata("Import.kts")
public void testImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/script/Import.kts");
@@ -672,7 +672,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractMoveStatementTest> {
model("codeInsight/moveUpDown/classBodyDeclarations", testMethod = "doTestClassBodyDeclaration")
model("codeInsight/moveUpDown/classBodyDeclarations", pattern = KT_OR_KTS, testMethod = "doTestClassBodyDeclaration")
model("codeInsight/moveUpDown/closingBraces", testMethod = "doTestExpression")
model("codeInsight/moveUpDown/expressions", pattern = KT_OR_KTS, testMethod = "doTestExpression")
model("codeInsight/moveUpDown/parametersAndArguments", testMethod = "doTestExpression")
@@ -0,0 +1,4 @@
// MOVE: up
fun test() {}
<caret>fun test1() {}
@@ -0,0 +1,4 @@
// MOVE: up
<caret>fun test1() {}
fun test() {}
@@ -35,7 +35,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class ClassBodyDeclarations extends AbstractMoveStatementTest {
public void testAllFilesPresentInClassBodyDeclarations() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/accessors")
@@ -67,7 +67,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
}
public void testAllFilesPresentInAccessors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/accessors"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/accessors"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
}
@@ -76,7 +76,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Class extends AbstractMoveStatementTest {
public void testAllFilesPresentInClass() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/class"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("classAtBrace1.kt")
@@ -205,7 +205,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class ClassInitializer extends AbstractMoveStatementTest {
public void testAllFilesPresentInClassInitializer() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/classInitializer"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/classInitializer"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("classInitializerAtBrace1.kt")
@@ -286,7 +286,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Enums extends AbstractMoveStatementTest {
public void testAllFilesPresentInEnums() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/enums"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/enums"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("enum1.kt")
@@ -343,7 +343,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Function extends AbstractMoveStatementTest {
public void testAllFilesPresentInFunction() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("functionAtBrace1.kt")
@@ -441,6 +441,12 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtProperty2.kt");
doTestClassBodyDeclaration(fileName);
}
@TestMetadata("functionAtTheScriptEnd.kts")
public void testFunctionAtTheScriptEnd() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts");
doTestClassBodyDeclaration(fileName);
}
}
@TestMetadata("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/functionAnchors")
@@ -448,7 +454,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class FunctionAnchors extends AbstractMoveStatementTest {
public void testAllFilesPresentInFunctionAnchors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/functionAnchors"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/functionAnchors"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("keyword.kt")
@@ -493,7 +499,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Property extends AbstractMoveStatementTest {
public void testAllFilesPresentInProperty() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/property"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/property"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("propertyAtBrace1.kt")
@@ -598,7 +604,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class PropertyAnchors extends AbstractMoveStatementTest {
public void testAllFilesPresentInPropertyAnchors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/propertyAnchors"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/classBodyDeclarations/propertyAnchors"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("keyword.kt")