Skip to content

Commit

Permalink
(fix: #233) Fix TextFormation editing (#239)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
thecoolwinter authored Mar 1, 2024
1 parent a5581a2 commit a72e6c9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
"state" : {
"revision" : "6653c21a603babf365a12d4d331fadc8f8b52d99",
"version" : "0.7.2"
"revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc",
"version" : "0.7.3"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
// A fast, efficient, text view for code.
.package(
url: "https://github.com/CodeEditApp/CodeEditTextView.git",
from: "0.7.2"
from: "0.7.3"
),
// tree-sitter languages
.package(
Expand Down
17 changes: 17 additions & 0 deletions Sources/CodeEditSourceEditor/Extensions/TextMutation+isEmpty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TextMutation+isEmpty.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 3/1/24.
//

import TextStory

extension TextMutation {
/// Determines if the mutation is an empty mutation.
///
/// Will return `true` if the mutation is neither a delete operation nor an insert operation.
var isEmpty: Bool {
self.string.isEmpty && self.range.isEmpty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@ extension TextView: TextInterface {
}

/// Applies the mutation to the text view.
///
/// If the mutation is empty it will be ignored.
///
/// - Parameter mutation: The mutation to apply.
public func applyMutation(_ mutation: TextMutation) {
guard !mutation.isEmpty else { return }

layoutManager.beginTransaction()
textStorage.beginEditing()

layoutManager.willReplaceCharactersInRange(range: mutation.range, with: mutation.string)
_undoManager?.registerMutation(mutation)
textStorage.replaceCharacters(in: mutation.range, with: mutation.string)
selectionManager.didReplaceCharacters(
in: mutation.range,
replacementLength: (mutation.string as NSString).length
)
textStorage.replaceCharacters(in: mutation.range, with: mutation.string)

textStorage.endEditing()
layoutManager.endTransaction()
}
}
10 changes: 7 additions & 3 deletions Sources/CodeEditSourceEditor/Filters/TabReplacementFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ struct TabReplacementFilter: Filter {
with providers: WhitespaceProviders
) -> FilterAction {
if mutation.string == "\t" && indentOption != .tab && mutation.delta > 0 {
interface.applyMutation(TextMutation(insert: indentOption.stringValue,
at: mutation.range.location,
limit: mutation.limit))
interface.applyMutation(
TextMutation(
insert: indentOption.stringValue,
at: mutation.range.location,
limit: mutation.limit
)
)
return .discard
} else {
return .none
Expand Down

0 comments on commit a72e6c9

Please sign in to comment.