mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-03-03 18:32:54 +01:00
Much better sublists
This commit is contained in:
parent
165caab526
commit
13ab68e5b1
src/com/maddyhome/idea/vim/vimscript/model/expressions
test/org/jetbrains/plugins/ideavim/ex/implementation/expressions
@ -24,6 +24,8 @@ import com.maddyhome.idea.vim.ex.ExException
|
||||
import com.maddyhome.idea.vim.vimscript.model.VimContext
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDictionary
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimList
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
|
||||
|
||||
data class OneElementSublistExpression(val index: Expression, val expression: Expression) : Expression() {
|
||||
@ -38,7 +40,14 @@ data class OneElementSublistExpression(val index: Expression, val expression: Ex
|
||||
}\""
|
||||
)
|
||||
} else {
|
||||
return SublistExpression(index, index, expression).evaluate(editor, context, vimContext)
|
||||
val indexValue = Integer.parseInt(index.evaluate(editor, context, vimContext).asString())
|
||||
if (expressionValue is VimList && (indexValue >= expressionValue.values.size || indexValue < expressionValue.values.size)) {
|
||||
throw ExException("E684: list index out of range: $indexValue")
|
||||
}
|
||||
if (indexValue < 0) {
|
||||
return VimString("")
|
||||
}
|
||||
return SublistExpression(SimpleExpression(VimInt(indexValue)), SimpleExpression(VimInt(indexValue)), SimpleExpression(expressionValue)).evaluate(editor, context, vimContext)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,14 +27,24 @@ data class SublistExpression(val from: Expression?, val to: Expression?, val exp
|
||||
toInt += arraySize
|
||||
}
|
||||
return if (expressionValue is VimList) {
|
||||
if (fromInt <= toInt) {
|
||||
if (fromInt > arraySize) {
|
||||
VimList(mutableListOf())
|
||||
} else if (fromInt == toInt) {
|
||||
expressionValue.values[fromInt]
|
||||
} else if (fromInt <= toInt) {
|
||||
VimList(expressionValue.values.subList(fromInt, toInt + 1))
|
||||
} else {
|
||||
VimList(mutableListOf())
|
||||
}
|
||||
} else {
|
||||
if (fromInt <= toInt) {
|
||||
VimString(expressionValue.asString().substring(fromInt, toInt + 1))
|
||||
if (fromInt > arraySize) {
|
||||
VimString("")
|
||||
} else if (fromInt <= toInt) {
|
||||
if (toInt > expressionValue.asString().length - 1) {
|
||||
VimString(expressionValue.asString().substring(fromInt))
|
||||
} else {
|
||||
VimString(expressionValue.asString().substring(fromInt, toInt + 1))
|
||||
}
|
||||
} else {
|
||||
VimString("")
|
||||
}
|
||||
|
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
|
||||
* Copyright (C) 2003-2021 The IdeaVim authors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.jetbrains.plugins.ideavim.ex.implementation.expressions
|
||||
|
||||
import org.jetbrains.plugins.ideavim.VimTestCase
|
||||
|
||||
class SublistExpressionTest : VimTestCase() {
|
||||
|
||||
fun `test strung sublist`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[0:1]"))
|
||||
assertExOutput("ab\n")
|
||||
}
|
||||
|
||||
fun `test negative index with sting`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[-1]"))
|
||||
assertExOutput("\n")
|
||||
}
|
||||
|
||||
fun `test index greater than size with string`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[1000]"))
|
||||
assertExOutput("\n")
|
||||
}
|
||||
|
||||
fun `test negative index with list`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo [1, 2][-1]"))
|
||||
assertPluginErrorMessageContains("E684: list index out of range: -1")
|
||||
}
|
||||
|
||||
fun `test index greater than size with list`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo [1, 2][1000]"))
|
||||
assertPluginErrorMessageContains("E684: list index out of range: 1000")
|
||||
}
|
||||
|
||||
fun `test negative first index`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[-1:]"))
|
||||
assertExOutput("c\n")
|
||||
}
|
||||
|
||||
fun `test negative last index`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[0:-2]"))
|
||||
assertExOutput("ab\n")
|
||||
}
|
||||
|
||||
fun `test negative last index2`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[0:-1]"))
|
||||
assertExOutput("abc\n")
|
||||
}
|
||||
|
||||
fun `test last index bigger sting size`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[1:10000]"))
|
||||
assertExOutput("bc\n")
|
||||
}
|
||||
|
||||
fun `test both indexes bigger sting size`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[100:10000]"))
|
||||
assertExOutput("\n")
|
||||
}
|
||||
|
||||
fun `test first index is bigger than second`() {
|
||||
configureByText("\n")
|
||||
typeText(commandToKeys("echo 'abc'[100:10]"))
|
||||
assertExOutput("\n")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user