diff --git a/src/main/java/com/maddyhome/idea/vim/ex/vimscript/VimScriptGlobalEnvironment.java b/src/main/java/com/maddyhome/idea/vim/ex/vimscript/VimScriptGlobalEnvironment.java new file mode 100644 index 0000000000..de2db6fe90 --- /dev/null +++ b/src/main/java/com/maddyhome/idea/vim/ex/vimscript/VimScriptGlobalEnvironment.java @@ -0,0 +1,38 @@ +/* + * Copyright 2003-2023 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.ex.vimscript; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author vlan + */ + +@Deprecated() // please use VariableService instead +@ApiStatus.ScheduledForRemoval(inVersion = "1.12") +public class VimScriptGlobalEnvironment { + private static final VimScriptGlobalEnvironment ourInstance = new VimScriptGlobalEnvironment(); + + private final Map myVariables = new HashMap<>(); + + private VimScriptGlobalEnvironment() { + } + + public static @NotNull VimScriptGlobalEnvironment getInstance() { + return ourInstance; + } + + public @NotNull Map getVariables() { + return myVariables; + } +} diff --git a/src/main/java/com/maddyhome/idea/vim/vimscript/services/IjVariableService.kt b/src/main/java/com/maddyhome/idea/vim/vimscript/services/IjVariableService.kt index a33cf6e1af..1f5369b8f4 100644 --- a/src/main/java/com/maddyhome/idea/vim/vimscript/services/IjVariableService.kt +++ b/src/main/java/com/maddyhome/idea/vim/vimscript/services/IjVariableService.kt @@ -12,12 +12,48 @@ import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.RoamingType import com.intellij.openapi.components.State import com.intellij.openapi.components.Storage +import com.maddyhome.idea.vim.api.ExecutionContext +import com.maddyhome.idea.vim.api.VimEditor +import com.maddyhome.idea.vim.ex.vimscript.VimScriptGlobalEnvironment +import com.maddyhome.idea.vim.vimscript.model.VimLContext +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimBlob +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDictionary +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimFloat +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimFuncref import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt +import com.maddyhome.idea.vim.vimscript.model.datatypes.VimList import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString +import com.maddyhome.idea.vim.vimscript.model.expressions.Scope +import com.maddyhome.idea.vim.vimscript.model.expressions.Variable import org.jdom.Element @State(name = "VimVariables", storages = [Storage(value = "\$APP_CONFIG$/vim_settings_local.xml", roamingType = RoamingType.DISABLED)]) internal class IjVariableService : VimVariableServiceBase(), PersistentStateComponent { + override fun storeVariable(variable: Variable, value: VimDataType, editor: VimEditor, context: ExecutionContext, vimContext: VimLContext) { + super.storeVariable(variable, value, editor, context, vimContext) + + val scope = variable.scope ?: getDefaultVariableScope(vimContext) + if (scope == Scope.GLOBAL_VARIABLE) { + val scopeForGlobalEnvironment = variable.scope?.toString() ?: "" + VimScriptGlobalEnvironment.getInstance() + .variables[scopeForGlobalEnvironment + variable.name.evaluate(editor, context, vimContext)] = value.simplify() + } + } + + private fun VimDataType.simplify(): Any { + return when (this) { + is VimString -> this.value + is VimInt -> this.value + is VimFloat -> this.value + is VimList -> this.values + is VimDictionary -> this.dictionary + is VimBlob -> "blob" + is VimFuncref -> "funcref" + else -> error("Unexpected") + } + } + override fun getState(): Element { val element = Element("variables") saveData(element) diff --git a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/LetCommandTest.kt b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/LetCommandTest.kt index 0732070f91..6b83a196e0 100644 --- a/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/LetCommandTest.kt +++ b/src/test/java/org/jetbrains/plugins/ideavim/ex/implementation/commands/LetCommandTest.kt @@ -10,6 +10,7 @@ package org.jetbrains.plugins.ideavim.ex.implementation.commands import com.maddyhome.idea.vim.api.Options import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.ex.vimscript.VimScriptGlobalEnvironment import com.maddyhome.idea.vim.newapi.vim import com.maddyhome.idea.vim.options.OptionAccessScope import org.jetbrains.plugins.ideavim.SkipNeovimReason @@ -221,6 +222,7 @@ class LetCommandTest : VimTestCase() { configureByText("\n") enterCommand("let g:WhichKey_ShowVimActions = \"true\"") assertCommandOutput("echo g:WhichKey_ShowVimActions", "true\n") + assertEquals("true", VimScriptGlobalEnvironment.getInstance().variables["g:WhichKey_ShowVimActions"]) } @Test