mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-03-04 21:32:52 +01:00
Simplify BoundedListOption
This commit is contained in:
parent
dfbec1f23a
commit
287ba7055e
src/com/maddyhome/idea/vim/option
@ -20,58 +20,26 @@ package com.maddyhome.idea.vim.option;
|
||||
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class BoundedListOption extends ListOption {
|
||||
protected final @NotNull List<String> values;
|
||||
protected final @NotNull List<String> allowedValues;
|
||||
|
||||
public BoundedListOption(@NonNls String name, @NonNls String abbrev, @NonNls String[] dflt, @NonNls String[] values) {
|
||||
super(name, abbrev, dflt, null);
|
||||
public BoundedListOption(@NonNls String name,
|
||||
@NonNls String abbrev,
|
||||
@NonNls String[] defaultValues,
|
||||
@NonNls String[] allowedValues) {
|
||||
super(name, abbrev, defaultValues);
|
||||
|
||||
this.values = new ArrayList<>(Arrays.asList(values));
|
||||
this.allowedValues = new ArrayList<>(Arrays.asList(allowedValues));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean set(String val) {
|
||||
List<String> vals = parseVals(val);
|
||||
if (vals != null && values.containsAll(vals)) {
|
||||
set(vals);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean append(String val) {
|
||||
List<String> vals = parseVals(val);
|
||||
if (vals != null && values.containsAll(vals)) {
|
||||
append(vals);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean prepend(String val) {
|
||||
List<String> vals = parseVals(val);
|
||||
if (vals != null && values.containsAll(vals)) {
|
||||
prepend(vals);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(String val) {
|
||||
List<String> vals = parseVals(val);
|
||||
if (vals != null && values.containsAll(vals)) {
|
||||
remove(vals);
|
||||
}
|
||||
|
||||
return true;
|
||||
protected @Nullable String ConvertToken(String token) {
|
||||
return allowedValues.contains(token) ? token : null;
|
||||
}
|
||||
}
|
||||
|
@ -112,8 +112,8 @@ public final class KeywordOption extends ListOption {
|
||||
|
||||
@Override
|
||||
public void resetDefault() {
|
||||
if (!dflt.equals(value)) {
|
||||
value = dflt;
|
||||
if (!defaultValues.equals(value)) {
|
||||
value = defaultValues;
|
||||
set(getValue());
|
||||
}
|
||||
}
|
||||
|
@ -179,14 +179,28 @@ public class ListOption extends TextOption {
|
||||
*
|
||||
* @param name The name of the option
|
||||
* @param abbrev The short name
|
||||
* @param dflt The option's default values
|
||||
* @param defaultValues The option's default values
|
||||
*/
|
||||
public ListOption(@VimNlsSafe String name, @VimNlsSafe String abbrev, @VimNlsSafe String[] defaultValues) {
|
||||
this(name, abbrev, defaultValues, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the option
|
||||
*
|
||||
* @param name The name of the option
|
||||
* @param abbrev The short name
|
||||
* @param defaultValues The option's default values
|
||||
* @param pattern A regular expression that is used to validate new values. null if no check needed
|
||||
*/
|
||||
public ListOption(@VimNlsSafe String name, @VimNlsSafe String abbrev, @VimNlsSafe String[] dflt, @VimNlsSafe String pattern) {
|
||||
public ListOption(@VimNlsSafe String name,
|
||||
@VimNlsSafe String abbrev,
|
||||
@VimNlsSafe String[] defaultValues,
|
||||
@VimNlsSafe @Nullable String pattern) {
|
||||
super(name, abbrev);
|
||||
|
||||
this.dflt = new ArrayList<>(Arrays.asList(dflt));
|
||||
this.value = new ArrayList<>(this.dflt);
|
||||
this.defaultValues = new ArrayList<>(Arrays.asList(defaultValues));
|
||||
this.value = new ArrayList<>(this.defaultValues);
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
@ -197,7 +211,7 @@ public class ListOption extends TextOption {
|
||||
*/
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return dflt.equals(value);
|
||||
return defaultValues.equals(value);
|
||||
}
|
||||
|
||||
protected @Nullable List<String> parseVals(String val) {
|
||||
@ -205,8 +219,9 @@ public class ListOption extends TextOption {
|
||||
StringTokenizer tokenizer = new StringTokenizer(val, ",");
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
String token = tokenizer.nextToken().trim();
|
||||
if (pattern == null || token.matches(pattern)) {
|
||||
res.add(token);
|
||||
String item = ConvertToken(token);
|
||||
if (item != null) {
|
||||
res.add(item);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
@ -216,6 +231,13 @@ public class ListOption extends TextOption {
|
||||
return res;
|
||||
}
|
||||
|
||||
protected @Nullable String ConvertToken(String token) {
|
||||
if (pattern == null) {
|
||||
return token;
|
||||
}
|
||||
return token.matches(pattern) ? token : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the string representation appropriate for output to :set all
|
||||
*
|
||||
@ -225,18 +247,18 @@ public class ListOption extends TextOption {
|
||||
return " " + getName() + "=" + getValue();
|
||||
}
|
||||
|
||||
protected final @NotNull List<String> dflt;
|
||||
protected final @NotNull List<String> defaultValues;
|
||||
protected @NotNull List<String> value;
|
||||
protected final String pattern;
|
||||
protected final @Nullable String pattern;
|
||||
|
||||
/**
|
||||
* Resets the option to its default value
|
||||
*/
|
||||
@Override
|
||||
public void resetDefault() {
|
||||
if (!dflt.equals(value)) {
|
||||
if (!defaultValues.equals(value)) {
|
||||
String oldValue = getValue();
|
||||
value = new ArrayList<>(dflt);
|
||||
value = new ArrayList<>(defaultValues);
|
||||
fireOptionChangeEvent(oldValue, getValue());
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ object OptionsManager {
|
||||
private val options: MutableMap<String, Option<*>> = mutableMapOf()
|
||||
private val abbrevs: MutableMap<String, Option<*>> = mutableMapOf()
|
||||
|
||||
val clipboard = addOption(ListOption(ClipboardOptionsData.name, ClipboardOptionsData.abbr, arrayOf(ClipboardOptionsData.ideaput, "autoselect,exclude:cons\\|linux"), null))
|
||||
val clipboard = addOption(ListOption(ClipboardOptionsData.name, ClipboardOptionsData.abbr, arrayOf(ClipboardOptionsData.ideaput, "autoselect,exclude:cons\\|linux")))
|
||||
val digraph = addOption(ToggleOption("digraph", "dg", false))
|
||||
val gdefault = addOption(ToggleOption("gdefault", "gd", false))
|
||||
val history = addOption(NumberOption("history", "hi", 50, 1, Int.MAX_VALUE))
|
||||
@ -59,7 +59,7 @@ object OptionsManager {
|
||||
val incsearch = addOption(ToggleOption("incsearch", "is", false))
|
||||
val iskeyword = addOption(KeywordOption("iskeyword", "isk", arrayOf("@", "48-57", "_")))
|
||||
val keymodel = addOption(KeyModelOptionData.option)
|
||||
val lookupKeys = addOption(ListOption(LookupKeysData.name, LookupKeysData.name, LookupKeysData.defaultValues, null))
|
||||
val lookupKeys = addOption(ListOption(LookupKeysData.name, LookupKeysData.name, LookupKeysData.defaultValues))
|
||||
val matchpairs = addOption(ListOption("matchpairs", "mps", arrayOf("(:)", "{:}", "[:]"), ".:."))
|
||||
val more = addOption(ToggleOption("more", "more", true))
|
||||
val nrformats = addOption(BoundedListOption("nrformats", "nf", arrayOf("hex"), arrayOf("octal", "hex", "alpha"))) // Octal is disabled as in neovim
|
||||
@ -80,7 +80,7 @@ object OptionsManager {
|
||||
val timeout = addOption(ToggleOption("timeout", "to", true))
|
||||
val timeoutlen = addOption(NumberOption("timeoutlen", "tm", 1000, -1, Int.MAX_VALUE))
|
||||
val undolevels = addOption(NumberOption("undolevels", "ul", 1000, -1, Int.MAX_VALUE))
|
||||
val viminfo = addOption(ListOption("viminfo", "vi", arrayOf("'100", "<50", "s10", "h"), null))
|
||||
val viminfo = addOption(ListOption("viminfo", "vi", arrayOf("'100", "<50", "s10", "h")))
|
||||
val virtualedit = addOption(BoundedStringOption(VirtualEditData.name, "ve", "", VirtualEditData.allValues))
|
||||
val visualbell = addOption(ToggleOption("visualbell", "vb", false))
|
||||
val wrapscan = addOption(ToggleOption("wrapscan", "ws", true))
|
||||
|
Loading…
Reference in New Issue
Block a user