@@ -497,6 +497,7 @@ open class TextView: NSView {
497497 textViewController. layoutIfNeeded ( )
498498 textViewController. handleContentSizeUpdateIfNeeded ( )
499499 updateCaretFrame ( )
500+ updateSelectedRectangles ( )
500501 }
501502
502503 override public func layoutSubtreeIfNeeded( ) {
@@ -517,6 +518,33 @@ open class TextView: NSView {
517518 }
518519 }
519520
521+ override public func mouseDown( with event: NSEvent ) {
522+ super. mouseDown ( with: event)
523+ if let location = locationClosestToPoint ( in: event) {
524+ textViewController. move ( to: location)
525+ textViewController. startDraggingSelection ( from: location)
526+ }
527+ }
528+
529+ override public func mouseDragged( with event: NSEvent ) {
530+ super. mouseDragged ( with: event)
531+ if let location = locationClosestToPoint ( in: event) {
532+ textViewController. extendDraggedSelection ( to: location)
533+ }
534+ }
535+
536+ override public func mouseUp( with event: NSEvent ) {
537+ super. mouseUp ( with: event)
538+ if let location = locationClosestToPoint ( in: event) {
539+ textViewController. extendDraggedSelection ( to: location)
540+ }
541+ }
542+
543+ override public func resetCursorRects( ) {
544+ super. resetCursorRects ( )
545+ addCursorRect ( bounds, cursor: . iBeam)
546+ }
547+
520548 /// Sets the current _state_ of the editor. The state contains the text to be displayed by the editor and
521549 /// various additional information about the text that the editor needs to show the text.
522550 ///
@@ -538,7 +566,6 @@ public extension TextView {
538566 guard var selectedRange = textViewController. markedRange ?? textViewController. selectedRange? . nonNegativeLength else {
539567 return
540568 }
541- textViewController. selectionService. resetPreviouslySelectedRange ( )
542569 if selectedRange. length == 0 {
543570 selectedRange. location -= 1
544571 selectedRange. length = 1
@@ -704,11 +731,6 @@ public extension TextView {
704731 textViewController. moveToEndOfDocumentAndModifySelection ( )
705732 }
706733
707- override func mouseDown( with event: NSEvent ) {
708- let point = scrollContentView. convert ( event. locationInWindow, from: nil )
709- textViewController. moveToLocation ( closestTo: point)
710- }
711-
712734 /// Copy the selected text.
713735 ///
714736 /// - Parameter sender: The object calling this method.
@@ -726,7 +748,6 @@ public extension TextView {
726748 @objc func paste( _ sender: Any ? ) {
727749 let selectedRange = selectedRange ( )
728750 if let string = NSPasteboard . general. string ( forType: . string) {
729- print ( string)
730751 let preparedText = textViewController. prepareTextForInsertion ( string)
731752 textViewController. replaceText ( in: selectedRange, with: preparedText)
732753 }
@@ -881,6 +902,15 @@ private extension TextView {
881902 }
882903}
883904
905+ // MARK: - Location
906+ private extension TextView {
907+ private func locationClosestToPoint( in event: NSEvent ) -> Int ? {
908+ let point = scrollContentView. convert ( event. locationInWindow, from: nil )
909+ let adjustedPoint = CGPoint ( x: point. x - gutterWidth - textContainerInset. left, y: point. y)
910+ return textViewController. layoutManager. closestIndex ( to: adjustedPoint)
911+ }
912+ }
913+
884914// MARK: - TextViewControllerDelegate
885915extension TextView : TextViewControllerDelegate {
886916 func textViewControllerDidChangeText( _ textViewController: TextViewController ) {
0 commit comments