-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fetaure autoclose bace and co (#41)
Signed-off-by: Vincent Gramer <vgramer@gmail.com>
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BracePair> = arrayOf( | ||
BracePair(RegoTypes.LBRACE, RegoTypes.RBRACE, true), | ||
BracePair(RegoTypes.LBRACK, RegoTypes.RBRACK, false), | ||
BracePair(RegoTypes.LPAREN, RegoTypes.RPAREN, false) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/test/kotlin/org/openpolicyagent/ideaplugin/ide/typing/RegoBraceMatcherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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.codeInsight.highlighting.BraceMatchingUtil | ||
import org.openpolicyagent.ideaplugin.OpaTestBase | ||
import org.openpolicyagent.ideaplugin.lang.RegoFileType | ||
|
||
class RegoBraceMatcherTest: OpaTestBase() { | ||
|
||
fun `test parenthesis are paired`()= doTest( | ||
""" | ||
package main | ||
a := sprinft<caret> | ||
""".trimIndent(), | ||
'(', | ||
""" | ||
package main | ||
a := sprinft(<caret>) | ||
""".trimIndent() | ||
) | ||
|
||
|
||
fun `test brackets are paired`()= doTest( | ||
""" | ||
package main | ||
a := <caret> | ||
""".trimIndent(), | ||
'[', | ||
""" | ||
package main | ||
a := [<caret>] | ||
""".trimIndent() | ||
) | ||
|
||
|
||
fun `test braces are paired`()= doTest( | ||
""" | ||
package main | ||
rule_1<caret> | ||
""".trimIndent(), | ||
'{', | ||
""" | ||
package main | ||
rule_1{<caret>} | ||
""".trimIndent() | ||
) | ||
|
||
fun `test parenthesis deletion`()= doTest( | ||
""" | ||
package main | ||
a := sprinft(<caret>) | ||
""".trimIndent(), | ||
'\b', | ||
""" | ||
package main | ||
a := sprinft<caret> | ||
""".trimIndent() | ||
) | ||
fun `test brackets deletion`()= doTest( | ||
""" | ||
package main | ||
a := [<caret>] | ||
""".trimIndent(), | ||
'\b', | ||
""" | ||
package main | ||
a := <caret> | ||
""".trimIndent() | ||
) | ||
|
||
fun `test braces deletion`()= doTest( | ||
""" | ||
package main | ||
rule_1{<caret>} | ||
""".trimIndent(), | ||
'\b', | ||
""" | ||
package main | ||
rule_1<caret> | ||
""".trimIndent() | ||
) | ||
|
||
fun `test parenthesis match` () = doMatch( | ||
""" | ||
package main | ||
a: = sprintf<caret>("hello %v", name[x]) | ||
""".trimIndent(), | ||
")" | ||
) | ||
|
||
fun `test brackets match` () = doMatch( | ||
""" | ||
package main | ||
a: = sprintf("hello %v", name<caret>[x]) | ||
""".trimIndent(), | ||
"]" | ||
) | ||
|
||
fun `test braces match` () = doMatch( | ||
""" | ||
package main | ||
rule_1 <caret>{ | ||
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("<caret>", "").lastIndexOf(coBrace) | ||
check(BraceMatchingUtil.getMatchedBraceOffset(myFixture.editor, true, myFixture.file) == expected) | ||
} | ||
|
||
} |