1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-02-28 02:45:59 +01:00

Migrate to effective value change listeners

This commit is contained in:
Matt Ellis 2023-05-01 13:59:36 +01:00 committed by Alex Pláte
parent 54dc184dd0
commit 4e33b5f351
6 changed files with 26 additions and 41 deletions
src
main/java/com/maddyhome/idea/vim
test/java/org/jetbrains/plugins/ideavim

View File

@ -24,8 +24,7 @@ import com.maddyhome.idea.vim.api.*;
import com.maddyhome.idea.vim.helper.*;
import com.maddyhome.idea.vim.newapi.IjVimDocument;
import com.maddyhome.idea.vim.newapi.IjVimEditor;
import com.maddyhome.idea.vim.options.LocalOptionChangeListener;
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt;
import com.maddyhome.idea.vim.options.EffectiveOptionValueChangeListener;
import org.jdom.Element;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
@ -254,7 +253,7 @@ public class EditorGroup implements PersistentStateComponent<Element>, VimEditor
notifyIdeaJoin(((IjVimEditor) editor).getEditor().getProject(), editor);
}
public static class NumberChangeListener implements LocalOptionChangeListener<VimInt> {
public static class NumberChangeListener implements EffectiveOptionValueChangeListener {
public static NumberChangeListener INSTANCE = new NumberChangeListener();
@Contract(pure = true)
@ -262,16 +261,7 @@ public class EditorGroup implements PersistentStateComponent<Element>, VimEditor
}
@Override
public void processGlobalValueChange(@Nullable VimInt oldValue) {
for (Editor editor : HelperKt.localEditors()) {
if (UserDataManager.getVimEditorGroup(editor) && supportsVimLineNumbers(editor)) {
updateLineNumbers(editor);
}
}
}
@Override
public void processLocalValueChange(@Nullable VimInt oldValue, @NotNull VimEditor editor) {
public void onEffectiveValueChanged(@NotNull VimEditor editor) {
Editor ijEditor = ((IjVimEditor)editor).getEditor();
if (UserDataManager.getVimEditorGroup(ijEditor) && supportsVimLineNumbers(ijEditor)) {

View File

@ -24,12 +24,10 @@ import com.maddyhome.idea.vim.helper.ScrollViewHelper
import com.maddyhome.idea.vim.helper.StrictMode
import com.maddyhome.idea.vim.helper.getNormalizedScrollOffset
import com.maddyhome.idea.vim.helper.getNormalizedSideScrollOffset
import com.maddyhome.idea.vim.helper.localEditors
import com.maddyhome.idea.vim.helper.vimEditorGroup
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.options.LocalOptionChangeListener
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
import com.maddyhome.idea.vim.options.EffectiveOptionValueChangeListener
import kotlin.math.abs
import kotlin.math.max
@ -258,16 +256,8 @@ internal class ScrollGroup : VimScrollGroup {
TOP, MIDDLE, BOTTOM
}
object ScrollOptionsChangeListener : LocalOptionChangeListener<VimInt> {
override fun processGlobalValueChange(oldValue: VimInt?) {
for (editor in localEditors()) {
if (editor.vimEditorGroup) {
ScrollViewHelper.scrollCaretIntoView(editor)
}
}
}
override fun processLocalValueChange(oldValue: VimInt?, editor: VimEditor) {
object ScrollOptionsChangeListener : EffectiveOptionValueChangeListener {
override fun onEffectiveValueChanged(editor: VimEditor) {
editor.ij.apply {
if (vimEditorGroup) {
ScrollViewHelper.scrollCaretIntoView(this)

View File

@ -70,6 +70,12 @@ import static com.maddyhome.idea.vim.register.RegisterConstants.LAST_SEARCH_REGI
})
public class SearchGroup extends VimSearchGroupBase implements PersistentStateComponent<Element> {
public SearchGroup() {
// TODO: Investigate migrating these listeners to use the effective value change listener
// This would allow us to update the editor we're told to update, rather than looping over all projects and updating
// the highlights in that project's current document's open editors (see VIM-2779).
// However, we probably only want to update the editors associated with the current document, so maybe the whole
// code needs to be reworked. We're currently using the same update code for changes in the search term as well as
// changes in the search options.
VimPlugin.getOptionGroup().addGlobalOptionChangeListener(Options.hlsearch, () -> {
resetShowSearchHighlight();
forceUpdateSearchHighlights();

View File

@ -13,15 +13,16 @@ import com.intellij.openapi.editor.CaretVisualAttributes
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.VimStateMachine
import com.maddyhome.idea.vim.newapi.ij
import com.maddyhome.idea.vim.newapi.vim
import com.maddyhome.idea.vim.options.OptionChangeListener
import com.maddyhome.idea.vim.options.EffectiveOptionValueChangeListener
import com.maddyhome.idea.vim.options.helpers.GuiCursorMode
import com.maddyhome.idea.vim.options.helpers.GuiCursorOptionHelper
import com.maddyhome.idea.vim.options.helpers.GuiCursorType
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import org.jetbrains.annotations.TestOnly
import java.awt.Color
@ -58,9 +59,9 @@ internal fun Editor.hasBlockOrUnderscoreCaret() = isBlockCursorOverride() ||
it == GuiCursorType.BLOCK || it == GuiCursorType.HOR
}
internal object GuicursorChangeListener : OptionChangeListener<VimString> {
override fun processGlobalValueChange(oldValue: VimString?) {
localEditors().forEach { it.updatePrimaryCaretVisualAttributes() }
internal object GuicursorChangeListener : EffectiveOptionValueChangeListener {
override fun onEffectiveValueChanged(editor: VimEditor) {
editor.ij.updatePrimaryCaretVisualAttributes()
}
}

View File

@ -111,11 +111,11 @@ internal object VimListenerManager {
}
val optionGroup = VimPlugin.getOptionGroup()
optionGroup.addListener(Options.number, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.addListener(Options.relativenumber, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.addListener(Options.scrolloff, ScrollGroup.ScrollOptionsChangeListener)
optionGroup.addEffectiveOptionValueChangeListener(Options.number, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.addEffectiveOptionValueChangeListener(Options.relativenumber, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.addEffectiveOptionValueChangeListener(Options.scrolloff, ScrollGroup.ScrollOptionsChangeListener)
optionGroup.addGlobalOptionChangeListener(Options.showcmd, ShowCmdOptionChangeListener)
optionGroup.addListener(Options.guicursor, GuicursorChangeListener)
optionGroup.addEffectiveOptionValueChangeListener(Options.guicursor, GuicursorChangeListener)
EventFacade.getInstance().addEditorFactoryListener(VimEditorFactoryListener, VimPlugin.getInstance().onOffDisposable)
@ -126,11 +126,11 @@ internal object VimListenerManager {
EventFacade.getInstance().restoreTypedActionHandler()
val optionGroup = VimPlugin.getOptionGroup()
optionGroup.removeListener(Options.number, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.removeListener(Options.relativenumber, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.removeListener(Options.scrolloff, ScrollGroup.ScrollOptionsChangeListener)
optionGroup.removeEffectiveOptionValueChangeListener(Options.number, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.removeEffectiveOptionValueChangeListener(Options.relativenumber, EditorGroup.NumberChangeListener.INSTANCE)
optionGroup.removeEffectiveOptionValueChangeListener(Options.scrolloff, ScrollGroup.ScrollOptionsChangeListener)
optionGroup.removeGlobalOptionChangeListener(Options.showcmd, ShowCmdOptionChangeListener)
optionGroup.removeListener(Options.guicursor, GuicursorChangeListener)
optionGroup.removeEffectiveOptionValueChangeListener(Options.guicursor, GuicursorChangeListener)
}
}

View File

@ -56,7 +56,6 @@ import com.maddyhome.idea.vim.group.GlobalIjOptions
import com.maddyhome.idea.vim.group.visual.VimVisualTimer.swingTimer
import com.maddyhome.idea.vim.handler.isOctopusEnabled
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.GuicursorChangeListener
import com.maddyhome.idea.vim.helper.RunnableHelper.runWriteCommand
import com.maddyhome.idea.vim.helper.TestInputModel
import com.maddyhome.idea.vim.helper.editorMode
@ -129,7 +128,6 @@ abstract class VimTestCase {
VimPlugin.getSearch().resetState()
if (!VimPlugin.isEnabled()) VimPlugin.setEnabled(true)
injector.globalOptions().ideastrictmode = true
GuicursorChangeListener.processGlobalValueChange(null)
Checks.reset()
clearClipboard()