Skip to content

handle cases with no spaces and more tests #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import org.jetbrains.kotlinx.ki.shell.parser.KotlinParser.*
* The goal is navigating to the sub-elements not directly represented in it and
* granular control over keywords space to be taken into account by highlighting.
*/
private const val IF_EXPR_OFFSET = 2
private const val ELSE_OFFSET = 4
private const val FOR_EXPR_OFFSET = 3
private const val WHILE_EXPR_OFFSET = 5
private const val IF_TOKEN_LENGTH = 2
private const val ELSE_TOKEN_LENGTH = 4
private const val FOR_TOKEN_LENGTH = 3
private const val WHILE_TOKEN_LENGTH = 5

/**
* This class provides an empty implementation of [KotlinParserListener],
Expand Down Expand Up @@ -96,9 +96,9 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {
}
}

fun highlightKeywordWithOffset(ctx: ParserRuleContext, start: Int, offset: Int) {
fun highlightKeywordWithOffset(ctx: ParserRuleContext, start: Int, tokenLength: Int) {
exitHighlightingElement(ctx)
result.add(ElementWithPos(start, start + offset, RecogizedElements.Keyword))
result.add(ElementWithPos(start, start + tokenLength - 1, RecogizedElements.Keyword))
lastElementStart = start
}

Expand Down Expand Up @@ -421,13 +421,13 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {
override fun exitLoopStatement(ctx: LoopStatementContext) {}

override fun enterForStatement(ctx: ForStatementContext) {
highlightKeywordWithOffset(ctx, ctx.start.startIndex, FOR_EXPR_OFFSET)
highlightKeywordWithOffset(ctx, ctx.start.startIndex, FOR_TOKEN_LENGTH)
}

override fun exitForStatement(ctx: ForStatementContext) {}

override fun enterWhileStatement(ctx: WhileStatementContext) {
highlightKeywordWithOffset(ctx, ctx.start.startIndex, WHILE_EXPR_OFFSET)
highlightKeywordWithOffset(ctx, ctx.start.startIndex, WHILE_TOKEN_LENGTH)
}

override fun exitWhileStatement(ctx: WhileStatementContext) {}
Expand All @@ -438,7 +438,7 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {

override fun exitDoWhileStatement(ctx: DoWhileStatementContext) {
ctx.WHILE()?.let {
highlightKeywordWithOffset(ctx, it.symbol.startIndex, WHILE_EXPR_OFFSET)
highlightKeywordWithOffset(ctx, it.symbol.startIndex, WHILE_TOKEN_LENGTH)
}
}

Expand Down Expand Up @@ -673,12 +673,12 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {
override fun exitSuperExpression(ctx: SuperExpressionContext) {}

override fun enterIfExpression(ctx: IfExpressionContext) {
highlightKeywordWithOffset(ctx, ctx.start.startIndex, IF_EXPR_OFFSET)
highlightKeywordWithOffset(ctx, ctx.start.startIndex, IF_TOKEN_LENGTH)
}

override fun exitIfExpression(ctx: IfExpressionContext) {
ctx.ELSE()?.let {
highlightKeywordWithOffset(ctx, it.symbol.startIndex, ELSE_OFFSET)
highlightKeywordWithOffset(ctx, it.symbol.startIndex, ELSE_TOKEN_LENGTH)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class KotlinHighlighterTest : TestCase() {
fun testIfElse() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk(1 > 2) 11 kkkkkkkk(1 > 2) { 12 } kkkkk{ 13 }",
"kk (1 > 2) 11 kkkk kk (1 > 2) { 12 } kkkk { 13 }",
ht.highlight("if (1 > 2) 11 else if (1 > 2) { 12 } else { 13 }").mnemonics()
)
}
Expand All @@ -112,38 +112,92 @@ class KotlinHighlighterTest : TestCase() {
fun testIfNoElse() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk(1 > 2) { 11 }",
"kk (1 > 2) { 11 }",
ht.highlight("if (1 > 2) { 11 }").mnemonics()
)
}

@Test
fun testIfNoSpace() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kk(1 > 2) { 11 }",
ht.highlight("if(1 > 2) { 11 }").mnemonics()
)
}

@Test
fun testForLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkkk(x in \"gg\") x + 1",
"kkk (x in \"gg\") x + 1",
ht.highlight("for (x in \"gg\") x + 1").mnemonics()
)
}

@Test
fun testForNoSpaceLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk(x in \"gg\") x + 1",
ht.highlight("for(x in \"gg\") x + 1").mnemonics()
)
}

@Test
fun testWhileLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkkkkk(1 > 2) { 1 + 2 }",
"kkkkk (1 > 2) { 1 + 2 }",
ht.highlight("while (1 > 2) { 1 + 2 }").mnemonics()
)
}

@Test
fun testWhileNoSpaceLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkkkk(1 > 2) { 1 + 2 }",
ht.highlight("while(1 > 2) { 1 + 2 }").mnemonics()
)
}

@Test
fun testDoWhileLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk{ 1 + 2 } kkkkkk(1 > 2)",
"kkk{ 1 + 2 } kkkkk (1 > 2)",
ht.highlight("do { 1 + 2 } while (1 > 2)").mnemonics()
)
}

@Test
fun testDoNoSpaceWhileLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kk{ 1 + 2 } kkkkk (1 > 2)",
ht.highlight("do{ 1 + 2 } while (1 > 2)").mnemonics()
)
}

@Test
fun testDoWhileNoSpaceLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk{ 1 + 2 } kkkkk(1 > 2)",
ht.highlight("do { 1 + 2 } while(1 > 2)").mnemonics()
)
}

@Test
fun testInvalidDoNoWhileLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"do { 1 + 2 } 11",
ht.highlight("do { 1 + 2 } 11").mnemonics()
)
}

private fun Map<String, AttributedStyle?>.filter(list: List<String>): HighlightStylesFromMap =
HighlightStylesFromMap(map { if (list.contains(it.key)) Pair(it.key, it.value) else Pair(it.key, null) }.toMap())

Expand Down