-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTextEditingSettings.swift
More file actions
350 lines (293 loc) Β· 12.8 KB
/
TextEditingSettings.swift
File metadata and controls
350 lines (293 loc) Β· 12.8 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// 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] {
var keys = [
"Prefer Indent Using",
"Tab Width",
"Wrap lines to editor width",
"Indent wrapped lines",
"Editor Overscroll",
"Font",
"Font Size",
"Font Weight",
"Line Height",
"Letter Spacing",
"Autocomplete braces",
"Enable type-over completion",
"Bracket Pair Emphasis",
"Bracket Pair Highlight",
"Show Gutter",
"Show Minimap",
"Reformat at Column",
"Show Reformatting Guide",
"Invisibles",
"Warning Characters"
]
if #available(macOS 14.0, *) {
keys.append("System Cursor")
}
return keys.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
/// Spaces to indent continuation lines when line wrapping
/// is on (e.g. 4 or 12). See:
/// https://github.com/CodeEditApp/CodeEditTextView/issues/18
var wrappedLineIndent: Int = 4
/// The percentage of overscroll to apply to the text view
var overscroll: OverscrollOption = .medium
/// A multiplier for setting the line height. Defaults to `1.2`
var lineHeightMultiple: Double = 1.2
/// 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 bracketEmphasis: BracketPairEmphasis = BracketPairEmphasis()
/// Use the system cursor for the source editor.
var useSystemCursor: Bool = true
/// Toggle the gutter in the editor.
var showGutter: Bool = true
/// Toggle the minimap in the editor.
var showMinimap: Bool = true
/// Toggle the code folding ribbon.
var showFoldingRibbon: Bool = true
/// The column at which to reformat text
var reformatAtColumn: Int = 80
/// Show the reformatting guide in the editor
var showReformattingGuide: Bool = false
var invisibleCharacters: InvisibleCharactersConfig = .default
/// Map of unicode character codes to a note about them
var warningCharacters: WarningCharacters = .default
/// Default initializer
init() {
self.populateCommands()
}
/// Explicit decoder init for setting default values when key is not present in `JSON`
init(from decoder: Decoder) throws { // swiftlint:disable:this function_body_length
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.wrappedLineIndent = try container.decodeIfPresent(
Int.self,
forKey: .wrappedLineIndent
) ?? 4
self.overscroll = try container.decodeIfPresent(
OverscrollOption.self,
forKey: .overscroll
) ?? .medium
self.lineHeightMultiple = try container.decodeIfPresent(
Double.self,
forKey: .lineHeightMultiple
) ?? 1.2
self.letterSpacing = try container.decodeIfPresent(
Double.self,
forKey: .letterSpacing
) ?? 1
self.bracketEmphasis = try container.decodeIfPresent(
BracketPairEmphasis.self,
forKey: .bracketEmphasis
) ?? BracketPairEmphasis()
if #available(macOS 14, *) {
self.useSystemCursor = try container.decodeIfPresent(Bool.self, forKey: .useSystemCursor) ?? true
} else {
self.useSystemCursor = false
}
self.showGutter = try container.decodeIfPresent(Bool.self, forKey: .showGutter) ?? true
self.showMinimap = try container.decodeIfPresent(Bool.self, forKey: .showMinimap) ?? true
self.showFoldingRibbon = try container.decodeIfPresent(Bool.self, forKey: .showFoldingRibbon) ?? true
self.reformatAtColumn = try container.decodeIfPresent(Int.self, forKey: .reformatAtColumn) ?? 80
self.showReformattingGuide = try container.decodeIfPresent(
Bool.self,
forKey: .showReformattingGuide
) ?? false
self.invisibleCharacters = try container.decodeIfPresent(
InvisibleCharactersConfig.self,
forKey: .invisibleCharacters
) ?? .default
self.warningCharacters = try container.decodeIfPresent(
WarningCharacters.self,
forKey: .warningCharacters
) ?? .default
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: {
Settings[\.textEditing].enableTypeOverCompletion.toggle()
}
)
mgr.addCommand(
name: "Toggle Autocomplete Braces",
title: "Toggle Autocomplete Braces",
id: "prefs.text_editing.autocomplete_braces",
command: {
Settings[\.textEditing].autocompleteBraces.toggle()
}
)
mgr.addCommand(
name: "Toggle Word Wrap",
title: "Toggle Word Wrap",
id: "prefs.text_editing.wrap_lines_to_editor_width",
command: {
Settings[\.textEditing].wrapLinesToEditorWidth.toggle()
}
)
mgr.addCommand(name: "Toggle Minimap", title: "Toggle Minimap", id: "prefs.text_editing.toggle_minimap") {
Settings[\.textEditing].showMinimap.toggle()
}
mgr.addCommand(name: "Toggle Gutter", title: "Toggle Gutter", id: "prefs.text_editing.toggle_gutter") {
Settings[\.textEditing].showGutter.toggle()
}
mgr.addCommand(
name: "Toggle Folding Ribbon",
title: "Toggle Folding Ribbon",
id: "prefs.text_editing.toggle_folding_ribbon"
) {
Settings[\.textEditing].showFoldingRibbon.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 BracketPairEmphasis: 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", bold: false, italic: false)
enum HighlightType: String, Codable {
case disabled
case bordered
case flash
case underline
}
}
enum OverscrollOption: String, Codable {
case none
case small
case medium
case large
var overscrollPercentage: CGFloat {
switch self {
case .none: return 0
case .small: return 0.25
case .medium: return 0.5
case .large: return 0.75
}
}
}
struct InvisibleCharactersConfig: Equatable, Hashable, Codable {
static var `default`: InvisibleCharactersConfig = {
InvisibleCharactersConfig(
enabled: false,
showSpaces: true,
showTabs: true,
showLineEndings: true
)
}()
var enabled: Bool
var showSpaces: Bool
var showTabs: Bool
var showLineEndings: Bool
var spaceReplacement: String = "Β·"
var tabReplacement: String = "β"
// Controlled by `showLineEndings`
var carriageReturnReplacement: String = "β΅"
var lineFeedReplacement: String = "Β¬"
var paragraphSeparatorReplacement: String = "ΒΆ"
var lineSeparatorReplacement: String = "β"
}
struct WarningCharacters: Equatable, Hashable, Codable {
static let `default`: WarningCharacters = WarningCharacters(enabled: true, characters: [
0x0003: "End of text",
0x00A0: "Non-breaking space",
0x202F: "Narrow non-breaking space",
0x200B: "Zero-width space",
0x200C: "Zero-width non-joiner",
0x2029: "Paragraph separator",
0x2013: "Em-dash",
0x00AD: "Soft hyphen",
0x2018: "Left single quote",
0x2019: "Right single quote",
0x201C: "Left double quote",
0x201D: "Right double quote",
0x037E: "Greek Question Mark"
])
var enabled: Bool
var characters: [UInt16: String]
}
}
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"
/// The weight of the custom font
var weight: NSFont.Weight = .medium
/// 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
self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight
}
/// 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 {
let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight)
return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
}
}
}