forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
105 lines (98 loc) · 4.1 KB
/
ContentView.swift
File metadata and controls
105 lines (98 loc) · 4.1 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
//
// ContentView.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//
import SwiftUI
import CodeEditSourceEditor
import CodeEditLanguages
import CodeEditTextView
struct ContentView: View {
@Environment(\.colorScheme)
var colorScheme
@Binding var document: CodeEditSourceEditorExampleDocument
let fileURL: URL?
@State private var language: CodeLanguage = .default
@State private var theme: EditorTheme = .light
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .medium)
@AppStorage("wrapLines") private var wrapLines: Bool = true
@State private var cursorPositions: [CursorPosition] = [.init(line: 1, column: 1)]
@AppStorage("systemCursor") private var useSystemCursor: Bool = false
@State private var isInLongParse = false
@State private var settingsIsPresented: Bool = false
@State private var treeSitterClient = TreeSitterClient()
@AppStorage("showMinimap") private var showMinimap: Bool = true
@State private var indentOption: IndentOption = .spaces(count: 4)
@AppStorage("reformatAtColumn") private var reformatAtColumn: Int = 80
@AppStorage("showReformattingGuide") private var showReformattingGuide: Bool = false
@State private var invisibleCharactersConfig: InvisibleCharactersConfig = .empty
init(document: Binding<CodeEditSourceEditorExampleDocument>, fileURL: URL?) {
self._document = document
self.fileURL = fileURL
}
var body: some View {
GeometryReader { proxy in
CodeEditSourceEditor(
document.text,
language: language,
theme: theme,
font: font,
tabWidth: 4,
indentOption: indentOption,
lineHeight: 1.2,
wrapLines: wrapLines,
editorOverscroll: 0.3,
cursorPositions: $cursorPositions,
useThemeBackground: true,
highlightProviders: [treeSitterClient],
contentInsets: NSEdgeInsets(top: proxy.safeAreaInsets.top, left: 0, bottom: 28.0, right: 0),
additionalTextInsets: NSEdgeInsets(top: 1, left: 0, bottom: 1, right: 0),
useSystemCursor: useSystemCursor,
showMinimap: showMinimap,
reformatAtColumn: reformatAtColumn,
showReformattingGuide: showReformattingGuide,
invisibleCharactersConfig: invisibleCharactersConfig
)
.overlay(alignment: .bottom) {
StatusBar(
fileURL: fileURL,
document: $document,
wrapLines: $wrapLines,
useSystemCursor: $useSystemCursor,
cursorPositions: $cursorPositions,
isInLongParse: $isInLongParse,
language: $language,
theme: $theme,
showMinimap: $showMinimap,
indentOption: $indentOption,
reformatAtColumn: $reformatAtColumn,
showReformattingGuide: $showReformattingGuide,
invisibles: $invisibleCharactersConfig
)
}
.ignoresSafeArea()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParse)) { _ in
withAnimation(.easeIn(duration: 0.1)) {
isInLongParse = true
}
}
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParseFinished)) { _ in
withAnimation(.easeIn(duration: 0.1)) {
isInLongParse = false
}
}
.onChange(of: colorScheme) { _, newValue in
if newValue == .dark {
theme = .dark
} else {
theme = .light
}
}
}
}
}
#Preview {
ContentView(document: .constant(CodeEditSourceEditorExampleDocument()), fileURL: nil)
}