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

Fleet #897

Merged
merged 22 commits into from
Jun 5, 2024
Merged

Fleet #897

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 1 addition & 103 deletions src/main/java/com/maddyhome/idea/vim/group/SearchGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
package com.maddyhome.idea.vim.group;

import com.intellij.openapi.application.ApplicationManager;
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.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
Expand Down Expand Up @@ -63,11 +59,8 @@
/**
* @deprecated Replace with IjVimSearchGroup
*/
@State(name = "VimSearchSettings", storages = {
@Storage(value = "$APP_CONFIG$/vim_settings_local.xml", roamingType = RoamingType.DISABLED)
})
@Deprecated
public class SearchGroup extends IjVimSearchGroup implements PersistentStateComponent<Element> {
public class SearchGroup extends IjVimSearchGroup {
public SearchGroup() {
super();
if (!globalIjOptions(injector).getUseNewRegex()) {
Expand Down Expand Up @@ -1409,101 +1402,6 @@ else if (lastPatternOffset.charAt(ppos + 1) == '?') {
return new Pair<Integer, MotionType>(res, motionType);
}


// *******************************************************************************************************************
//
// Persistent state
//
// *******************************************************************************************************************
//region Persistent state
public void saveData(@NotNull Element element) {
logger.debug("saveData");
Element search = new Element("search");

addOptionalTextElement(search, "last-search", lastSearch);
addOptionalTextElement(search, "last-substitute", lastSubstitute);
addOptionalTextElement(search, "last-offset", !lastPatternOffset.isEmpty() ? lastPatternOffset : null);
addOptionalTextElement(search, "last-replace", lastReplace);
addOptionalTextElement(search, "last-pattern", lastPatternIdx == RE_SEARCH ? lastSearch : lastSubstitute);
addOptionalTextElement(search, "last-dir", Integer.toString(lastDir.toInt()));
addOptionalTextElement(search, "show-last", Boolean.toString(showSearchHighlight));

element.addContent(search);
}

private static void addOptionalTextElement(@NotNull Element element, @NotNull String name, @Nullable String text) {
if (text != null) {
element.addContent(VimPlugin.getXML().setSafeXmlText(new Element(name), text));
}
}

public void readData(@NotNull Element element) {
logger.debug("readData");
Element search = element.getChild("search");
if (search == null) {
return;
}

lastSearch = getSafeChildText(search, "last-search");
lastSubstitute = getSafeChildText(search, "last-substitute");
lastReplace = getSafeChildText(search, "last-replace");
lastPatternOffset = getSafeChildText(search, "last-offset", "");

final String lastPatternText = getSafeChildText(search, "last-pattern");
if (lastPatternText == null || lastPatternText.equals(lastSearch)) {
lastPatternIdx = RE_SEARCH;
}
else {
lastPatternIdx = RE_SUBST;
}

Element dir = search.getChild("last-dir");
try {
lastDir = Direction.Companion.fromInt(Integer.parseInt(dir.getText()));
}
catch (NumberFormatException e) {
lastDir = Direction.FORWARDS;
}

Element show = search.getChild("show-last");
final boolean disableHighlight = globalOptions(injector).getViminfo().contains("h");
showSearchHighlight = !disableHighlight && Boolean.parseBoolean(show.getText());
if (logger.isDebugEnabled()) {
logger.debug("show=" + show + "(" + show.getText() + ")");
logger.debug("showSearchHighlight=" + showSearchHighlight);
}
}

private static @Nullable String getSafeChildText(@NotNull Element element, @NotNull String name) {
final Element child = element.getChild(name);
return child != null ? VimPlugin.getXML().getSafeXmlText(child) : null;
}

@SuppressWarnings("SameParameterValue")
private static @NotNull String getSafeChildText(@NotNull Element element, @NotNull String name, @NotNull String defaultValue) {
final Element child = element.getChild(name);
if (child != null) {
final String value = VimPlugin.getXML().getSafeXmlText(child);
return value != null ? value : defaultValue;
}
return defaultValue;
}

@Nullable
@Override
public Element getState() {
Element element = new Element("search");
saveData(element);
return element;
}

@Override
public void loadState(@NotNull Element state) {
readData(state);
}
//endregion


private enum ReplaceConfirmationChoice {
SUBSTITUTE_THIS,
SUBSTITUTE_LAST,
Expand Down
97 changes: 96 additions & 1 deletion src/main/java/com/maddyhome/idea/vim/newapi/IjVimSearchGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
package com.maddyhome.idea.vim.newapi

import com.intellij.openapi.application.ApplicationManager
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.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.util.Ref
Expand All @@ -20,6 +24,9 @@ import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.VimSearchGroupBase
import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.common.Direction
import com.maddyhome.idea.vim.common.Direction.Companion.fromInt
import com.maddyhome.idea.vim.diagnostic.vimLogger
import com.maddyhome.idea.vim.helper.MessageHelper
import com.maddyhome.idea.vim.helper.TestInputModel.Companion.getInstance
import com.maddyhome.idea.vim.helper.addSubstitutionConfirmationHighlight
Expand All @@ -30,10 +37,18 @@ import com.maddyhome.idea.vim.helper.updateSearchHighlights
import com.maddyhome.idea.vim.options.GlobalOptionChangeListener
import com.maddyhome.idea.vim.ui.ModalEntry
import com.maddyhome.idea.vim.vimscript.model.functions.handlers.SubmatchFunctionHandler
import org.jdom.Element
import org.jetbrains.annotations.TestOnly
import javax.swing.KeyStroke

public open class IjVimSearchGroup : VimSearchGroupBase() {
@State(
name = "VimSearchSettings",
storages = [Storage(value = "\$APP_CONFIG$/vim_settings_local.xml", roamingType = RoamingType.DISABLED)]
)
public open class IjVimSearchGroup : VimSearchGroupBase(), PersistentStateComponent<Element> {
public companion object {
private val logger = vimLogger<IjVimSearchGroup>()
}

init {
// We use the global option listener instead of the effective listener that gets called for each affected editor
Expand Down Expand Up @@ -175,6 +190,86 @@ public open class IjVimSearchGroup : VimSearchGroupBase() {
updateSearchHighlights(false)
}

public fun saveData(element: Element) {
logger.debug("saveData")
val search = Element("search")

addOptionalTextElement(search, "last-search", lastSearchPattern)
addOptionalTextElement(search, "last-substitute", lastSubstitutePattern)
addOptionalTextElement(search, "last-offset", lastPatternTrailing)
addOptionalTextElement(search, "last-replace", lastReplaceString)
addOptionalTextElement(
search,
"last-pattern",
if (lastPatternType == PatternType.SEARCH) lastSearchPattern else lastSubstitutePattern
)
addOptionalTextElement(search, "last-dir", getLastSearchDirection().toInt().toString())
addOptionalTextElement(search, "show-last", showSearchHighlight.toString())

element.addContent(search)
}

private fun addOptionalTextElement(element: Element, name: String, text: String?) {
if (text != null) {
element.addContent(VimPlugin.getXML().setSafeXmlText(Element(name), text))
}
}

public fun readData(element: Element) {
logger.debug("readData")
val search = element.getChild("search") ?: return

lastSearchPattern = getSafeChildText(search, "last-search")
lastSubstitutePattern = getSafeChildText(search, "last-substitute")
lastReplaceString = getSafeChildText(search, "last-replace")
lastPatternTrailing = getSafeChildText(search, "last-offset", "")

val lastPatternText = getSafeChildText(search, "last-pattern")
if (lastPatternText == null || lastPatternText == lastSearchPattern) {
lastPatternType = PatternType.SEARCH
} else {
lastPatternType = PatternType.SUBSTITUTE
}

val dir = search.getChild("last-dir")
try {
lastDirection = fromInt(dir.text.toInt())
} catch (e: NumberFormatException) {
lastDirection = Direction.FORWARDS
}

val show = search.getChild("show-last")
val disableHighlight = injector.globalOptions().viminfo.contains("h")
showSearchHighlight = !disableHighlight && show.text.toBoolean()
if (logger.isDebug()) {
logger.debug("show=" + show + "(" + show.text + ")")
logger.debug("showSearchHighlight=$showSearchHighlight")
}
}

private fun getSafeChildText(element: Element, name: String): String? {
val child = element.getChild(name)
return if (child != null) VimPlugin.getXML().getSafeXmlText(child) else null
}

private fun getSafeChildText(element: Element, name: String, defaultValue: String): String {
val child = element.getChild(name)
if (child != null) {
val value = VimPlugin.getXML().getSafeXmlText(child)
return value ?: defaultValue
}
return defaultValue
}

public override fun getState(): Element? {
val element = Element("search")
saveData(element)
return element
}

public override fun loadState(state: Element) {
readData(state)
}
private class IjSearchHighlight(private val editor: Editor, private val highlighter: RangeHighlighter) :
SearchHighlight() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,35 @@ public abstract class VimSearchGroupBase : VimSearchGroup {
/**
* Last string trailing a pattern. E.g. in '/pattern/e+2', 'e+2' is trailing.
*/
private var lastPatternTrailing: String? = ""
public var lastPatternTrailing: String? = ""

/**
* Last used search direction.
*/
private var lastDirection: Direction = Direction.FORWARDS
@JvmStatic
protected var lastDirection: Direction = Direction.FORWARDS
@JvmStatic
get
@JvmStatic
set

/**
* The type of the last used pattern.
*/
private var lastPatternType: PatternType? = null
@JvmStatic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need all new @JvmStatic? I see that their usage was moved from java class to kotlin class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's Kotlin magic to make protected fields in companion objects work

protected var lastPatternType: PatternType? = null
@JvmStatic
get
@JvmStatic
set

/**
* Last used substitute string.
*/
private var lastSubstituteString: String? = null

public var lastReplaceString: String? = null

private val CLASS_NAMES: List<String> = listOf(
"alnum:]",
"alpha:]",
Expand Down Expand Up @@ -936,6 +948,7 @@ public abstract class VimSearchGroupBase : VimSearchGroup {

// Set last substitute pattern, but only for explicitly typed patterns. Reused patterns are not saved/updated
setLastUsedPattern(pattern, PatternType.SUBSTITUTE, isNewPattern)
lastReplaceString = sub

// Always reset after checking, only set for nv_ident
lastIgnoreSmartCase = false
Expand Down