KotlinLikeLangLineIndentProvider: cleanup code

Part of #KT-22211
This commit is contained in:
Dmitry Gridin
2020-06-14 14:21:25 +07:00
parent 01707800c1
commit c95216dc5d
@@ -128,6 +128,87 @@ abstract class KotlinLikeLangLineIndentProvider : JavaLikeLangLineIndentProvider
} }
} }
private enum class KotlinElement : SemanticEditorPosition.SyntaxElement {
TemplateEntryOpen,
TemplateEntryClose,
Arrow,
WhenKeyword,
WhileKeyword,
RegularStringPart,
KDoc,
Identifier,
OpenTypeBrace,
CloseTypeBrace,
FunctionKeyword,
Dot,
Quest,
Eq,
Val, Var,
}
companion object {
private val SYNTAX_MAP: Map<IElementType, SemanticEditorPosition.SyntaxElement> = hashMapOf(
KtTokens.WHITE_SPACE to Whitespace,
KtTokens.EOL_COMMENT to LineComment,
KtTokens.BLOCK_COMMENT to BlockComment,
KtTokens.DOC_COMMENT to KDoc,
KtTokens.ARROW to Arrow,
KtTokens.LONG_TEMPLATE_ENTRY_START to TemplateEntryOpen,
KtTokens.LONG_TEMPLATE_ENTRY_END to TemplateEntryClose,
KtTokens.LBRACE to BlockOpeningBrace,
KtTokens.RBRACE to BlockClosingBrace,
KtTokens.LPAR to LeftParenthesis,
KtTokens.RPAR to RightParenthesis,
KtTokens.LBRACKET to ArrayOpeningBracket,
KtTokens.RBRACKET to ArrayClosingBracket,
KtTokens.LT to OpenTypeBrace,
KtTokens.GT to CloseTypeBrace,
KtTokens.IF_KEYWORD to IfKeyword,
KtTokens.ELSE_KEYWORD to ElseKeyword,
KtTokens.WHEN_KEYWORD to WhenKeyword,
KtTokens.TRY_KEYWORD to TryKeyword,
KtTokens.WHILE_KEYWORD to WhileKeyword,
KtTokens.DO_KEYWORD to DoKeyword,
KtTokens.FOR_KEYWORD to ForKeyword,
KtTokens.REGULAR_STRING_PART to RegularStringPart,
KtTokens.IDENTIFIER to Identifier,
KtTokens.FUN_KEYWORD to FunctionKeyword,
KtTokens.DOT to Dot,
KtTokens.QUEST to Quest,
KtTokens.COMMA to Comma,
KtTokens.COLON to Colon,
KtTokens.EQ to Eq,
KtTokens.VAL_KEYWORD to Val,
KtTokens.VAR_KEYWORD to Var,
)
private val CONTROL_FLOW_KEYWORDS: HashSet<SemanticEditorPosition.SyntaxElement> = hashSetOf(
WhenKeyword,
IfKeyword,
ElseKeyword,
DoKeyword,
WhileKeyword,
ForKeyword,
TryKeyword,
)
private val WHITE_SPACE_OR_COMMENT_BIT_SET: Array<SemanticEditorPosition.SyntaxElement> = arrayOf(
Whitespace,
LineComment,
BlockComment,
)
private fun IndentCalculatorFactory.createIndentCalculatorForBrace( private fun IndentCalculatorFactory.createIndentCalculatorForBrace(
before: SemanticEditorPosition, before: SemanticEditorPosition,
after: SemanticEditorPosition, after: SemanticEditorPosition,
@@ -480,10 +561,27 @@ abstract class KotlinLikeLangLineIndentProvider : JavaLikeLangLineIndentProvider
} }
} }
private fun SemanticEditorPosition.moveBeforeWhileThisIsWhiteSpaceOrComment() =
moveBeforeOptionalMix(*WHITE_SPACE_OR_COMMENT_BIT_SET)
private fun SemanticEditorPosition.moveBeforeIgnoringWhiteSpaceOrComment() {
moveBefore()
moveBeforeWhileThisIsWhiteSpaceOrComment()
}
private fun SemanticEditorPosition.beforeIgnoringWhiteSpaceOrComment() = copyAnd { it.moveBeforeIgnoringWhiteSpaceOrComment() }
private fun SemanticEditorPosition.moveBeforeWhileThisIsWhiteSpaceOnSameLineOrBlockComment(): Boolean {
while (!isAtEnd) {
if (isAt(Whitespace) && isAtMultiline) return false
if (!isAt(BlockComment)) break
moveBefore()
}
return true
}
private fun SemanticEditorPosition.isIdentifier(): Boolean = isAt(Identifier) || isAt(KtTokens.THIS_KEYWORD) private fun SemanticEditorPosition.isIdentifier(): Boolean = isAt(Identifier) || isAt(KtTokens.THIS_KEYWORD)
private fun SemanticEditorPosition.isVarOrVal(): Boolean = isAtAnyOf(Var, Val) private fun SemanticEditorPosition.isVarOrVal(): Boolean = isAtAnyOf(Var, Val)
private fun SemanticEditorPosition.moveBeforeBlockIfPossible(): Boolean = moveBeforeParenthesesIfPossible( private fun SemanticEditorPosition.moveBeforeBlockIfPossible(): Boolean = moveBeforeParenthesesIfPossible(
leftParenthesis = BlockOpeningBrace, leftParenthesis = BlockOpeningBrace,
rightParenthesis = BlockClosingBrace, rightParenthesis = BlockClosingBrace,
@@ -509,99 +607,6 @@ abstract class KotlinLikeLangLineIndentProvider : JavaLikeLangLineIndentProvider
moveBeforeWhileThisIsWhiteSpaceOrComment() moveBeforeWhileThisIsWhiteSpaceOrComment()
return true return true
} }
private fun SemanticEditorPosition.moveBeforeWhileThisIsWhiteSpaceOrComment() = moveBeforeOptionalMix(*WHITE_SPACE_OR_COMMENT_BIT_SET)
private fun SemanticEditorPosition.moveBeforeWhileThisIsWhiteSpaceOnSameLineOrBlockComment(): Boolean {
while (!isAtEnd) {
if (isAt(Whitespace) && isAtMultiline) return false
if (!isAt(BlockComment)) break
moveBefore()
}
return true
}
private fun SemanticEditorPosition.moveBeforeIgnoringWhiteSpaceOrComment() {
moveBefore()
moveBeforeWhileThisIsWhiteSpaceOrComment()
}
private fun SemanticEditorPosition.beforeIgnoringWhiteSpaceOrComment() = copyAnd { it.moveBeforeIgnoringWhiteSpaceOrComment() }
private enum class KotlinElement : SemanticEditorPosition.SyntaxElement {
TemplateEntryOpen,
TemplateEntryClose,
Arrow,
WhenKeyword,
WhileKeyword,
RegularStringPart,
KDoc,
Identifier,
OpenTypeBrace,
CloseTypeBrace,
FunctionKeyword,
Dot,
Quest,
Eq,
Val, Var,
}
companion object {
private val SYNTAX_MAP: Map<IElementType, SemanticEditorPosition.SyntaxElement> = hashMapOf(
KtTokens.WHITE_SPACE to Whitespace,
KtTokens.LONG_TEMPLATE_ENTRY_START to TemplateEntryOpen,
KtTokens.LONG_TEMPLATE_ENTRY_END to TemplateEntryClose,
KtTokens.EOL_COMMENT to LineComment,
KtTokens.BLOCK_COMMENT to BlockComment,
KtTokens.DOC_COMMENT to KDoc,
KtTokens.ARROW to Arrow,
KtTokens.LBRACE to BlockOpeningBrace,
KtTokens.RBRACE to BlockClosingBrace,
KtTokens.LPAR to LeftParenthesis,
KtTokens.RPAR to RightParenthesis,
KtTokens.IF_KEYWORD to IfKeyword,
KtTokens.ELSE_KEYWORD to ElseKeyword,
KtTokens.WHEN_KEYWORD to WhenKeyword,
KtTokens.TRY_KEYWORD to TryKeyword,
KtTokens.WHILE_KEYWORD to WhileKeyword,
KtTokens.DO_KEYWORD to DoKeyword,
KtTokens.FOR_KEYWORD to ForKeyword,
KtTokens.REGULAR_STRING_PART to RegularStringPart,
KtTokens.LBRACKET to ArrayOpeningBracket,
KtTokens.RBRACKET to ArrayClosingBracket,
KtTokens.IDENTIFIER to Identifier,
KtTokens.LT to OpenTypeBrace,
KtTokens.GT to CloseTypeBrace,
KtTokens.FUN_KEYWORD to FunctionKeyword,
KtTokens.DOT to Dot,
KtTokens.QUEST to Quest,
KtTokens.COMMA to Comma,
KtTokens.COLON to Colon,
KtTokens.EQ to Eq,
KtTokens.VAL_KEYWORD to Val,
KtTokens.VAR_KEYWORD to Var,
)
private val CONTROL_FLOW_KEYWORDS: HashSet<SemanticEditorPosition.SyntaxElement> = hashSetOf(
WhenKeyword,
IfKeyword,
ElseKeyword,
DoKeyword,
WhileKeyword,
ForKeyword,
TryKeyword,
)
private val WHITE_SPACE_OR_COMMENT_BIT_SET: Array<SemanticEditorPosition.SyntaxElement> = arrayOf(
Whitespace,
LineComment,
BlockComment,
)
} }
} }