Skip to content

Commit

Permalink
Update dependencies and API (#4)
Browse files Browse the repository at this point in the history
* Update to use TextStoring

* Add indenter

* re-indent
  • Loading branch information
krzyzanowskim authored Aug 8, 2024
1 parent c92768d commit 89a64f3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 50 deletions.
3 changes: 3 additions & 0 deletions DemoApp/DemoApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
758BAB502A9BDFC200D840BF /* Products */,
75E2FDF02A9BE2260055DF08 /* Frameworks */,
);
indentWidth = 2;
sourceTree = "<group>";
tabWidth = 2;
usesTabs = 1;
};
758BAB502A9BDFC200D840BF /* Products */ = {
isa = PBXGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
{
"pins" : [
{
"identity" : "mainoffender",
"identity" : "rearrange",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattmassicotte/MainOffender",
"location" : "https://github.com/ChimeHQ/Rearrange",
"state" : {
"revision" : "343cc3797618c29b48b037b4e2beea0664e75315",
"version" : "0.1.0"
"revision" : "0fb658e721c68495f6340c211cc6d4719e6b52d8",
"version" : "1.6.0"
}
},
{
"identity" : "rearrange",
"identity" : "sttextkitplus",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/Rearrange",
"location" : "https://github.com/krzyzanowskim/STTextKitPlus",
"state" : {
"revision" : "0fb658e721c68495f6340c211cc6d4719e6b52d8",
"version" : "1.6.0"
"revision" : "87d700502fe9b64a7b29a553505ce94adc2be7da",
"version" : "0.1.4"
}
},
{
"identity" : "sttextview",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/STTextView",
"state" : {
"revision" : "177460e2d7cdcc62d021d0a2eb5a99a1ae36367c",
"version" : "0.8.13"
"revision" : "8c4b880259f122c5ff4283adc698ac0d5d4b9027",
"version" : "0.9.6"
}
},
{
"identity" : "textformation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/TextFormation",
"state" : {
"revision" : "b4987856bc860643ac2c9cdbc7d5f3e9ade68377",
"version" : "0.8.1"
"revision" : "b1ce9a14bd86042bba4de62236028dc4ce9db6a1",
"version" : "0.9.0"
}
},
{
"identity" : "textstory",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/TextStory",
"state" : {
"revision" : "8883fa739aa213e70e6cb109bfbf0a0b551e4cb5",
"version" : "0.8.0"
"revision" : "8dc9148b46fcf93b08ea9d4ef9bdb5e4f700e008",
"version" : "0.9.0"
}
}
],
Expand Down
20 changes: 12 additions & 8 deletions DemoApp/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ final class EditorViewController: NSViewController {
super.viewDidLoad()
view.frame.size = CGSize(width: 500, height: 500)

let filters = [
StandardOpenPairFilter(open: "[", close: "]")
let filters: [Filter] = [
StandardOpenPairFilter(open: "{", close: "}"),
StandardOpenPairFilter(open: "[", close: "]"),
StandardOpenPairFilter(open: "(", close: ")"),
]

let indenter = TextualIndenter(patterns: TextualIndenter.basicPatterns)

let providers = WhitespaceProviders(
leadingWhitespace: WhitespaceProviders.passthroughProvider,
leadingWhitespace: indenter.substitionProvider(indentationUnit: " ", width: 4),
trailingWhitespace: WhitespaceProviders.removeAllProvider
)

Expand All @@ -38,12 +42,12 @@ final class EditorViewController: NSViewController {
textView.font = .monospacedSystemFont(ofSize: 0, weight: .regular)

textView.string = """
import Foundation
import Foundation
func hello() {
print("Hello World!")
}
"""
func hello() {
print("Hello World!")
}
"""
}
}

Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ let package = Package(
.library(name: "TextFormationPlugin", targets: ["TextFormationPlugin"]),
],
dependencies: [
.package(url: "https://github.com/krzyzanowskim/STTextView", from: "0.8.13"),
.package(url: "https://github.com/ChimeHQ/TextFormation", from: "0.8.1"),
.package(url: "https://github.com/ChimeHQ/TextStory", from: "0.8.0"),
.package(url: "https://github.com/krzyzanowskim/STTextView", from: "0.9.6"),
.package(url: "https://github.com/ChimeHQ/TextFormation", from: "0.9.0"),
.package(url: "https://github.com/ChimeHQ/TextStory", from: "0.9.0"),
],
targets: [
.target(
Expand Down
69 changes: 44 additions & 25 deletions Sources/TextFormationPlugin/TextStorageAdapter+STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,68 @@ import STTextView
import TextStory
import TextFormation

extension STTextView {
fileprivate var contentStorage: NSTextContentStorage? {
textContentManager as? NSTextContentStorage
}
private final class TextStoringAdapter: TextStoring {
weak var textView: STTextView?

func applyMutation(_ mutation: TextMutation) {
guard let contentStorage = contentStorage else { return }
var length: Int {
MainActor.assumeIsolated {
// this works around a duplicate public 'length' method defined on NSTextContentStorage in STTextView
(textView?.contentStorage as TextStoring?)?.length ?? 0
}
}

if let manager = undoManager {
let inverse = contentStorage.inverseMutation(for: mutation)
init(textView: STTextView) {
self.textView = textView
}

manager.registerUndo(withTarget: self, handler: {
$0.applyMutation(inverse)
})
func substring(from range: NSRange) -> String? {
MainActor.assumeIsolated {
textView?.contentStorage?.substring(from: range)
}
}

func applyMutation(_ mutation: TextStory.TextMutation) {
MainActor.assumeIsolated {
guard let textView, let contentStorage = textView.contentStorage else {
return
}

if let manager = textView.undoManager {
let inverse = contentStorage.inverseMutation(for: mutation)

manager.registerUndo(withTarget: self, handler: { adapter in
contentStorage.performEditingTransaction {
adapter.applyMutation(inverse)
}
})
}

contentStorage.applyMutation(mutation)
textView.textWillChange(nil)

didChangeText()
contentStorage.performEditingTransaction {
contentStorage.applyMutation(mutation)
}

textView.didChangeText()
}
}
}

extension TextStorageAdapter {
@MainActor
public convenience init(textView: STTextView) {
self.init {
// this works around a duplicate public 'length' method defined on NSTextContentStorage in STTextView
(textView.contentStorage as TextStoring?)?.length ?? 0
} substringProvider: { range in
textView.contentStorage?.substring(from: range)
} mutationApplier: { mutation in
textView.applyMutation(mutation)
}
private extension STTextView {
var contentStorage: NSTextContentStorage? {
textContentManager as? NSTextContentStorage
}
}

extension TextInterfaceAdapter {

@MainActor
public convenience init(textView: STTextView) {
self.init(
getSelection: { textView.selectedRange() },
setSelection: { textView.setSelectedRange($0) },
storage: TextStorageAdapter(textView: textView)
storage: TextStoringAdapter(textView: textView)
)
}
}

0 comments on commit 89a64f3

Please sign in to comment.