Skip to content

Commit

Permalink
refactor AceConfig, fixes acejump#305
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 26, 2019
1 parent cdcba93 commit a3801ff
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 38 deletions.
38 changes: 33 additions & 5 deletions src/main/kotlin/org/acejump/config/AceConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.acejump.config
import com.intellij.openapi.components.*
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.options.Configurable
import org.acejump.label.Pattern
import org.acejump.label.Pattern.Companion.KeyLayout
import java.awt.Color

/* Persists the state of the AceJump IDE settings across IDE restarts.
* https://www.jetbrains.org/intellij/sdk/docs/basics/persisting_state_of_components.html
Expand All @@ -11,7 +14,28 @@ import com.intellij.openapi.options.Configurable
@State(name = "AceConfig", storages = [(Storage("AceJump.xml"))])
object AceConfig: Configurable, PersistentStateComponent<AceSettings> {
private val logger = Logger.getInstance(AceConfig::class.java)
var settings = AceSettings()
private var settings = AceSettings()
set(value) {
allPossibleTags = value.allowedChars.bigrams()
field = value
}

val allowedChars: String get() = settings.allowedChars
val layout: KeyLayout get() = settings.layout
val jumpModeColor: Color get() = settings.jumpModeColor
val textHighlightColor: Color get() = settings.textHighlightColor
val targetModeColor: Color get() = settings.targetModeColor
val definitionModeColor: Color get() = settings.definitionModeColor
val tagForegroundColor: Color get() = settings.tagForegroundColor
val tagBackgroundColor: Color get() = settings.tagBackgroundColor

private var allPossibleTags: Set<String> = settings.allowedChars.bigrams()

private fun String.bigrams() = run { flatMap { e -> map { c -> "$e$c" } } }
.sortedWith(Pattern.defaultTagOrder).toSet()

fun getCompatibleTags(query: String, matching: (String) -> Boolean) =
Pattern.filter(allPossibleTags, query).filter(matching).toSet()

override fun getState() = settings

Expand All @@ -35,12 +59,16 @@ object AceConfig: Configurable, PersistentStateComponent<AceSettings> {
panel.tagForegroundColor != settings.tagForegroundColor ||
panel.tagBackgroundColor != settings.tagBackgroundColor

private fun String.distinctAlphanumerics() = toList().distinct().run {
if (isEmpty()) settings.layout.chars() else filter { it.isLetterOrDigit() }
}.joinToString("")
private fun String.distinctAlphanumerics() =
if (isEmpty()) settings.layout.text
else toList().distinct().filter(Char::isLetterOrDigit).joinToString("")

override fun apply() {
settings.allowedChars = panel.allowedChars.distinctAlphanumerics()
panel.allowedChars.distinctAlphanumerics().let {
settings.allowedChars = it
allPossibleTags = it.bigrams()
}

settings.layout = panel.keyboardLayout
panel.jumpModeColor?.let { settings.jumpModeRGB = it.rgb }
panel.targetModeColor?.let { settings.targetModeRGB = it.rgb }
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/acejump/config/AceSettings.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.acejump.config

import org.acejump.label.Pattern.Companion.KeyLayout
import org.acejump.label.Pattern.Companion.KeyLayout.QWERTY
import java.awt.Color
import java.awt.Color.*
import kotlin.reflect.KProperty

// TODO: https://github.com/acejump/AceJump/issues/215
data class AceSettings(var layout: KeyLayout = KeyLayout.QWERTY,
var allowedChars: String = layout.allChars(),
data class AceSettings(var layout: KeyLayout = QWERTY,
var allowedChars: String = layout.text,
// These must be primitives in order to be serializable
internal var jumpModeRGB: Int = BLUE.rgb,
internal var targetModeRGB: Int = RED.rgb,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/acejump/config/AceSettingsPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class AceSettingsPanel {

keyboardLayoutCombo.run {
KeyLayout.values().forEach { addItem(it) }
addActionListener { keyChars = (selectedItem as KeyLayout).keyboard() }
addActionListener { keyChars = (selectedItem as KeyLayout).joinBy("\n") }
}
}

Expand Down
27 changes: 12 additions & 15 deletions src/main/kotlin/org/acejump/label/Pattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@ enum class Pattern(val string: String) {
companion object {
private fun distance(fromKey: Char, toKey: Char) = nearby[fromKey]!![toKey]

private val allBigrams
get() = AceConfig.settings.allowedChars
.run { flatMap { e -> map { c -> "$e$c" } } }
.sortedWith(defaultTagOrder)
val defaultTagOrder: Comparator<String> = compareBy(
{ it[0].isDigit() || it[1].isDigit() },
{ distance(it[0], it.last()) },
AceConfig.layout.priority { it[0] })

val NUM_TAGS: Int
get() = NUM_CHARS * NUM_CHARS

val NUM_CHARS: Int
get() = AceConfig.settings.allowedChars.length

val defaultTagOrder: Comparator<String> = compareBy(
{ it[0].isDigit() || it[1].isDigit() },
{ distance(it[0], it.last()) },
AceConfig.settings.layout.priority { it[0] })
get() = AceConfig.allowedChars.length

fun filterTags(query: String) = allBigrams.filter { !query.endsWith(it[0]) }
fun filter(bigrams: Set<String>, query: String) =
bigrams.filter { !query.endsWith(it[0]) }

/**
* Sorts available tags by key distance. Tags which are ergonomically easier
Expand All @@ -43,7 +39,7 @@ enum class Pattern(val string: String) {
* keys (ex. 12, 21) to keys that are located further apart on the keyboard.
*/

enum class KeyLayout(vararg val text: String) {
enum class KeyLayout(vararg val rows: String) {
COLEMK("1234567890", "qwfpgjluy", "arstdhneio", "zxcvbkm"),
WORKMN("1234567890", "qdrwbjfup", "ashtgyneoi", "zxmcvkl"),
DVORAK("1234567890", "pyfgcrl", "aoeuidhtns", "qjkxbmwvz"),
Expand All @@ -65,13 +61,14 @@ enum class Pattern(val string: String) {
WORKMN -> "tnhegysoaiclvkmxzwfrubjdpq5849673210"
}.mapIndices()

fun chars() = text.flatMap { it.toList() }.sortedBy { priority[it] }
val text by lazy {
joinBy("").toCharArray().sortedBy { priority[it] }.joinToString("")
}

fun priority(tagToChar: (String) -> Char): (String) -> Int? =
{ priority[tagToChar(it)] }

fun keyboard() = text.joinToString("\n")
fun allChars() = chars().joinToString("")
fun joinBy(separator: CharSequence) = rows.joinToString(separator)
}

private val nearby: Map<Char, Map<Char, Int>> = mapOf(
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/acejump/label/Solver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import kotlin.system.measureTimeMillis
*/

class Solver(val text: String,
val query: String = "",
val query: String,
val results: Collection<Int>,
val availableTags: Set<String> = Pattern.filterTags("").toSet(),
val availableTags: Set<String>,
val viewBounds: IntRange = 0..text.length) {
private val logger = Logger.getInstance(Solver::class.java)
private var newTags: MutableMap<String, Int> = HashMap(Pattern.NUM_TAGS)
Expand All @@ -72,7 +72,7 @@ class Solver(val text: String,

private val tagOrder = defaultTagOrder
.thenBy { eligibleSitesByTag[it].size }
.thenBy(AceConfig.settings.layout.priority { it.last() })
.thenBy(AceConfig.layout.priority { it.last() })

/**
* Sorts jump targets to determine which positions get first choice for tags,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/acejump/label/Tagger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.acejump.label
import com.google.common.collect.BiMap
import com.google.common.collect.HashBiMap
import com.intellij.openapi.diagnostic.Logger
import org.acejump.config.AceConfig
import org.acejump.control.Scroller
import org.acejump.label.Pattern.Companion.defaultTagOrder
import org.acejump.label.Pattern.Companion.filterTags
import org.acejump.search.*
import org.acejump.view.Marker
import org.acejump.view.Model.editor
Expand Down Expand Up @@ -61,7 +61,7 @@ object Tagger : Resettable {
logger.info("Received query: \"$query\"")
}

val availableTags = filterTags(query).filter { it !in tagMap }.toSet()
val availableTags = AceConfig.getCompatibleTags(query) { it !in tagMap }

measureTimeMillis {
textMatches = refineSearchResults(results)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/acejump/search/AceUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import java.awt.Point
import java.util.*
import javax.swing.JComponent
import kotlin.math.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

interface Resettable {
fun reset()
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/org/acejump/search/JumpMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ internal enum class JumpMode {
set(value) {
field = value
setCaretColor(when (field) {
DEFAULT -> AceConfig.settings.jumpModeColor
DEFINE -> AceConfig.settings.definitionModeColor
TARGET -> AceConfig.settings.targetModeColor
DEFAULT -> AceConfig.jumpModeColor
DEFINE -> AceConfig.definitionModeColor
TARGET -> AceConfig.targetModeColor
DISABLED -> Model.naturalCaretColor
else -> AceConfig.settings.jumpModeColor
else -> AceConfig.jumpModeColor
})

Finder.markup(Tagger.markers)
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/org/acejump/view/Marker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Marker : CustomHighlighterRenderer {

setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON)

color = AceConfig.settings.textHighlightColor
color = AceConfig.textHighlightColor
if (regex) highlightRegex()
else {
fillRoundRect(start.x, startY, searchWidth, rectHeight, arcD, arcD)
Expand All @@ -128,7 +128,7 @@ class Marker : CustomHighlighterRenderer {
}

private fun Graphics2D.surroundTargetWord() {
color = AceConfig.settings.targetModeColor
color = AceConfig.targetModeColor
val (wordStart, wordEnd) = text.wordBounds(index)

val xPos = editor.getPoint(wordStart).x
Expand All @@ -140,7 +140,7 @@ class Marker : CustomHighlighterRenderer {

private fun Graphics2D.drawTagForeground(tagPosition: Point?) {
font = Model.font
color = AceConfig.settings.tagForegroundColor
color = AceConfig.tagForegroundColor
composite = getInstance(SRC_OVER, 1.toFloat())

drawString(tag!!.toUpperCase(), tagPosition!!.x, tagPosition.y + fontHeight)
Expand Down Expand Up @@ -191,7 +191,7 @@ class Marker : CustomHighlighterRenderer {

fun highlightFirst() {
composite = getInstance(SRC_OVER, 0.40.toFloat())
color = AceConfig.settings.textHighlightColor
color = AceConfig.textHighlightColor

if (endsWith && query.last() != textChar) {
fillRoundRect(tagX, yPos, fontWidth, rectHeight, arcD, arcD)
Expand All @@ -201,7 +201,7 @@ class Marker : CustomHighlighterRenderer {
}

fun highlightLast() {
color = AceConfig.settings.tagBackgroundColor
color = AceConfig.tagBackgroundColor
if (alignment != RIGHT || text.hasSpaceRight(index) || regex)
composite = getInstance(SRC_OVER, 1.toFloat())

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/acejump/view/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ object Model {
fun Editor.setupCaret() {
settings.isBlockCursor = true
settings.isBlinkCaret = false
colorsScheme.setColor(CARET_COLOR, AceConfig.settings.jumpModeColor)
colorsScheme.setColor(CARET_COLOR, AceConfig.jumpModeColor)
}
}

0 comments on commit a3801ff

Please sign in to comment.