Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix: #233) Fix TextFormation editing #239

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading