diff --git a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/GetCmdTypeFunctionTest.kt b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/GetCmdTypeFunctionTest.kt new file mode 100644 index 0000000000..9886f91a41 --- /dev/null +++ b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/GetCmdTypeFunctionTest.kt @@ -0,0 +1,51 @@ +/* + * 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.functions + +import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.ui.ex.ExEntryPanel +import org.jetbrains.plugins.ideavim.VimTestCase +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class GetCmdTypeFunctionTest : VimTestCase() { + + @Test + fun `test getcmdtype() for a regular command`() { + configureByText("\n") + enterCommand("cmap z getcmdtype()") + typeText(":fooz") + assertEquals("foo:", (injector.commandLine.getActiveCommandLine() as ExEntryPanel).visibleText) + } + + @Test + fun `test getcmdtype() for a forward search`() { + configureByText("\n") + enterCommand("cmap z getcmdtype()") + typeText("/fooz") + assertEquals("foo/", (injector.commandLine.getActiveCommandLine() as ExEntryPanel).visibleText) + } + + @Test + fun `test getcmdtype() for a backward search`() { + configureByText("\n") + enterCommand("cmap z getcmdtype()") + typeText("?fooz") + assertEquals("foo?", (injector.commandLine.getActiveCommandLine() as ExEntryPanel).visibleText) + } + + @Test + fun `test getcmdtype() for an expression command`() { + configureByText("\n") + enterCommand("cmap z getcmdtype()") + typeText("i=fooz") + assertEquals("foo=", (injector.commandLine.getActiveCommandLine() as ExEntryPanel).visibleText) + } + +} \ No newline at end of file diff --git a/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/GetCmdTypeFunctionHandler.kt b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/GetCmdTypeFunctionHandler.kt new file mode 100644 index 0000000000..d6ec77a937 --- /dev/null +++ b/vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/GetCmdTypeFunctionHandler.kt @@ -0,0 +1,54 @@ +/* + * 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.functions.handlers + +import com.intellij.vim.annotations.VimscriptFunction +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.state.mode.Mode +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 +import com.maddyhome.idea.vim.vimscript.model.expressions.Expression +import com.maddyhome.idea.vim.vimscript.model.functions.FunctionHandler + +/* +Return the current command-line type. Possible return values are: + : normal Ex command + / forward search command + ? backward search command + = i_CTRL-R_= + +Returns an empty string otherwise. + +Not yet implemented: + > debug mode command debug-mode + @ input() command + - :insert or :append command + */ +@VimscriptFunction(name = "getcmdtype") +internal class GetCmdTypeFunctionHandler : FunctionHandler() { + override val minimumNumberOfArguments = 0 + override val maximumNumberOfArguments = 0 + + override fun doFunction( + argumentValues: List, + editor: VimEditor, + context: ExecutionContext, + vimContext: VimLContext, + ): VimDataType { + val mode = editor.mode + return when (mode) { + is Mode.CMD_LINE -> VimString(injector.commandLine.getActiveCommandLine()?.label ?: "") + else -> VimString("") + } + } + +} diff --git a/vim-engine/src/main/resources/ksp-generated/engine_vimscript_functions.json b/vim-engine/src/main/resources/ksp-generated/engine_vimscript_functions.json index ff5db29720..9631f4e1dd 100644 --- a/vim-engine/src/main/resources/ksp-generated/engine_vimscript_functions.json +++ b/vim-engine/src/main/resources/ksp-generated/engine_vimscript_functions.json @@ -5,6 +5,7 @@ "funcref": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.FuncrefFunctionHandler", "function": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.FunctionFunctionHandler", "get": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.GetFunctionHandler", + "getcmdtype": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.GetCmdTypeFunctionHandler", "join": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.JoinFunctionHandler", "len": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.LenFunctionHandler", "sin": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.SinFunctionHandler",