Skip to content

Commit

Permalink
Use options API for more cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
citizenmatt committed Apr 26, 2023
1 parent 904ee6b commit 0c6425e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ 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 com.maddyhome.idea.vim.options.OptionScope
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import java.awt.event.InputEvent
import java.awt.event.KeyEvent
import javax.swing.KeyStroke
Expand Down Expand Up @@ -255,15 +257,13 @@ internal class VimShortcutKeyAction : AnAction(), DumbAware/*, LightEditCompatib
* if the pressed key is presented in this list. The caches are used to speedup the process.
*/
private object LookupKeys {
private var parsedLookupKeys: Set<KeyStroke> = parseLookupKeys()

init {
VimPlugin.getOptionGroup().addListener(IjOptions.lookupkeys, { parsedLookupKeys = parseLookupKeys() })
fun isEnabledForLookup(keyStroke: KeyStroke): Boolean {
val parsedLookupKeys =
injector.optionGroup.getParsedEffectiveOptionValue(IjOptions.lookupkeys, OptionScope.GLOBAL, ::parseLookupKeys)
return keyStroke !in parsedLookupKeys
}

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

private fun parseLookupKeys() = injector.globalIjOptions().lookupkeys
private fun parseLookupKeys(value: VimString) = IjOptions.lookupkeys.split(value.asString())
.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 @@ -59,7 +59,6 @@ internal fun Editor.hasBlockOrUnderscoreCaret() = isBlockCursorOverride() ||
internal object GuicursorChangeListener : OptionChangeListener<VimString> {
override fun processGlobalValueChange(oldValue: VimString?) {
AttributesCache.clear()
GuiCursorOptionHelper.clearEffectiveValues()
localEditors().forEach { it.updatePrimaryCaretVisualAttributes() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

package com.maddyhome.idea.vim.options.helpers

import com.maddyhome.idea.vim.api.globalOptions
import com.maddyhome.idea.vim.api.Options
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.command.VimStateMachine
import com.maddyhome.idea.vim.ex.exExceptionMessage
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.options.OptionScope
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import java.util.*

public object GuiCursorOptionHelper {

private val effectiveValues = mutableMapOf<GuiCursorMode, GuiCursorAttributes>()

public fun convertToken(token: String): GuiCursorEntry {
val split = token.split(':')
if (split.size == 1) {
Expand Down Expand Up @@ -72,43 +71,70 @@ public object GuiCursorOptionHelper {
}
}

return GuiCursorEntry(token, modes, type, thickness, highlightGroup, lmapHighlightGroup, blinkModes)
return GuiCursorEntry(modes, type, thickness, highlightGroup, lmapHighlightGroup, blinkModes)
}

public fun getAttributes(mode: GuiCursorMode): GuiCursorAttributes {
return effectiveValues.computeIfAbsent(mode) {
var type = GuiCursorType.BLOCK
var thickness = 0
var highlightGroup = ""
var lmapHighlightGroup = ""
var blinkModes = emptyList<String>()
injector.globalOptions().guicursor
.map { convertToken(it) }
.forEach { data ->
if (data.modes.contains(mode) || data.modes.contains(GuiCursorMode.ALL)) {
if (data.type != null) {
type = data.type
}
if (data.thickness != null) {
thickness = data.thickness
}
if (data.highlightGroup.isNotEmpty()) {
highlightGroup = data.highlightGroup
}
if (data.lmapHighlightGroup.isNotEmpty()) {
lmapHighlightGroup = data.lmapHighlightGroup
}
if (data.blinkModes.isNotEmpty()) {
blinkModes = data.blinkModes
}
val attributes = injector.optionGroup.getParsedEffectiveOptionValue(Options.guicursor, OptionScope.GLOBAL, ::parseGuicursor)
return attributes[mode] ?: GuiCursorAttributes.DEFAULT
}

private fun parseGuicursor(guicursor: VimString) = GuiCursorAttributeBuilders().also { builders ->
// Split into entries. Each entry has a list of modes and various attributes and adds to/overrides current values
Options.guicursor.split(guicursor.asString()).map { convertToken(it) }
.forEach { entry ->
entry.modes.forEach {
if (it == GuiCursorMode.ALL) {
builders.updateAllModes(entry)
} else {
builders.updateMode(it, entry)
}
}
GuiCursorAttributes(type, thickness, highlightGroup, lmapHighlightGroup, blinkModes)
}
}.build()

private class GuiCursorAttributeBuilders {
private class GuiCursorAttributesBuilder {
private var type: GuiCursorType = GuiCursorType.BLOCK
private var thickness: Int = 0
private var highlightGroup: String = ""
private var lmapHighlightGroup: String = ""
private var blinkModes: List<String> = emptyList()

fun updateFrom(entry: GuiCursorEntry) {
if (entry.type != null) {
type = entry.type
}
if (entry.thickness != null) {
thickness = entry.thickness
}
if (entry.highlightGroup.isNotEmpty()) {
highlightGroup = entry.highlightGroup
}
if (entry.lmapHighlightGroup.isNotEmpty()) {
lmapHighlightGroup = entry.lmapHighlightGroup
}
if (entry.blinkModes.isNotEmpty()) {
blinkModes = entry.blinkModes
}
}

fun build() = GuiCursorAttributes(type, thickness, highlightGroup, lmapHighlightGroup, blinkModes)
}

private val builders = mutableMapOf<GuiCursorMode, GuiCursorAttributesBuilder>()

fun updateMode(mode: GuiCursorMode, entry: GuiCursorEntry) {
builders.getOrPut(mode) { GuiCursorAttributesBuilder() }.updateFrom(entry)
}

fun updateAllModes(entry: GuiCursorEntry) {
GuiCursorMode.values().filter { it != GuiCursorMode.ALL }.forEach {
updateMode(it, entry)
}
}
}

public fun clearEffectiveValues() {
effectiveValues.clear()
fun build() = builders.map { it.key to it.value.build() }.toMap()
}
}

Expand Down Expand Up @@ -163,24 +189,28 @@ public enum class GuiCursorType(public val token: String) {
}

public class GuiCursorEntry(
private val originalString: String,
public val modes: EnumSet<GuiCursorMode>,
public val type: GuiCursorType?,
public val thickness: Int?,
public val highlightGroup: String,
public val lmapHighlightGroup: String,
public val blinkModes: List<String>,
) {
public override fun toString(): String {
// We need to match the original string for output and remove purposes
return originalString
}
}
)

public data class GuiCursorAttributes(
val type: GuiCursorType,
val thickness: Int,
val highlightGroup: String,
val lmapHighlightGroup: String,
val blinkModes: List<String>,
)
) {
public companion object {
public val DEFAULT: GuiCursorAttributes = GuiCursorAttributes(GuiCursorType.BLOCK,
thickness = 0,
highlightGroup = "",
lmapHighlightGroup = "",
blinkModes = emptyList()
)
}
}

0 comments on commit 0c6425e

Please sign in to comment.