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 {