Skip to content

if/else/for/do/while highlighting fixed #97

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 2 commits into from
Jan 26, 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
2 changes: 1 addition & 1 deletion ki-shell/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>ki</artifactId>
<version>0.4.5-SNAPSHOT</version>
<version>0.4.5</version>
</parent>

<artifactId>ki-shell</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import org.antlr.v4.runtime.tree.ErrorNode
import org.antlr.v4.runtime.tree.TerminalNode
import org.jetbrains.kotlinx.ki.shell.parser.KotlinParser.*

/**
* Magic constants are taken from official grammar.
* https://kotlinlang.org/docs/reference/grammar.html#ifExpression
* https://kotlinlang.org/docs/reference/grammar.html#forStatement
* https://kotlinlang.org/docs/reference/grammar.html#whileStatement
* https://kotlinlang.org/docs/reference/grammar.html#doWhileStatement
* 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

/**
* This class provides an empty implementation of [KotlinParserListener],
* which can be extended to create a listener which only needs to handle a subset
Expand Down Expand Up @@ -82,6 +96,12 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {
}
}

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

override fun enterKotlinFile(ctx: KotlinFileContext) {}

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

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

override fun exitForStatement(ctx: ForStatementContext) {}

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

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

override fun exitDoWhileStatement(ctx: DoWhileStatementContext) {}
override fun exitDoWhileStatement(ctx: DoWhileStatementContext) {
val whileToken = ctx.WHILE().symbol
highlightKeywordWithOffset(ctx, whileToken.startIndex, WHILE_EXPR_OFFSET)
}

override fun enterAssignment(ctx: AssignmentContext) {}

Expand Down Expand Up @@ -649,10 +672,13 @@ class KotlinParserListenerForHighlighting : KotlinParserListener {
override fun exitSuperExpression(ctx: SuperExpressionContext) {}

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

override fun exitIfExpression(ctx: IfExpressionContext) {}
override fun exitIfExpression(ctx: IfExpressionContext) {
val elseToken = ctx.ELSE().symbol
highlightKeywordWithOffset(ctx, elseToken.startIndex, ELSE_OFFSET)
}

override fun enterWhenSubject(ctx: WhenSubjectContext) {
exitHighlightingElement(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,42 @@ class KotlinHighlighterTest : TestCase() {
assertEquals("fun <T, T> const(x: P, y: K): P = x", ht.highlight("fun <P, K> const(x: P, y: K): P = x").mnemonics())
}

@Test
fun testIfElse() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkk(1 > 2) 11 kkkkkkkk(1 > 2) { 12 } kkkkk{ 13 }",
ht.highlight("if (1 > 2) 11 else if (1 > 2) { 12 } else { 13 }").mnemonics()
)
}

@Test
fun testForLoop() {
val ht = KotlinHighlighter( styles.filter(listOf(keyword, parenthesis)) )
assertEquals(
"kkkk(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 }",
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)",
ht.highlight("do { 1 + 2 } while (1 > 2)").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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>ki</artifactId>
<version>0.4.5-SNAPSHOT</version>
<version>0.4.5</version>

<packaging>pom</packaging>
<name>Ki Shell :: parent</name>
Expand Down