-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCodeFileView.swift
More file actions
193 lines (170 loc) · 6.57 KB
/
CodeFileView.swift
File metadata and controls
193 lines (170 loc) · 6.57 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
//
// CodeFileView.swift
// CodeEditModules/CodeFile
//
// Created by Marco Carnevali on 17/03/22.
//
import Foundation
import SwiftUI
import CodeEditSourceEditor
import CodeEditTextView
import CodeEditLanguages
import Combine
/// CodeFileView is just a wrapper of the `CodeEditor` dependency
struct CodeFileView: View {
@ObservedObject private var codeFile: CodeFileDocument
/// The current cursor positions in the view
@State private var cursorPositions: [CursorPosition] = []
/// Any coordinators passed to the view.
private var textViewCoordinators: [TextViewCoordinator]
@AppSettings(\.textEditing.defaultTabWidth)
var defaultTabWidth
@AppSettings(\.textEditing.indentOption)
var indentOption
@AppSettings(\.textEditing.lineHeightMultiple)
var lineHeightMultiple
@AppSettings(\.textEditing.wrapLinesToEditorWidth)
var wrapLinesToEditorWidth
@AppSettings(\.textEditing.overscroll)
var overscroll
@AppSettings(\.textEditing.font)
var settingsFont
@AppSettings(\.theme.useThemeBackground)
var useThemeBackground
@AppSettings(\.theme.matchAppearance)
var matchAppearance
@AppSettings(\.textEditing.letterSpacing)
var letterSpacing
@AppSettings(\.textEditing.bracketHighlight)
var bracketHighlight
@AppSettings(\.textEditing.useSystemCursor)
var useSystemCursor
@Environment(\.colorScheme)
private var colorScheme
@ObservedObject private var themeModel: ThemeModel = .shared
private var cancellables = Set<AnyCancellable>()
private let isEditable: Bool
private let undoManager = CEUndoManager()
init(codeFile: CodeFileDocument, textViewCoordinators: [TextViewCoordinator] = [], isEditable: Bool = true) {
self._codeFile = .init(wrappedValue: codeFile)
self.textViewCoordinators = textViewCoordinators
+ [codeFile.contentCoordinator]
+ [codeFile.lspCoordinator].compactMap({ $0 })
self.isEditable = isEditable
if let openOptions = codeFile.openOptions {
codeFile.openOptions = nil
self.cursorPositions = openOptions.cursorPositions
}
codeFile
.contentCoordinator
.textUpdatePublisher
.sink { _ in
codeFile.updateChangeCount(.changeDone)
}
.store(in: &cancellables)
codeFile
.contentCoordinator
.textUpdatePublisher
.debounce(for: 1.0, scheduler: DispatchQueue.main)
.sink { _ in
// updateChangeCount is automatically managed by autosave(), so no manual call is necessary
codeFile.autosave(withImplicitCancellability: false) { error in
if let error {
CodeFileDocument.logger.error("Failed to autosave document, error: \(error)")
}
}
}
.store(in: &cancellables)
codeFile.undoManager = self.undoManager.manager
}
private var currentTheme: Theme {
themeModel.selectedTheme ?? themeModel.themes.first!
}
@State private var font: NSFont = Settings[\.textEditing].font.current
@State private var bracketPairHighlight: BracketPairHighlight? = {
let theme = ThemeModel.shared.selectedTheme ?? ThemeModel.shared.themes.first!
let color = Settings[\.textEditing].bracketHighlight.useCustomColor
? Settings[\.textEditing].bracketHighlight.color.nsColor
: theme.editor.text.nsColor.withAlphaComponent(0.8)
switch Settings[\.textEditing].bracketHighlight.highlightType {
case .disabled:
return nil
case .flash:
return .flash
case .bordered:
return .bordered(color: color)
case .underline:
return .underline(color: color)
}
}()
@Environment(\.edgeInsets)
private var edgeInsets
var body: some View {
CodeEditSourceEditor(
codeFile.content ?? NSTextStorage(),
language: codeFile.getLanguage(),
theme: currentTheme.editor.editorTheme,
font: font,
tabWidth: codeFile.defaultTabWidth ?? defaultTabWidth,
indentOption: (codeFile.indentOption ?? indentOption).textViewOption(),
lineHeight: lineHeightMultiple,
wrapLines: codeFile.wrapLines ?? wrapLinesToEditorWidth,
editorOverscroll: overscroll.overscrollPercentage,
cursorPositions: $cursorPositions,
useThemeBackground: useThemeBackground,
contentInsets: edgeInsets.nsEdgeInsets,
isEditable: isEditable,
letterSpacing: letterSpacing,
bracketPairHighlight: bracketPairHighlight,
useSystemCursor: useSystemCursor,
undoManager: undoManager,
coordinators: textViewCoordinators
)
.id(codeFile.fileURL)
.background {
if colorScheme == .dark {
EffectView(.underPageBackground)
} else {
EffectView(.contentBackground)
}
}
.colorScheme(currentTheme.appearance == .dark ? .dark : .light)
// minHeight zero fixes a bug where the app would freeze if the contents of the file are empty.
.frame(minHeight: .zero, maxHeight: .infinity)
.onChange(of: settingsFont) { newFontSetting in
font = newFontSetting.current
}
.onChange(of: bracketHighlight) { _ in
bracketPairHighlight = getBracketPairHighlight()
}
}
private func getBracketPairHighlight() -> BracketPairHighlight? {
let color = if Settings[\.textEditing].bracketHighlight.useCustomColor {
Settings[\.textEditing].bracketHighlight.color.nsColor
} else {
currentTheme.editor.text.nsColor.withAlphaComponent(0.8)
}
switch Settings[\.textEditing].bracketHighlight.highlightType {
case .disabled:
return nil
case .flash:
return .flash
case .bordered:
return .bordered(color: color)
case .underline:
return .underline(color: color)
}
}
}
// This extension is kept here because it should not be used elsewhere in the app and may cause confusion
// due to the similar type name from the CETV module.
private extension SettingsData.TextEditingSettings.IndentOption {
func textViewOption() -> IndentOption {
switch self.indentType {
case .spaces:
return IndentOption.spaces(count: spaceCount)
case .tab:
return IndentOption.tab
}
}
}