1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-08-10 06:40:37 +02:00

Move register variable to its own separate class

This commit is contained in:
Julien Phalip 2024-11-19 21:31:31 -08:00 committed by lippfi
parent 7062dc4860
commit ec3db81c6d
5 changed files with 116 additions and 40 deletions
src/test/java/org/jetbrains/plugins/ideavim/ex/implementation
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript

View File

@ -116,34 +116,4 @@ class VimVariableServiceTest : VimTestCase() {
assertState("1\n2\n1\n2\n1\n2\n1\n2\n${c}1\n")
}
@Test
fun `test v register`() {
configureByText("abcd")
// Check default register
typeText(commandToKeys("echo v:register"))
assertExOutput("\"")
// Check default register in expression
enterCommand(
"""
nnoremap <expr> X ':echo v:register<CR>'
""".trimIndent(),
)
typeText("X")
assertExOutput("\"")
// Check named register
typeText("\"w")
typeText(commandToKeys("echo v:register"))
assertExOutput("w")
// Check named register in expression
// Here the key is that the register is defined as part of the expression
// itself, i.e. it hasn't been committed yet as the current register
enterCommand(
"""
vnoremap <expr> y '"' . v:register . 'y'
""".trimIndent(),
)
typeText("vl\"zy")
val register = injector.registerGroup.getRegisters().filter { reg -> reg.name == 'z' }.first()
assertEquals("ab", register.text)
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package org.jetbrains.plugins.ideavim.ex.implementation.variables
import com.maddyhome.idea.vim.api.injector
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class RegisterVariableTest : VimTestCase() {
@Test
fun `test default v register`() {
configureByText("abcd")
typeText(commandToKeys("echo v:register"))
assertExOutput("\"")
}
@Test
fun `test default v register in expression mapping`() {
configureByText("abcd")
enterCommand("""nnoremap <expr> X ':echo v:register<CR>'""")
typeText("X")
assertExOutput("\"")
}
@Test
fun `test named v register`() {
configureByText("abcd")
typeText("\"w")
typeText(commandToKeys("echo v:register"))
assertExOutput("w")
}
@Test
fun `test named v register in expression mapping`() {
configureByText("abcd")
enterCommand("""vnoremap <expr> y '"' . v:register . 'y'""")
typeText("vl\"zy")
val register = injector.registerGroup.getRegisters()
.filter { reg -> reg.name == 'z' }
.first()
assertEquals("ab", register.text)
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.vimscript.model.variables
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.vimscript.model.VimLContext
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
/**
* The name of the register in effect for the current normal mode
* command (regardless of whether that command actually used a
* register). Or for the currently executing normal mode mapping
* (use this in custom commands that take a register).
* If none is supplied it is the default register '"', unless
* 'clipboard' contains "unnamed" or "unnamedplus", then it is
* "*" or '+' ("unnamedplus" prevails).
*/
class RegisterVariable : Variable() {
override fun evaluate(
name: String,
editor: VimEditor,
context: ExecutionContext,
vimContext: VimLContext,
): VimDataType {
val register = KeyHandler.getInstance().keyHandlerState.commandBuilder.registerSnapshot
?: injector.registerGroup.currentRegister
return VimString(register.toString())
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.vimscript.model.variables
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.vimscript.model.VimLContext
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType
/**
* Base class for Vim variables
*/
abstract class Variable {
abstract fun evaluate(name: String, editor: VimEditor, context: ExecutionContext, vimContext: VimLContext): VimDataType
}

View File

@ -27,6 +27,7 @@ import com.maddyhome.idea.vim.vimscript.model.expressions.Scope
import com.maddyhome.idea.vim.vimscript.model.expressions.Variable
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionDeclaration
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionFlag
import com.maddyhome.idea.vim.vimscript.model.variables.RegisterVariable
abstract class VimVariableServiceBase : VariableService {
private var globalVariables: MutableMap<String, VimDataType> = mutableMapOf()
@ -181,16 +182,7 @@ abstract class VimVariableServiceBase : VariableService {
"count1" -> VimInt(KeyHandler.getInstance().keyHandlerState.commandBuilder.calculateCount0Snapshot().coerceAtLeast(1))
"searchforward" -> VimInt(if (injector.searchGroup.getLastSearchDirection() == Direction.FORWARDS) 1 else 0)
"register" -> {
// The name of the register in effect for the current normal mode
// command (regardless of whether that command actually used a
// register). Or for the currently executing normal mode mapping
// (use this in custom commands that take a register).
// If none is supplied it is the default register '"', unless
// 'clipboard' contains "unnamed" or "unnamedplus", then it is
// "*" or '+' ("unnamedplus" prevails).
val register = KeyHandler.getInstance().keyHandlerState.commandBuilder.registerSnapshot
?: injector.registerGroup.currentRegister
VimString(register.toString())
RegisterVariable().evaluate(name, editor, context, vimContext)
}
else -> throw ExException("The 'v:${name}' variable is not implemented yet")
}