Phase 2: commenter and brace matcher
Brace matching pairs the Jai-specific single-token openers .{ and .[ with the
ordinary } and ]; verified headlessly through the editor highlighter.
This commit is contained in:
60
src/test/kotlin/dev/hgh/jai/editor/JaiEditorSupportTest.kt
Normal file
60
src/test/kotlin/dev/hgh/jai/editor/JaiEditorSupportTest.kt
Normal file
@@ -0,0 +1,60 @@
|
||||
package dev.hgh.jai.editor
|
||||
|
||||
import com.intellij.codeInsight.highlighting.BraceMatchingUtil
|
||||
import com.intellij.lang.LanguageBraceMatching
|
||||
import com.intellij.lang.LanguageCommenters
|
||||
import com.intellij.openapi.editor.ex.EditorEx
|
||||
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||
import dev.hgh.jai.JaiFileType
|
||||
import dev.hgh.jai.JaiLanguage
|
||||
|
||||
class JaiEditorSupportTest : BasePlatformTestCase() {
|
||||
fun testCommenterIsRegistered() {
|
||||
val commenter = LanguageCommenters.INSTANCE.forLanguage(JaiLanguage)
|
||||
assertTrue("expected JaiCommenter, got $commenter", commenter is JaiCommenter)
|
||||
assertEquals("//", commenter.lineCommentPrefix)
|
||||
assertEquals("/*", commenter.blockCommentPrefix)
|
||||
assertEquals("*/", commenter.blockCommentSuffix)
|
||||
}
|
||||
|
||||
fun testBraceMatcherIsRegistered() {
|
||||
val matcher = LanguageBraceMatching.INSTANCE.forLanguage(JaiLanguage)
|
||||
assertTrue("expected JaiBraceMatcher, got $matcher", matcher is JaiBraceMatcher)
|
||||
}
|
||||
|
||||
fun testMatchesOrdinaryBraces() {
|
||||
assertMatchForward("main :: () |{ x := 1; }", "main :: () { x := 1; |}")
|
||||
}
|
||||
|
||||
fun testMatchesStructLiteralBrace() {
|
||||
// `.{` is one token; it still has to pair with the ordinary closing brace.
|
||||
assertMatchForward("p := Vector2.|{ 1, 2 };", "p := Vector2.{ 1, 2 |};")
|
||||
}
|
||||
|
||||
fun testMatchesArrayLiteralBracket() {
|
||||
assertMatchForward("names := .|[ \"a\", \"b\" ];", "names := .[ \"a\", \"b\" |];")
|
||||
}
|
||||
|
||||
/**
|
||||
* Both arguments are the same text with a `|` marking the caret; the first marks the
|
||||
* opening brace, the second the offset the matcher must jump to.
|
||||
*/
|
||||
private fun assertMatchForward(
|
||||
withOpen: String,
|
||||
withClose: String,
|
||||
) {
|
||||
val open = withOpen.indexOf('|')
|
||||
val close = withClose.indexOf('|')
|
||||
val text = withOpen.replace("|", "")
|
||||
assertEquals("the two markers must describe the same text", text, withClose.replace("|", ""))
|
||||
|
||||
myFixture.configureByText("braces.jai", text)
|
||||
val editor = myFixture.editor as EditorEx
|
||||
val iterator = editor.highlighter.createIterator(open)
|
||||
assertTrue(
|
||||
"no forward match found for the brace at $open in $withOpen",
|
||||
BraceMatchingUtil.matchBrace(editor.document.charsSequence, JaiFileType, iterator, true),
|
||||
)
|
||||
assertEquals(close, iterator.start)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user