Skip to content

Commit

Permalink
Revert "Remove deprecated VimScriptGlobalEnvironment.java"
Browse files Browse the repository at this point in the history
This reverts commit 5c64ebf.
  • Loading branch information
lippfi committed Jan 29, 2024
1 parent e87290a commit 5e01f72
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Object> myVariables = new HashMap<>();

private VimScriptGlobalEnvironment() {
}

public static @NotNull VimScriptGlobalEnvironment getInstance() {
return ourInstance;
}

public @NotNull Map<String, Object> getVariables() {
return myVariables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element?> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e01f72

Please sign in to comment.