Always assign shebang comment to same position before package

This commit is contained in:
Nikolay Krasko
2017-08-04 17:29:26 +03:00
parent 8d226594ff
commit 7d66af6583
8 changed files with 84 additions and 4 deletions
@@ -231,8 +231,8 @@ public class KotlinParsing extends AbstractKotlinParsing {
parseFileAnnotationList(FILE_ANNOTATIONS_WHEN_PACKAGE_OMITTED);
packageDirective = mark();
packageDirective.done(PACKAGE_DIRECTIVE);
// this is necessary to allow comments at the start of the file to be bound to the first declaration
packageDirective.setCustomEdgeTokenBinders(DoNotBindAnything.INSTANCE, null);
// Need to skip everything but shebang comment to allow comments at the start of the file to be bound to the first declaration.
packageDirective.setCustomEdgeTokenBinders(BindFirstShebangWithWhitespaceOnly.INSTANCE, null);
}
parseImportDirectives();
@@ -115,6 +115,17 @@ object DoNotBindAnything : WhitespacesAndCommentsBinder {
}
}
object BindFirstShebangWithWhitespaceOnly : WhitespacesAndCommentsBinder {
override fun getEdgePosition(
tokens: List<IElementType>, atStreamEdge: Boolean, getter: WhitespacesAndCommentsBinder.TokenTextGetter): Int {
if (tokens.firstOrNull() == KtTokens.SHEBANG_COMMENT) {
return if (tokens.getOrNull(1) == KtTokens.WHITE_SPACE) 2 else 1
}
return 0
}
}
class BindAll(val isTrailing: Boolean) : WhitespacesAndCommentsBinder {
override fun getEdgePosition(
tokens: List<IElementType>, atStreamEdge: Boolean, getter: WhitespacesAndCommentsBinder.TokenTextGetter): Int {
+2 -2
View File
@@ -1,12 +1,12 @@
KtFile: Shebang.kts
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
<empty list>
SCRIPT
BLOCK
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
SCRIPT_INITIALIZER
CALL_EXPRESSION
REFERENCE_EXPRESSION
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env kotlin
import test
println(args)
+24
View File
@@ -0,0 +1,24 @@
KtFile: ShebangWithImports.kts
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
PACKAGE_DIRECTIVE
<empty list>
IMPORT_LIST
IMPORT_DIRECTIVE
PsiElement(import)('import')
PsiWhiteSpace(' ')
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('test')
SCRIPT
BLOCK
PsiWhiteSpace('\n\n')
SCRIPT_INITIALIZER
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('println')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('args')
PsiElement(RPAR)(')')
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env kotlin
package test
println(args)
+23
View File
@@ -0,0 +1,23 @@
KtFile: ShebangWithPackage.kts
PsiComment(SHEBANG_COMMENT)('#!/usr/bin/env kotlin')
PsiWhiteSpace('\n\n')
PACKAGE_DIRECTIVE
PsiElement(package)('package')
PsiWhiteSpace(' ')
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('test')
IMPORT_LIST
<empty list>
SCRIPT
BLOCK
PsiWhiteSpace('\n\n')
SCRIPT_INITIALIZER
CALL_EXPRESSION
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('println')
VALUE_ARGUMENT_LIST
PsiElement(LPAR)('(')
VALUE_ARGUMENT
REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('args')
PsiElement(RPAR)(')')
@@ -2719,6 +2719,18 @@ public class ParsingTestGenerated extends AbstractParsingTest {
doParsingTest(fileName);
}
@TestMetadata("ShebangWithImports.kts")
public void testShebangWithImports() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/script/ShebangWithImports.kts");
doParsingTest(fileName);
}
@TestMetadata("ShebangWithPackage.kts")
public void testShebangWithPackage() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/script/ShebangWithPackage.kts");
doParsingTest(fileName);
}
@TestMetadata("SimpleScript.kts")
public void testSimpleScript() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/psi/script/SimpleScript.kts");