Skip to content

Commit ad754b7

Browse files
committed
Add IndentPicker
1 parent af2d71b commit ad754b7

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
1CB30C3A2DAA1C28008058A7 /* IndentPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CB30C392DAA1C28008058A7 /* IndentPicker.swift */; };
1011
61621C612C74FB2200494A4A /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 61621C602C74FB2200494A4A /* CodeEditSourceEditor */; };
1112
61CE772F2D19BF7D00908C57 /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 61CE772E2D19BF7D00908C57 /* CodeEditSourceEditor */; };
1213
61CE77322D19BFAA00908C57 /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 61CE77312D19BFAA00908C57 /* CodeEditSourceEditor */; };
@@ -22,6 +23,7 @@
2223
/* End PBXBuildFile section */
2324

2425
/* Begin PBXFileReference section */
26+
1CB30C392DAA1C28008058A7 /* IndentPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IndentPicker.swift; sourceTree = "<group>"; };
2527
6C13652A2B8A7B94004A1D18 /* CodeEditSourceEditorExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CodeEditSourceEditorExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
2628
6C13652D2B8A7B94004A1D18 /* CodeEditSourceEditorExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEditSourceEditorExampleApp.swift; sourceTree = "<group>"; };
2729
6C13652F2B8A7B94004A1D18 /* CodeEditSourceEditorExampleDocument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEditSourceEditorExampleDocument.swift; sourceTree = "<group>"; };
@@ -114,6 +116,7 @@
114116
children = (
115117
6C1365312B8A7B94004A1D18 /* ContentView.swift */,
116118
6C1365452B8A7F2D004A1D18 /* LanguagePicker.swift */,
119+
1CB30C392DAA1C28008058A7 /* IndentPicker.swift */,
117120
);
118121
path = Views;
119122
sourceTree = "<group>";
@@ -208,6 +211,7 @@
208211
6C1365302B8A7B94004A1D18 /* CodeEditSourceEditorExampleDocument.swift in Sources */,
209212
6C13652E2B8A7B94004A1D18 /* CodeEditSourceEditorExampleApp.swift in Sources */,
210213
6C1365442B8A7EED004A1D18 /* String+Lines.swift in Sources */,
214+
1CB30C3A2DAA1C28008058A7 /* IndentPicker.swift in Sources */,
211215
6C1365322B8A7B94004A1D18 /* ContentView.swift in Sources */,
212216
6C1365462B8A7F2D004A1D18 /* LanguagePicker.swift in Sources */,
213217
);

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct ContentView: View {
2626
@State private var isInLongParse = false
2727
@State private var settingsIsPresented: Bool = false
2828
@State private var treeSitterClient = TreeSitterClient()
29+
@State private var indentOption: IndentOption = .spaces(count: 4)
2930

3031
init(document: Binding<CodeEditSourceEditorExampleDocument>, fileURL: URL?) {
3132
self._document = document
@@ -40,6 +41,7 @@ struct ContentView: View {
4041
theme: theme,
4142
font: font,
4243
tabWidth: 4,
44+
indentOption: indentOption,
4345
lineHeight: 1.2,
4446
wrapLines: wrapLines,
4547
cursorPositions: $cursorPositions,
@@ -85,6 +87,8 @@ struct ContentView: View {
8587
.frame(height: 12)
8688
LanguagePicker(language: $language)
8789
.buttonStyle(.borderless)
90+
IndentPicker(indentOption: $indentOption, enabled: document.text.isEmpty)
91+
.buttonStyle(.borderless)
8892
}
8993
.font(.subheadline)
9094
.fontWeight(.medium)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import SwiftUI
2+
import CodeEditSourceEditor
3+
4+
struct IndentPicker: View {
5+
@Binding var indentOption: IndentOption
6+
let enabled: Bool
7+
8+
private let possibleIndents: [IndentOption] = [
9+
.spaces(count: 4),
10+
.spaces(count: 2),
11+
.tab
12+
]
13+
14+
var body: some View {
15+
Picker(
16+
"Indent",
17+
selection: $indentOption
18+
) {
19+
ForEach(possibleIndents, id: \.optionDescription) { indent in
20+
Text(indent.optionDescription)
21+
.tag(indent)
22+
}
23+
}
24+
.labelsHidden()
25+
.disabled(!enabled)
26+
}
27+
}
28+
29+
extension IndentOption {
30+
var optionDescription: String {
31+
switch self {
32+
case .spaces(count: let count):
33+
return "Spaces (\(count))"
34+
case .tab:
35+
return "Tab"
36+
}
37+
}
38+
}
39+
40+
#Preview {
41+
IndentPicker(indentOption: .constant(.spaces(count: 4)), enabled: true)
42+
}

Sources/CodeEditSourceEditor/Enums/IndentOption.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
/// Represents what to insert on a tab key press.
9-
public enum IndentOption: Equatable {
9+
public enum IndentOption: Equatable, Hashable {
1010
case spaces(count: Int)
1111
case tab
1212

0 commit comments

Comments
 (0)