1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-05-03 13:34:03 +02:00

Add exponential functions

This commit is contained in:
Matt Ellis 2025-02-19 01:40:10 +00:00 committed by Alex Pláte
parent 6f63371e9b
commit 613a8b5792
5 changed files with 232 additions and 0 deletions
src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/functions/floatFunctions
vim-engine/src/main
kotlin/com/maddyhome/idea/vim/vimscript/model/functions/handlers/floatFunctions
resources/ksp-generated

View File

@ -0,0 +1,65 @@
/*
* Copyright 2003-2025 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.floatFunctions
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
class ExpFunctionTest : VimTestCase() {
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
configureByText("\n")
}
@Test
fun `test exp with Number`() {
assertCommandOutput("echo exp(2)", "7.389056")
}
@Test
fun `test exp with Float`() {
assertCommandOutput("echo exp(-4.01)", "0.018133")
}
@Test
fun `test exp with negative number`() {
assertCommandOutput("echo exp(-1)", "0.367879")
}
@Test
fun `test exp with string causes errors`() {
enterCommand("echo exp('1.0')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test exp with invalid string causes errors`() {
enterCommand("echo exp('cheese')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test exp with list causes errors`() {
enterCommand("echo exp([1.0])")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test exp with dictionary causes errors`() {
enterCommand("echo exp({1: 1.0})")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2003-2025 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.floatFunctions
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
class Log10FunctionTest : VimTestCase() {
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
configureByText("\n")
}
@Test
fun `test log10 with Number`() {
assertCommandOutput("echo log10(1000)", "3.0")
}
@Test
fun `test log10 with Float`() {
assertCommandOutput("echo log10(0.01)", "-2.0")
}
@Test
fun `test log10 with negative number`() {
assertCommandOutput("echo log10(-1)", "nan")
}
@Test
fun `test log10 with string causes errors`() {
enterCommand("echo log10('1.0')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log10 with invalid string causes errors`() {
enterCommand("echo log10('cheese')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log10 with list causes errors`() {
enterCommand("echo log10([1.0])")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log10 with dictionary causes errors`() {
enterCommand("echo log10({1: 1.0})")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2003-2025 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.floatFunctions
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
class LogFunctionTest : VimTestCase() {
@BeforeEach
override fun setUp(testInfo: TestInfo) {
super.setUp(testInfo)
configureByText("\n")
}
@Test
fun `test log with Number`() {
assertCommandOutput("echo log(10)", "2.302585")
}
@Test
fun `test log with Float`() {
assertCommandOutput("echo log(0.1)", "-2.302585")
}
@Test
fun `test log and exp round trips value`() {
assertCommandOutput("echo log(exp(1))", "1.0")
}
@Test
fun `test log with negative number`() {
assertCommandOutput("echo log(-1)", "nan")
}
@Test
fun `test log with string causes errors`() {
enterCommand("echo log('1.0')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log with invalid string causes errors`() {
enterCommand("echo log('cheese')")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log with list causes errors`() {
enterCommand("echo log([1.0])")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
@Test
fun `test log with dictionary causes errors`() {
enterCommand("echo log({1: 1.0})")
assertPluginError(true)
assertPluginErrorMessageContains("E808: Number or Float required")
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2003-2025 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.floatFunctions
import com.intellij.vim.annotations.VimscriptFunction
import kotlin.math.exp
import kotlin.math.ln
import kotlin.math.log10
@VimscriptFunction("exp")
internal class ExpFunctionHandler : UnaryFloatFunctionHandlerBase() {
override fun invoke(argument: Double) = exp(argument)
}
@VimscriptFunction("log")
internal class LogFunctionHandler : UnaryFloatFunctionHandlerBase() {
override fun invoke(argument: Double) = ln(argument)
}
@VimscriptFunction("log10")
internal class Log10FunctionHandler : UnaryFloatFunctionHandlerBase() {
override fun invoke(argument: Double) = log10(argument)
}

View File

@ -12,6 +12,7 @@
"empty": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.EmptyFunctionHandler",
"escape": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.stringFunctions.EscapeFunctionHandler",
"exists": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.variousFunctions.ExistsFunctionHandler",
"exp": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.ExpFunctionHandler",
"floor": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.FloorFunctionHandler",
"funcref": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.varFunctions.FuncrefFunctionHandler",
"function": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.varFunctions.FunctionFunctionHandler",
@ -21,6 +22,8 @@
"join": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.listFunctions.JoinFunctionHandler",
"len": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.LenFunctionHandler",
"line": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.cursorFunctions.LineFunctionHandler",
"log": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.LogFunctionHandler",
"log10": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.Log10FunctionHandler",
"or": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.bitwiseFunctions.OrFunctionHandler",
"round": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.RoundFunctionHandler",
"sin": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.SinFunctionHandler",