1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-08-14 17:17:07 +02:00

Faster command parser

This commit is contained in:
Alex Plate
2019-11-07 12:15:43 +03:00
parent ae31b1c23d
commit eba9eaf37a
2 changed files with 10 additions and 26 deletions
src/com/maddyhome/idea/vim/ex
test/org/jetbrains/plugins/ideavim/ex

@@ -21,8 +21,9 @@ package com.maddyhome.idea.vim.ex
data class CommandName(val required: String, val optional: String = "")
fun commands(vararg commands: String) = commands.map { command ->
commandPattern.matchEntire(command)?.groupValues?.let { CommandName(it[1], it[2]) }
?: throw RuntimeException("$command is invalid!")
val openBracketIndex = command.indexOf('[')
if (openBracketIndex < 0) CommandName(command)
else CommandName(command.take(openBracketIndex), command.substring(openBracketIndex + 1, command.lastIndex))
}.toTypedArray()
fun flags(
@@ -31,5 +32,3 @@ fun flags(
access: CommandHandler.Access,
vararg flags: CommandHandler.Flag
) = CommandHandlerFlags(rangeFlag, argumentFlag, access, flags.toSet())
private val commandPattern: Regex = "^([^\\[]+)(?:\\[([^]]+)])?\$".toRegex()

@@ -20,20 +20,22 @@ package org.jetbrains.plugins.ideavim.ex
import com.maddyhome.idea.vim.ex.commands
import junit.framework.TestCase
import org.jetbrains.plugins.ideavim.VimTestCase
import org.jetbrains.plugins.ideavim.VimTestCase.assertThrows
import org.junit.Test
import kotlin.test.assertEquals
/**
* @author Alex Plate
*/
class CommandParserTest : VimTestCase() {
class CommandParserTest {
@Test
fun `test one letter without optional`() {
val commands = commands("a")
TestCase.assertEquals(1, commands.size)
assertEquals(1, commands.size)
assertEquals("a", commands[0].required)
assertEquals("", commands[0].optional)
}
@Test
fun `test without optional`() {
val commands = commands("a_discovery")
TestCase.assertEquals(1, commands.size)
@@ -41,24 +43,7 @@ class CommandParserTest : VimTestCase() {
assertEquals("", commands[0].optional)
}
fun `test empty optional`() {
assertThrows<RuntimeException>(RuntimeException::class.java) {
commands("a_discovery[]")
}
}
fun `test empty`() {
assertThrows<RuntimeException>(RuntimeException::class.java) {
commands("")
}
}
fun `test no closing bracket`() {
assertThrows<RuntimeException>(RuntimeException::class.java) {
commands("a_discovery[")
}
}
@Test
fun `test with optional`() {
val commands = commands("a[discovery]")
TestCase.assertEquals(1, commands.size)