-
-
Notifications
You must be signed in to change notification settings - Fork 971
/
TextEditingSettings.swift
188 lines (159 loc) · 6.86 KB
/
TextEditingSettings.swift
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// TextEditingPreferences.swift
// CodeEditModules/Settings
//
// Created by Nanashi Li on 2022/04/08.
//
import AppKit
import Foundation
extension SettingsData {
/// The global settings for text editing
struct TextEditingSettings: Codable, Hashable, SearchableSettingsPage {
var searchKeys: [String] {
[
"Prefer Indent Using",
"Tab Width",
"Wrap lines to editor width",
"Font",
"Font Size",
"Line Height",
"Letter Spacing",
"Autocomplete braces",
"Enable type-over completion",
"Bracket Pair Highlight"
]
.map { NSLocalizedString($0, comment: "") }
}
/// An integer indicating how many spaces a `tab` will appear as visually.
var defaultTabWidth: Int = 4
/// The behavior of a `tab` keypress. If `.tab`, will insert a tab character. If `.spaces` will insert
/// `.spaceCount` spaces instead.
var indentOption: IndentOption = IndentOption(indentType: .spaces, spaceCount: 4)
/// The font to use in editor.
var font: EditorFont = .init()
/// A flag indicating whether type-over completion is enabled
var enableTypeOverCompletion: Bool = true
/// A flag indicating whether braces are automatically completed
var autocompleteBraces: Bool = true
/// A flag indicating whether to wrap lines to editor width
var wrapLinesToEditorWidth: Bool = true
/// A multiplier for setting the line height. Defaults to `1.45`
var lineHeightMultiple: Double = 1.45
/// A multiplier for setting the letter spacing, `1` being no spacing and
/// `2` is one character of spacing between letters, defaults to `1`.
var letterSpacing: Double = 1.0
/// The behavior of bracket pair highlights.
var bracketHighlight: BracketPairHighlight = BracketPairHighlight()
/// Default initializer
init() {
self.populateCommands()
}
/// Explicit decoder init for setting default values when key is not present in `JSON`
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.defaultTabWidth = try container.decodeIfPresent(Int.self, forKey: .defaultTabWidth) ?? 4
self.indentOption = try container.decodeIfPresent(
IndentOption.self,
forKey: .indentOption
) ?? IndentOption(indentType: .spaces, spaceCount: 4)
self.font = try container.decodeIfPresent(EditorFont.self, forKey: .font) ?? .init()
self.enableTypeOverCompletion = try container.decodeIfPresent(
Bool.self,
forKey: .enableTypeOverCompletion
) ?? true
self.autocompleteBraces = try container.decodeIfPresent(
Bool.self,
forKey: .autocompleteBraces
) ?? true
self.wrapLinesToEditorWidth = try container.decodeIfPresent(
Bool.self,
forKey: .wrapLinesToEditorWidth
) ?? true
self.lineHeightMultiple = try container.decodeIfPresent(
Double.self,
forKey: .lineHeightMultiple
) ?? 1.45
self.letterSpacing = try container.decodeIfPresent(
Double.self,
forKey: .letterSpacing
) ?? 1
self.bracketHighlight = try container.decodeIfPresent(
BracketPairHighlight.self,
forKey: .bracketHighlight
) ?? BracketPairHighlight()
self.populateCommands()
}
/// Adds toggle-able preferences to the command palette via shared `CommandManager`
private func populateCommands() {
let mgr = CommandManager.shared
mgr.addCommand(
name: "Toggle Type-Over Completion",
title: "Toggle Type-Over Completion",
id: "prefs.text_editing.type_over_completion",
command: CommandClosureWrapper {
Settings.shared.preferences.textEditing.enableTypeOverCompletion.toggle()
}
)
mgr.addCommand(
name: "Toggle Autocomplete Braces",
title: "Toggle Autocomplete Braces",
id: "prefs.text_editing.autocomplete_braces",
command: CommandClosureWrapper {
Settings.shared.preferences.textEditing.autocompleteBraces.toggle()
}
)
mgr.addCommand(
name: "Toggle Word Wrap",
title: "Toggle Word Wrap",
id: "prefs.text_editing.wrap_lines_to_editor_width",
command: CommandClosureWrapper {
Settings[\.textEditing].wrapLinesToEditorWidth.toggle()
}
)
}
struct IndentOption: Codable, Hashable {
var indentType: IndentType
// Kept even when `indentType` is `.tab` to retain the user's
// settings when changing `indentType`.
var spaceCount: Int = 4
enum IndentType: String, Codable {
case tab
case spaces
}
}
struct BracketPairHighlight: Codable, Hashable {
/// The type of highlight to use
var highlightType: HighlightType = .flash
var useCustomColor: Bool = false
/// The color to use for the highlight.
var color: Theme.Attributes = Theme.Attributes(color: "FFFFFF")
enum HighlightType: String, Codable {
case disabled
case bordered
case flash
case underline
}
}
}
struct EditorFont: Codable, Hashable {
/// The font size for the font
var size: Double = 12
/// The name of the custom font
var name: String = "SF Mono"
/// Default initializer
init() {}
/// Explicit decoder init for setting default values when key is not present in `JSON`
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name
}
/// Returns an NSFont representation of the current configuration.
///
/// Returns the custom font, if enabled and able to be instantiated.
/// Otherwise returns a default system font monospaced.
var current: NSFont {
return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
}
}
}