-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for escape() vim function
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/EscapeFunctionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 org.jetbrains.plugins.ideavim.VimTestCase | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
class EscapeFunctionTest : VimTestCase() { | ||
@Test | ||
fun `test escape windows path with spaces`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('c:\program files\vim', ' \')""")) | ||
assertExOutput("""c:\\program\ files\\vim""") | ||
} | ||
|
||
@Test | ||
fun `test escape multiple special characters`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('special chars: #$^', '#$^')""")) | ||
assertExOutput("""special chars: \#\$\^""") | ||
} | ||
|
||
@Test | ||
fun `test escape when no escaping needed`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('no escaping needed', 'xyz')""")) | ||
assertExOutput("no escaping needed") | ||
} | ||
|
||
@Test | ||
fun `test escape empty strings`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('', '')""")) | ||
assertExOutput("") | ||
} | ||
|
||
@Test | ||
fun `test escape consecutive special characters`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('$$$$', '$')""")) | ||
assertExOutput("""\$\$\$\$""") | ||
} | ||
|
||
@Test | ||
fun `test escape with double backslashes`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('test\\here', '\\')""")) | ||
assertExOutput("""test\\\\here""") | ||
} | ||
|
||
@Test | ||
fun `test escape with unicode characters`() { | ||
configureByText("\n") | ||
typeText(commandToKeys("""echo escape('Hello 👋 #world', '#')""")) | ||
assertExOutput("""Hello 👋 \#world""") | ||
|
||
typeText(commandToKeys("""echo escape('🎉$🎊$', '$')""")) | ||
assertExOutput("""🎉\$🎊\$""") | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/EscapeFunctionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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.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 | ||
|
||
/** | ||
* Implementation of Vim's escape() function. | ||
* Escapes characters specified in the second argument that occur in the first | ||
* argument with a backslash. | ||
* Example: escape('c:\program files\vim', ' \') returns 'c:\\program\ files\\vim' | ||
*/ | ||
@VimscriptFunction(name = "escape") | ||
internal class EscapeFunctionHandler : FunctionHandler() { | ||
override val minimumNumberOfArguments = 2 | ||
override val maximumNumberOfArguments = 2 | ||
|
||
override fun doFunction( | ||
argumentValues: List<Expression>, | ||
editor: VimEditor, | ||
context: ExecutionContext, | ||
vimContext: VimLContext, | ||
): VimDataType { | ||
// Get the input string and characters to escape | ||
val string = argumentValues[0].evaluate(editor, context, vimContext).asString() | ||
val charsToEscape = argumentValues[1].evaluate(editor, context, vimContext).asString() | ||
|
||
// Process each character in the input string | ||
val result = StringBuilder() | ||
for (c in string) { | ||
// If the current character should be escaped, add a backslash before it | ||
if (c in charsToEscape) { | ||
result.append('\\') | ||
} | ||
result.append(c) | ||
} | ||
|
||
return VimString(result.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters