-
Notifications
You must be signed in to change notification settings - Fork 7
/
Settings.kt
111 lines (92 loc) · 2.83 KB
/
Settings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.fwdekker.randomness
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.util.xmlb.annotations.Transient
/**
* Settings are composed of [Scheme]s and persist these over IDE restarts.
*
* @param SELF the type of settings that should be persisted; should be a self reference
* @param SCHEME the type of scheme that the settings consist of
*/
interface Settings<SELF, SCHEME : Scheme<SCHEME>> : PersistentStateComponent<SELF> {
/**
* The various schemes that are contained within the settings.
*/
var schemes: MutableList<SCHEME>
/**
* The name of the scheme that is currently active.
*/
var currentSchemeName: String
/**
* The instance of the scheme that is currently active.
*
* This field is backed by [currentSchemeName]. If [currentSchemeName] refers to a scheme that is not contained in
* [schemes], `get`ting this field will throw an exception.
*/
@Suppress("UseCheckOrError") // This is shorter and faster
var currentScheme: SCHEME
@Transient
get() = schemes.firstOrNull { it.name == currentSchemeName }
?: throw IllegalStateException("Current scheme does not exist.")
set(value) {
currentSchemeName = value.name
}
/**
* Returns a deep copy of the settings and the contained schemes.
*
* @return a deep copy of the settings and the contained schemes
*/
fun deepCopy(): SELF
/**
* Returns `this`.
*
* @return `this`
*/
override fun getState(): SELF
/**
* Copies the fields of `state` to `this`.
*
* @param state the state to load into `this`
*/
override fun loadState(state: SELF)
}
/**
* A scheme is a collection of configurable values.
*
* In a typical use case a user can quickly switch between instances of schemes of the same type to change the "preset"
* or "configuration" that is currently being used.
*
* @param SELF the type of scheme that is stored; should be a self reference
* @see Settings
*/
interface Scheme<SELF> : com.intellij.openapi.options.Scheme {
/**
* The name of the scheme, used to identify it.
*/
var myName: String
/**
* Same as [myName].
*/
override fun getName() = myName
/**
* Shallowly copies the state of [other] into `this`.
*
* @param other the state to copy into `this`
*/
fun copyFrom(other: SELF)
/**
* Returns a copy of this scheme that has the given name.
*
* @param name the name to give to the copy
* @return a copy of this scheme that has the given name
*/
fun copyAs(name: String): SELF
/**
* Holds constants.
*/
companion object {
/**
* The name of the default scheme.
*/
const val DEFAULT_NAME = "Default"
}
}