Skip to content

Commit a72e6c9

Browse files
(fix: #233) Fix TextFormation editing (#239)
### Description - Cleans up undo/redo operations when TextFormation is activated, for instance when inserting or deleting a newline. > ~Note: Requires [CodeEditTextView#25](CodeEditApp/CodeEditTextView#25) to be merged.~ Merged! ### Related Issues * closes #233 ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots https://github.com/CodeEditApp/CodeEditSourceEditor/assets/35942988/d842b77d-60b4-4afd-be6c-2fb3d0342c4c
1 parent a5581a2 commit a72e6c9

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
1616
"state" : {
17-
"revision" : "6653c21a603babf365a12d4d331fadc8f8b52d99",
18-
"version" : "0.7.2"
17+
"revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc",
18+
"version" : "0.7.3"
1919
}
2020
},
2121
{

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
// A fast, efficient, text view for code.
1818
.package(
1919
url: "https://github.com/CodeEditApp/CodeEditTextView.git",
20-
from: "0.7.2"
20+
from: "0.7.3"
2121
),
2222
// tree-sitter languages
2323
.package(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// TextMutation+isEmpty.swift
3+
// CodeEditSourceEditor
4+
//
5+
// Created by Khan Winter on 3/1/24.
6+
//
7+
8+
import TextStory
9+
10+
extension TextMutation {
11+
/// Determines if the mutation is an empty mutation.
12+
///
13+
/// Will return `true` if the mutation is neither a delete operation nor an insert operation.
14+
var isEmpty: Bool {
15+
self.string.isEmpty && self.range.isEmpty
16+
}
17+
}

Sources/CodeEditSourceEditor/Extensions/TextView+/TextView+TextFormation.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,25 @@ extension TextView: TextInterface {
3333
}
3434

3535
/// Applies the mutation to the text view.
36+
///
37+
/// If the mutation is empty it will be ignored.
38+
///
3639
/// - Parameter mutation: The mutation to apply.
3740
public func applyMutation(_ mutation: TextMutation) {
41+
guard !mutation.isEmpty else { return }
42+
43+
layoutManager.beginTransaction()
44+
textStorage.beginEditing()
45+
3846
layoutManager.willReplaceCharactersInRange(range: mutation.range, with: mutation.string)
47+
_undoManager?.registerMutation(mutation)
48+
textStorage.replaceCharacters(in: mutation.range, with: mutation.string)
3949
selectionManager.didReplaceCharacters(
4050
in: mutation.range,
4151
replacementLength: (mutation.string as NSString).length
4252
)
43-
textStorage.replaceCharacters(in: mutation.range, with: mutation.string)
53+
54+
textStorage.endEditing()
55+
layoutManager.endTransaction()
4456
}
4557
}

Sources/CodeEditSourceEditor/Filters/TabReplacementFilter.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ struct TabReplacementFilter: Filter {
2020
with providers: WhitespaceProviders
2121
) -> FilterAction {
2222
if mutation.string == "\t" && indentOption != .tab && mutation.delta > 0 {
23-
interface.applyMutation(TextMutation(insert: indentOption.stringValue,
24-
at: mutation.range.location,
25-
limit: mutation.limit))
23+
interface.applyMutation(
24+
TextMutation(
25+
insert: indentOption.stringValue,
26+
at: mutation.range.location,
27+
limit: mutation.limit
28+
)
29+
)
2630
return .discard
2731
} else {
2832
return .none

0 commit comments

Comments
 (0)