From 8d226594ff54a640a61d09ed2d65cf7e57559304 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 3 Aug 2017 16:27:38 +0300 Subject: [PATCH] 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 --- .../kotlin/parsing/KotlinParsing.java | 6 +++++ .../KotlinWhitespaceAndCommentsBinders.kt | 15 ++++++++++- .../testData/psi/script/FunctionComment.kts | 5 ++++ .../testData/psi/script/FunctionComment.txt | 26 +++++++++++++++++++ compiler/testData/psi/script/Import.txt | 2 +- compiler/testData/psi/script/Shebang.txt | 4 +-- .../kotlin/parsing/ParsingTestGenerated.java | 6 +++++ .../kotlin/generators/tests/GenerateTests.kt | 2 +- .../function/functionAtTheScriptEnd.kts | 4 +++ .../function/functionAtTheScriptEnd.kts.after | 4 +++ .../MoveStatementTestGenerated.java | 24 ++++++++++------- 11 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 compiler/testData/psi/script/FunctionComment.kts create mode 100644 compiler/testData/psi/script/FunctionComment.txt create mode 100644 idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts create mode 100644 idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts.after diff --git a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java index a9a7ffaef74..d14913c618f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java @@ -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); } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinWhitespaceAndCommentsBinders.kt b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinWhitespaceAndCommentsBinders.kt index be3121e1585..c31c7d3e838 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinWhitespaceAndCommentsBinders.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinWhitespaceAndCommentsBinders.kt @@ -113,4 +113,17 @@ object DoNotBindAnything : WhitespacesAndCommentsBinder { tokens: List, atStreamEdge: Boolean, getter: WhitespacesAndCommentsBinder.TokenTextGetter): Int { return 0 } -} \ No newline at end of file +} + +class BindAll(val isTrailing: Boolean) : WhitespacesAndCommentsBinder { + override fun getEdgePosition( + tokens: List, 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) \ No newline at end of file diff --git a/compiler/testData/psi/script/FunctionComment.kts b/compiler/testData/psi/script/FunctionComment.kts new file mode 100644 index 00000000000..e4f64fc8f12 --- /dev/null +++ b/compiler/testData/psi/script/FunctionComment.kts @@ -0,0 +1,5 @@ +/** Comment */ +fun test() {} + +// Some other comment + diff --git a/compiler/testData/psi/script/FunctionComment.txt b/compiler/testData/psi/script/FunctionComment.txt new file mode 100644 index 00000000000..4ac3555b137 --- /dev/null +++ b/compiler/testData/psi/script/FunctionComment.txt @@ -0,0 +1,26 @@ +KtFile: FunctionComment.kts + PACKAGE_DIRECTIVE + + IMPORT_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') \ No newline at end of file diff --git a/compiler/testData/psi/script/Import.txt b/compiler/testData/psi/script/Import.txt index 3c150bfd50d..f4b6575bd07 100644 --- a/compiler/testData/psi/script/Import.txt +++ b/compiler/testData/psi/script/Import.txt @@ -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(' ') diff --git a/compiler/testData/psi/script/Shebang.txt b/compiler/testData/psi/script/Shebang.txt index 6be1ebb2185..192a9a621e3 100644 --- a/compiler/testData/psi/script/Shebang.txt +++ b/compiler/testData/psi/script/Shebang.txt @@ -3,10 +3,10 @@ KtFile: Shebang.kts IMPORT_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 diff --git a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java index 8d90df896d0..b0e5cf44e75 100644 --- a/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/parsing/ParsingTestGenerated.java @@ -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"); diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 63a80669140..187156c92b8 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -672,7 +672,7 @@ fun main(args: Array) { } testClass { - 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") diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts new file mode 100644 index 00000000000..8cec69ac7cf --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts @@ -0,0 +1,4 @@ +// MOVE: up + +fun test() {} +fun test1() {} diff --git a/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts.after b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts.after new file mode 100644 index 00000000000..46f943bd4c7 --- /dev/null +++ b/idea/testData/codeInsight/moveUpDown/classBodyDeclarations/function/functionAtTheScriptEnd.kts.after @@ -0,0 +1,4 @@ +// MOVE: up + +fun test1() {} +fun test() {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java index 188ecee94d0..a669c031e72 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/moveUpDown/MoveStatementTestGenerated.java @@ -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")