Always assign shebang comment to same position before package
This commit is contained in:
@@ -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();
|
||||
|
||||
+11
@@ -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
@@ -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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env kotlin
|
||||
|
||||
import test
|
||||
|
||||
println(args)
|
||||
@@ -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)(')')
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env kotlin
|
||||
|
||||
package test
|
||||
|
||||
println(args)
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user