From 1a9fe2c4025e6570462e6d3526a9609a943053b3 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 14 Nov 2024 15:09:21 -0800 Subject: [PATCH 1/3] Add partial support for getcmdtype() function Tests not included --- .../handlers/GetCmdTypeFunctionHandler.kt | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/GetCmdTypeFunctionHandler.kt 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..09faa7c615 --- /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 + > debug mode command debug-mode + / forward search command + ? backward search command + = i_CTRL-R_= + +Returns an empty string otherwise. + +Not yet implemented: + @ 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("") + } + } + +} From 15692457502a9717bf034ab417a7fb2c97ab07b8 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 14 Nov 2024 15:20:44 -0800 Subject: [PATCH 2/3] Add JSON entry for getcmdtype function --- .../main/resources/ksp-generated/engine_vimscript_functions.json | 1 + 1 file changed, 1 insertion(+) 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", From 1c45f1ca53f01fdc1a97b2dcad96f2523d944840 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Tue, 19 Nov 2024 20:43:09 -0800 Subject: [PATCH 3/3] Add some tests for the getcmdtype() function --- .../functions/GetCmdTypeFunctionTest.kt | 51 +++++++++++++++++++ .../handlers/GetCmdTypeFunctionHandler.kt | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/GetCmdTypeFunctionTest.kt 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 index 09faa7c615..d6ec77a937 100644 --- 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 @@ -22,7 +22,6 @@ import com.maddyhome.idea.vim.vimscript.model.functions.FunctionHandler /* Return the current command-line type. Possible return values are: : normal Ex command - > debug mode command debug-mode / forward search command ? backward search command = i_CTRL-R_= @@ -30,6 +29,7 @@ Return the current command-line type. Possible return values are: Returns an empty string otherwise. Not yet implemented: + > debug mode command debug-mode @ input() command - :insert or :append command */