From 693bc9abc5b0581b732e4b92bb002c14083cad29 Mon Sep 17 00:00:00 2001 From: Vincent Gramer Date: Wed, 23 Sep 2020 21:35:42 +0200 Subject: [PATCH] add fetaure autoclose bace and co (#41) Signed-off-by: Vincent Gramer --- .../ideaplugin/ide/typing/RegoBraceMatcher.kt | 32 +++++ src/main/resources/META-INF/opa-core.xml | 3 + .../ide/typing/RegoBraceMatcherTest.kt | 124 ++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 src/main/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcher.kt create mode 100644 src/test/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcherTest.kt diff --git a/src/main/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcher.kt b/src/main/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcher.kt new file mode 100644 index 0000000..06d7753 --- /dev/null +++ b/src/main/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcher.kt @@ -0,0 +1,32 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.openpolicyagent.ideaplugin.ide.typing + +import com.intellij.lang.BracePair +import com.intellij.lang.PairedBraceMatcher +import com.intellij.psi.PsiFile +import com.intellij.psi.tree.IElementType +import org.openpolicyagent.ideaplugin.lang.psi.RegoTypes + +/** + * Defines the brace matching for Rego language (ie allow to auto close brace, bracket and parenthesis when typed, also + * color matching braces, brackets...) + */ +class RegoBraceMatcher: PairedBraceMatcher { + override fun getPairs() = PAIRS + + override fun isPairedBracesAllowedBeforeType(lbraceType: IElementType, contextType: IElementType?): Boolean = true + + override fun getCodeConstructStart(file: PsiFile, openingBraceOffset: Int): Int = openingBraceOffset + + companion object { + private val PAIRS: Array = arrayOf( + BracePair(RegoTypes.LBRACE, RegoTypes.RBRACE, true), + BracePair(RegoTypes.LBRACK, RegoTypes.RBRACK, false), + BracePair(RegoTypes.LPAREN, RegoTypes.RPAREN, false) + ) + } +} diff --git a/src/main/resources/META-INF/opa-core.xml b/src/main/resources/META-INF/opa-core.xml index e7b57f0..6990194 100644 --- a/src/main/resources/META-INF/opa-core.xml +++ b/src/main/resources/META-INF/opa-core.xml @@ -33,6 +33,9 @@ + + + + """.trimIndent(), + '(', + """ + package main + a := sprinft() + """.trimIndent() + ) + + + fun `test brackets are paired`()= doTest( + """ + package main + a := + """.trimIndent(), + '[', + """ + package main + a := [] + """.trimIndent() + ) + + + fun `test braces are paired`()= doTest( + """ + package main + rule_1 + """.trimIndent(), + '{', + """ + package main + rule_1{} + """.trimIndent() + ) + + fun `test parenthesis deletion`()= doTest( + """ + package main + a := sprinft() + """.trimIndent(), + '\b', + """ + package main + a := sprinft + """.trimIndent() + ) + fun `test brackets deletion`()= doTest( + """ + package main + a := [] + """.trimIndent(), + '\b', + """ + package main + a := + """.trimIndent() + ) + + fun `test braces deletion`()= doTest( + """ + package main + rule_1{} + """.trimIndent(), + '\b', + """ + package main + rule_1 + """.trimIndent() + ) + + fun `test parenthesis match` () = doMatch( + """ + package main + a: = sprintf("hello %v", name[x]) + """.trimIndent(), + ")" + ) + + fun `test brackets match` () = doMatch( + """ + package main + a: = sprintf("hello %v", name[x]) + """.trimIndent(), + "]" + ) + + fun `test braces match` () = doMatch( + """ + package main + rule_1 { + a: = sprintf("hello %v", name[x]) + } + """.trimIndent(), + "}" + ) + private fun doTest(before: String, type: Char, after: String) { + myFixture.configureByText(RegoFileType, before) + myFixture.type(type) + myFixture.checkResult(after) + } + + private fun doMatch(source: String, coBrace: String) { + myFixture.configureByText(RegoFileType, source) + val expected = source.replace("", "").lastIndexOf(coBrace) + check(BraceMatchingUtil.getMatchedBraceOffset(myFixture.editor, true, myFixture.file) == expected) + } + +}