package com.plexworlds.l3 fun String.shouldBeSkippedOnPosition(offset: Int) = checkElementUnderCaret(this, offset) { afterSemicolon() || afterLBrace() || afterRBrace() || beforeLParenthesis() || afterRParenthesis() || insideIdentifier() || atTheBoundaryOfStringLiteral() || atTheBoundaryOfCharLiteral() || atTheEndOfDigitalLiteral() } private fun Pair.afterSemicolon(): Boolean { return first == ';' } private fun Pair.afterLBrace(): Boolean { return first == '{' } private fun Pair.afterRBrace(): Boolean { return first == '}' } private fun Pair.beforeLParenthesis(): Boolean { return second == '(' } private fun Pair.afterRParenthesis(): Boolean { return first == ')' } private fun Pair.insideIdentifier(): Boolean { return first.isLetterOrDigit() && second.isLetterOrDigit() } private fun Pair.atTheBoundaryOfStringLiteral(): Boolean { return first == '"' || second == '"' } private fun Pair.atTheBoundaryOfCharLiteral(): Boolean { return first == '\'' || second == '\'' } private fun Pair.atTheEndOfDigitalLiteral(): Boolean { fun Char.isArithmeticOperator() = this == '+' || this == '-' || this == '*' || this == '/' fun Char.isLogicalOperator() = this == '&' || this == '|' || this == '!' || this == '=' || this == '<' || this == '>' return (first.isDigit() || first == 'L' || first == 'f') && (second == ';' || second == ' ' || second.isArithmeticOperator() || second.isLogicalOperator()) } private fun checkElementUnderCaret( text: String, offset: Int, satisfySkippedCriteria: Pair.() -> Boolean ): Boolean { val beforeCaretChar = if (offset in 0..text.lastIndex) text[offset] else '\n' val afterCaretChar = if (offset + 1 in 0..text.lastIndex) text[offset + 1] else '\n' return (beforeCaretChar to afterCaretChar).satisfySkippedCriteria() }