Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/option delegated properties #616

Merged
5 changes: 2 additions & 3 deletions src/main/java/com/maddyhome/idea/vim/PluginStartup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManagerListener
import com.intellij.openapi.startup.StartupActivity
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.localEditors
import com.maddyhome.idea.vim.newapi.globalIjOptions

/**
* @author Alex Plate
Expand All @@ -42,7 +41,7 @@ internal class PluginStartup : StartupActivity.DumbAware/*, LightEditCompatible*
// This is a temporal workaround for VIM-2487
internal class PyNotebooksCloseWorkaround : ProjectManagerListener {
override fun projectClosingBeforeSave(project: Project) {
if (injector.globalOptions().isSet(IjOptions.closenotebooks)) {
if (injector.globalIjOptions().closenotebooks) {
localEditors().forEach { editor ->
val virtualFile = EditorHelper.getVirtualFile(editor)
if (virtualFile?.extension == "ipynb") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.intellij.openapi.editor.actionSystem.ActionPlan
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
import com.intellij.openapi.editor.actionSystem.TypedActionHandlerEx
import com.intellij.openapi.progress.ProcessCanceledException
import com.maddyhome.idea.vim.api.Options
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.helper.inInsertMode
Expand All @@ -32,7 +31,7 @@ import javax.swing.KeyStroke
*/
internal class VimTypedActionHandler(origHandler: TypedActionHandler) : TypedActionHandlerEx {
private val handler = KeyHandler.getInstance()
private val traceTime = injector.globalOptions().isSet(Options.ideatracetime)
private val traceTime = injector.globalOptions().ideatracetime

init {
KeyHandlerKeeper.getInstance().originalHandler = origHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import com.intellij.openapi.util.Key
import com.intellij.ui.KeyStrokeAdapter
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.Options
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.options
import com.maddyhome.idea.vim.group.IjOptionConstants
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.handler.enableOctopus
Expand All @@ -44,6 +42,7 @@ import com.maddyhome.idea.vim.key.ShortcutOwner
import com.maddyhome.idea.vim.key.ShortcutOwnerInfo
import com.maddyhome.idea.vim.listener.AceJumpService
import com.maddyhome.idea.vim.listener.AppCodeTemplates.appCodeTemplateCaptured
import com.maddyhome.idea.vim.newapi.globalIjOptions
import com.maddyhome.idea.vim.newapi.vim
import java.awt.event.InputEvent
import java.awt.event.KeyEvent
Expand All @@ -56,7 +55,7 @@ import javax.swing.KeyStroke
* These keys are not passed to [com.maddyhome.idea.vim.VimTypedActionHandler] and should be handled by actions.
*/
internal class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatible*/ {
private val traceTime = injector.globalOptions().isSet(Options.ideatracetime)
private val traceTime = injector.globalOptions().ideatracetime

override fun actionPerformed(e: AnActionEvent) {
LOG.trace("Executing shortcut key action")
Expand Down Expand Up @@ -211,7 +210,7 @@ internal class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatib
}

private fun isEnabledForEscape(editor: Editor): Boolean {
val ideaVimSupportDialog = injector.options(editor.vim).hasValue(IjOptions.ideavimsupport, IjOptionConstants.ideavimsupport_dialog)
val ideaVimSupportDialog = injector.globalIjOptions().ideavimsupport.contains(IjOptionConstants.ideavimsupport_dialog)
return editor.isPrimaryEditor() ||
EditorHelper.isFileEditor(editor) && !editor.inNormalMode ||
ideaVimSupportDialog && !editor.inNormalMode
Expand Down Expand Up @@ -264,7 +263,7 @@ internal class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatib

fun isEnabledForLookup(keyStroke: KeyStroke): Boolean = keyStroke !in parsedLookupKeys

private fun parseLookupKeys() = injector.globalOptions().getStringListValues(IjOptions.lookupkeys)
private fun parseLookupKeys() = injector.globalIjOptions().lookupkeys
.map { injector.parser.parseKeys(it) }
.filter { it.isNotEmpty() }
.map { it.first() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.options
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler
import com.maddyhome.idea.vim.newapi.ijOptions

public class DeleteJoinLinesAction : ChangeEditorActionHandler.ConditionalSingleExecution() {
override val type: Command.Type = Command.Type.DELETE
Expand All @@ -26,7 +25,7 @@ public class DeleteJoinLinesAction : ChangeEditorActionHandler.ConditionalSingle
cmd: Command,
operatorArguments: OperatorArguments,
): Boolean {
return !injector.options(editor).isSet(IjOptions.ideajoin)
return !injector.ijOptions(editor).ideajoin
}

override fun execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ package com.maddyhome.idea.vim.action.change.delete
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.options
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler
import com.maddyhome.idea.vim.newapi.ijOptions

public class DeleteJoinLinesSpacesAction : ChangeEditorActionHandler.SingleExecution() {
override val type: Command.Type = Command.Type.DELETE
Expand All @@ -27,7 +26,7 @@ public class DeleteJoinLinesSpacesAction : ChangeEditorActionHandler.SingleExecu
operatorArguments: OperatorArguments,
): Boolean {
if (editor.isOneLineMode()) return false
if (injector.options(editor).isSet(IjOptions.ideajoin)) {
if (injector.ijOptions(editor).ideajoin) {
return injector.changeGroup.joinViaIdeaByCount(editor, context, operatorArguments.count1)
}
injector.editorGroup.notifyIdeaJoin(editor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.options
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.handler.VisualOperatorActionHandler
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.newapi.ijOptions
import java.util.*

/**
Expand All @@ -37,7 +36,7 @@ public class DeleteJoinVisualLinesAction : VisualOperatorActionHandler.SingleExe
operatorArguments: OperatorArguments,
): Boolean {
if (editor.isOneLineMode()) return false
if (injector.options(editor).isSet(IjOptions.ideajoin)) {
if (injector.ijOptions(editor).ideajoin) {
injector.changeGroup.joinViaIdeaBySelections(editor, context, caretsAndSelections)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimCaret
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.options
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.OperatorArguments
import com.maddyhome.idea.vim.group.IjOptions
import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.handler.VisualOperatorActionHandler
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.newapi.ijOptions
import java.util.*

/**
Expand All @@ -37,7 +36,7 @@ public class DeleteJoinVisualLinesSpacesAction : VisualOperatorActionHandler.Sin
operatorArguments: OperatorArguments,
): Boolean {
if (editor.isOneLineMode()) return false
if (injector.options(editor).isSet(IjOptions.ideajoin)) {
if (injector.ijOptions(editor).ideajoin) {
injector.changeGroup.joinViaIdeaBySelections(editor, context, caretsAndSelections)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.intellij.openapi.extensions.ExtensionPointListener
import com.intellij.openapi.extensions.PluginDescriptor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.VimExtensionRegistrator
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.api.setToggleOption
import com.maddyhome.idea.vim.key.MappingOwner.Plugin.Companion.remove
Expand Down Expand Up @@ -65,7 +64,7 @@ internal object VimExtensionRegistrar : VimExtensionRegistrator {
option,
object : OptionChangeListener<VimInt> {
override fun processGlobalValueChange(oldValue: VimInt?) {
if (injector.globalOptions().isSet(option)) {
if (injector.optionGroup.getOptionValue(option, OptionScope.GLOBAL).asBoolean()) {
initExtension(extensionBean, name)
PluginState.enabledExtensions.add(name)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.intellij.openapi.util.NlsSafe
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.Options
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.getText
import com.maddyhome.idea.vim.api.injector
Expand Down Expand Up @@ -319,7 +318,7 @@ internal class VimMultipleCursorsExtension : VimExtension {

private fun findNextOccurrence(editor: Editor, startOffset: Int, text: String, whole: Boolean): Int {
val searchOptions = enumSetOf(SearchOptions.WHOLE_FILE)
if (injector.options(editor.vim).isSet(Options.wrapscan)) {
if (injector.options(editor.vim).wrapscan) {
searchOptions.add(SearchOptions.WRAP)
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/maddyhome/idea/vim/group/ChangeGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void type(@NotNull VimEditor vimEditor, @NotNull ExecutionContext context
*/
@Override
public boolean changeCaseToggleCharacter(@NotNull VimEditor editor, @NotNull VimCaret caret, int count) {
boolean allowWrap = options(injector, editor).hasValue(Options.whichwrap, "~");
boolean allowWrap = options(injector, editor).getWhichwrap().contains("~");

Motion motion = injector.getMotion().getHorizontalMotion(editor, caret, count, true, allowWrap);
if (motion instanceof Motion.Error) return false;
Expand Down Expand Up @@ -571,7 +571,7 @@ public boolean changeNumberVisualMode(final @NotNull VimEditor editor,
}
}

List<String> nf = options(injector, editor).getStringListValues(Options.nrformats);
List<String> nf = options(injector, editor).getNrformats();
boolean alpha = nf.contains("alpha");
boolean hex = nf.contains("hex");
boolean octal = nf.contains("octal");
Expand Down Expand Up @@ -601,7 +601,7 @@ public boolean changeNumberVisualMode(final @NotNull VimEditor editor,

@Override
public boolean changeNumber(final @NotNull VimEditor editor, @NotNull VimCaret caret, final int count) {
final List<String> nf = options(injector, editor).getStringListValues(Options.nrformats);
final List<String> nf = options(injector, editor).getNrformats();
final boolean alpha = nf.contains("alpha");
final boolean hex = nf.contains("hex");
final boolean octal = nf.contains("octal");
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/com/maddyhome/idea/vim/group/EditorGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@
import com.intellij.openapi.project.Project;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.api.ExecutionContext;
import com.maddyhome.idea.vim.api.Options;
import com.maddyhome.idea.vim.api.VimEditor;
import com.maddyhome.idea.vim.api.VimEditorGroup;
import com.maddyhome.idea.vim.api.*;
import com.maddyhome.idea.vim.helper.*;
import com.maddyhome.idea.vim.newapi.IjVimEditor;
import com.maddyhome.idea.vim.options.LocalOptionChangeListener;
import com.maddyhome.idea.vim.options.OptionValueAccessor;
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt;
import org.jdom.Element;
import org.jetbrains.annotations.Contract;
Expand All @@ -41,8 +37,10 @@
import java.util.Collection;
import java.util.stream.Collectors;

import static com.maddyhome.idea.vim.api.VimInjectorKt.*;
import static com.maddyhome.idea.vim.api.VimInjectorKt.injector;
import static com.maddyhome.idea.vim.api.VimInjectorKt.options;
import static com.maddyhome.idea.vim.helper.CaretVisualAttributesHelperKt.updateCaretsVisualAttributes;
import static com.maddyhome.idea.vim.newapi.IjVimInjectorKt.ijOptions;

/**
* @author vlan
Expand All @@ -57,7 +55,7 @@ public class EditorGroup implements PersistentStateComponent<Element>, VimEditor
@Override
public void caretPositionChanged(@NotNull CaretEvent e) {
final boolean requiresRepaint = e.getNewPosition().line != e.getOldPosition().line;
if (requiresRepaint && options(injector, new IjVimEditor(e.getEditor())).isSet(Options.relativenumber)) {
if (requiresRepaint && options(injector, new IjVimEditor(e.getEditor())).getRelativenumber()) {
repaintRelativeLineNumbers(e.getEditor());
}
}
Expand Down Expand Up @@ -105,9 +103,9 @@ private static boolean isProjectDisposed(final @NotNull Editor editor) {
}

private static void updateLineNumbers(final @NotNull Editor editor) {
final OptionValueAccessor options = options(injector, new IjVimEditor(editor));
final boolean relativeNumber = options.isSet(Options.relativenumber);
final boolean number = options.isSet(Options.number);
final EffectiveOptions options = options(injector, new IjVimEditor(editor));
final boolean relativeNumber = options.getRelativenumber();
final boolean number = options.getNumber();

final boolean showBuiltinEditorLineNumbers = shouldShowBuiltinLineNumbers(editor, number, relativeNumber);

Expand Down Expand Up @@ -231,8 +229,8 @@ public void editorDeinit(@NotNull Editor editor, boolean isReleased) {
CaretVisualAttributesHelperKt.removeCaretsVisualAttributes(editor);
}

public void notifyIdeaJoin(@Nullable Project project) {
if (VimPlugin.getVimState().isIdeaJoinNotified() || globalOptions(injector).isSet(IjOptions.INSTANCE.getIdeajoin())) {
public void notifyIdeaJoin(@Nullable Project project, @NotNull VimEditor editor) {
if (VimPlugin.getVimState().isIdeaJoinNotified() || ijOptions(injector, editor).getIdeajoin()) {
return;
}

Expand All @@ -255,7 +253,7 @@ public void loadState(@NotNull Element state) {

@Override
public void notifyIdeaJoin(@NotNull VimEditor editor) {
notifyIdeaJoin(((IjVimEditor) editor).getEditor().getProject());
notifyIdeaJoin(((IjVimEditor) editor).getEditor().getProject(), editor);
}

public static class NumberChangeListener implements LocalOptionChangeListener<VimInt> {
Expand Down Expand Up @@ -287,7 +285,7 @@ public void processLocalValueChange(@Nullable VimInt oldValue, @NotNull VimEdito
private static class RelativeLineNumberConverter implements LineNumberConverter {
@Override
public Integer convert(@NotNull Editor editor, int lineNumber) {
final boolean number = options(injector, new IjVimEditor(editor)).isSet(Options.number);
final boolean number = options(injector, new IjVimEditor(editor)).getNumber();
final int caretLine = editor.getCaretModel().getLogicalPosition().line;

// lineNumber is 1 based
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/maddyhome/idea/vim/group/FileGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import java.io.File;
import java.util.Collection;

import static com.maddyhome.idea.vim.api.VimInjectorKt.globalOptions;
import static com.maddyhome.idea.vim.api.VimInjectorKt.injector;
import static com.maddyhome.idea.vim.newapi.IjVimInjectorKt.globalIjOptions;

public class FileGroup extends VimFileBase {
public boolean openFile(@NotNull String filename, @NotNull ExecutionContext context) {
Expand Down Expand Up @@ -185,7 +185,7 @@ public void closeFile(int number, @NotNull ExecutionContext context) {
@Override
public void saveFile(@NotNull ExecutionContext context) {
NativeAction action;
if (globalOptions(injector).hasValue(IjOptions.ideawrite, IjOptionConstants.ideawrite_all)) {
if (globalIjOptions(injector).getIdeawrite().contains(IjOptionConstants.ideawrite_all)) {
action = injector.getNativeActionManager().getSaveAll();
}
else {
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/group/IjOptionProperties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.group

import com.maddyhome.idea.vim.api.GlobalOptions
import com.maddyhome.idea.vim.api.StringListOptionValue
import com.maddyhome.idea.vim.options.OptionScope

/**
* An accessor class for IntelliJ implementation specific global options
*
* This class will only access IntelliJ specific global options. It does not provide access to Vim standard global
* options
*/
@Suppress("SpellCheckingInspection")
public open class GlobalIjOptions(scope: OptionScope = OptionScope.GLOBAL) : GlobalOptions(scope) {
public var closenotebooks: Boolean by optionProperty(IjOptions.closenotebooks)
public var ide: String by optionProperty(IjOptions.ide)
public var ideamarks: Boolean by optionProperty(IjOptions.ideamarks)
public var ideastatusicon: String by optionProperty(IjOptions.ideastatusicon)
public val ideavimsupport: StringListOptionValue by optionProperty(IjOptions.ideavimsupport)
public var ideawrite: String by optionProperty(IjOptions.ideawrite)
public val lookupkeys: StringListOptionValue by optionProperty(IjOptions.lookupkeys)
public var trackactionids: Boolean by optionProperty(IjOptions.trackactionids)
public var visualdelay: Int by optionProperty(IjOptions.visualdelay)

// TODO: Handle these options as global-local
// Decide if they should live in global or effective options when we support global-local
// (I suspect they should live in effective, because we'll always want to read the local value. We are unlikely to
// ever set from code, but we'd expect normal `:set` behaviour, which appears to be to write to the global value).
// Also double check that these options should be global-local
public var ideajoin: Boolean by optionProperty(IjOptions.ideajoin)
public var idearefactormode: String by optionProperty(IjOptions.idearefactormode)

// Temporary options to control work-in-progress behaviour
public var octopushandler: Boolean by optionProperty(IjOptions.octopushandler)
public var oldundo: Boolean by optionProperty(IjOptions.oldundo)
public var unifyjumps: Boolean by optionProperty(IjOptions.unifyjumps)
}

/**
* An accessor class for IntelliJ implementation specific option values effective for the given local scope
*
* As a convenience, this class will also provide access to the global options that are effective for the local scope.
*/
public class EffectiveIjOptions(scope: OptionScope.LOCAL): GlobalIjOptions(scope) {
public var ideacopypreprocess: Boolean by optionProperty(IjOptions.ideacopypreprocess)
}
Loading