File tree Expand file tree Collapse file tree 2 files changed +29
-10
lines changed
Sources/CodeEditTextView/TextView Expand file tree Collapse file tree 2 files changed +29
-10
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,21 @@ import AppKit
99
1010extension TextView {
1111 override public func insertNewline( _ sender: Any ? ) {
12+ var attachments : [ AnyTextAttachment ] = selectionManager. textSelections. compactMap ( { selection in
13+ let content = layoutManager. contentRun ( at: selection. range. location)
14+ if case let . attachment( attachment) = content? . data, attachment. range == selection. range {
15+ return attachment
16+ }
17+ return nil
18+ } )
19+
20+ if !attachments. isEmpty {
21+ for attachment in attachments. sorted ( by: { $0. range. location > $1. range. location } ) {
22+ performAttachmentAction ( attachment: attachment)
23+ }
24+ return
25+ }
26+
1227 insertText ( layoutManager. detectedLineEnding. rawValue)
1328 }
1429
Original file line number Diff line number Diff line change @@ -87,21 +87,25 @@ extension TextView {
8787 case 1 :
8888 selectionManager. setSelectedRange ( attachment. range)
8989 case 2 :
90- let action = attachment. attachment. attachmentAction ( )
91- switch action {
92- case . none:
93- return
94- case . discard:
95- layoutManager. attachments. remove ( atOffset: offset)
96- selectionManager. setSelectedRange ( NSRange ( location: attachment. range. location, length: 0 ) )
97- case let . replace( text) :
98- replaceCharacters ( in: attachment. range, with: text)
99- }
90+ performAttachmentAction ( attachment: attachment)
10091 default :
10192 break
10293 }
10394 }
10495
96+ func performAttachmentAction( attachment: AnyTextAttachment ) {
97+ let action = attachment. attachment. attachmentAction ( )
98+ switch action {
99+ case . none:
100+ return
101+ case . discard:
102+ layoutManager. attachments. remove ( atOffset: attachment. range. location)
103+ selectionManager. setSelectedRange ( NSRange ( location: attachment. range. location, length: 0 ) )
104+ case let . replace( text) :
105+ replaceCharacters ( in: attachment. range, with: text)
106+ }
107+ }
108+
105109 override public func mouseUp( with event: NSEvent ) {
106110 mouseDragAnchor = nil
107111 disableMouseAutoscrollTimer ( )
You can’t perform that action at this time.
0 commit comments