mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-05-07 13:34:02 +02:00
Add float classification functions
This commit is contained in:
parent
d1127a5238
commit
9a5c558060
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
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 IsinfFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for valid integer`() {
|
||||
assertCommandOutput("echo isinf(42)", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for valid float`() {
|
||||
assertCommandOutput("echo isinf(4.2)", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns one for positive infinity`() {
|
||||
assertCommandOutput("echo isinf(1.0/0.0)", "1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns minus one for negative infinity`() {
|
||||
assertCommandOutput("echo isinf(-1.0/0.0)", "-1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for dividing by integer zero`() {
|
||||
assertCommandOutput("echo isinf(0/0) isinf(1/0)", "0 0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for string value`() {
|
||||
assertCommandOutput("echo isinf('42')", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for list`() {
|
||||
assertCommandOutput("echo isinf([1, 2, 3])", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isinf returns false for dictionary`() {
|
||||
assertCommandOutput("echo isinf({1: 1})", "0")
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 IsnanFunctionTest : VimTestCase() {
|
||||
@BeforeEach
|
||||
override fun setUp(testInfo: TestInfo) {
|
||||
super.setUp(testInfo)
|
||||
configureByText("\n")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for valid integer`() {
|
||||
assertCommandOutput("echo isnan(42)", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for valid float`() {
|
||||
assertCommandOutput("echo isnan(4.2)", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns true for NaN`() {
|
||||
assertCommandOutput("echo isnan(0.0/0.0)", "1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for dividing by integer zero`() {
|
||||
assertCommandOutput("echo isnan(0/0)", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for string value`() {
|
||||
assertCommandOutput("echo isnan('42')", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for list`() {
|
||||
assertCommandOutput("echo isnan([1, 2, 3])", "0")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test isnan returns false for dictionary`() {
|
||||
assertCommandOutput("echo isnan({1: 1})", "0")
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 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.VimFloat
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
|
||||
import com.maddyhome.idea.vim.vimscript.model.datatypes.asVimInt
|
||||
import com.maddyhome.idea.vim.vimscript.model.expressions.Expression
|
||||
import com.maddyhome.idea.vim.vimscript.model.functions.FunctionHandler
|
||||
|
||||
@VimscriptFunction(name = "isinf")
|
||||
internal class IsInfFunctionHandler : FunctionHandler() {
|
||||
override val minimumNumberOfArguments = 1
|
||||
override val maximumNumberOfArguments = 1
|
||||
|
||||
override fun doFunction(
|
||||
argumentValues: List<Expression>,
|
||||
editor: VimEditor,
|
||||
context: ExecutionContext,
|
||||
vimContext: VimLContext,
|
||||
): VimDataType {
|
||||
val argument = argumentValues[0].evaluate(editor, context, vimContext)
|
||||
if (argument !is VimInt && argument !is VimFloat) {
|
||||
return false.asVimInt()
|
||||
}
|
||||
return when (argument.asDouble()) {
|
||||
Double.POSITIVE_INFINITY -> VimInt.Companion.ONE
|
||||
Double.NEGATIVE_INFINITY -> VimInt.Companion.MINUS_ONE
|
||||
else -> VimInt.Companion.ZERO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@VimscriptFunction("isnan")
|
||||
internal class IsNanFunctionHandler : FunctionHandler() {
|
||||
override val minimumNumberOfArguments = 1
|
||||
override val maximumNumberOfArguments = 1
|
||||
|
||||
override fun doFunction(
|
||||
argumentValues: List<Expression>,
|
||||
editor: VimEditor,
|
||||
context: ExecutionContext,
|
||||
vimContext: VimLContext,
|
||||
): VimDataType {
|
||||
val argument = argumentValues[0].evaluate(editor, context, vimContext)
|
||||
if (argument !is VimInt && argument !is VimFloat) {
|
||||
return false.asVimInt()
|
||||
}
|
||||
return argument.asDouble().isNaN().asVimInt()
|
||||
}
|
||||
}
|
@ -19,6 +19,8 @@
|
||||
"get": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.GetFunctionHandler",
|
||||
"getcmdtype": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.commandLineFunctions.GetCmdTypeFunctionHandler",
|
||||
"invert": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.bitwiseFunctions.InvertFunctionHandler",
|
||||
"isinf": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.IsInfFunctionHandler",
|
||||
"isnan": "com.maddyhome.idea.vim.vimscript.model.functions.handlers.floatFunctions.IsNanFunctionHandler",
|
||||
"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",
|
||||
|
Loading…
Reference in New Issue
Block a user