diff --git a/Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 6b45b7e63..d312fc00e 100644 --- a/Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -18,15 +18,6 @@ "version" : "0.2.3" } }, - { - "identity" : "codeedittextview", - "kind" : "remoteSourceControl", - "location" : "https://github.com/CodeEditApp/CodeEditTextView.git", - "state" : { - "revision" : "d7ac3f11f22ec2e820187acce8f3a3fb7aa8ddec", - "version" : "0.12.1" - } - }, { "identity" : "rearrange", "kind" : "remoteSourceControl", diff --git a/Package.resolved b/Package.resolved index ebadf1983..027ac4282 100644 --- a/Package.resolved +++ b/Package.resolved @@ -18,22 +18,13 @@ "version" : "0.2.3" } }, - { - "identity" : "codeedittextview", - "kind" : "remoteSourceControl", - "location" : "https://github.com/CodeEditApp/CodeEditTextView.git", - "state" : { - "revision" : "d7ac3f11f22ec2e820187acce8f3a3fb7aa8ddec", - "version" : "0.12.1" - } - }, { "identity" : "rearrange", "kind" : "remoteSourceControl", "location" : "https://github.com/ChimeHQ/Rearrange", "state" : { - "revision" : "f1d74e1642956f0300756ad8d1d64e9034857bc3", - "version" : "2.0.0" + "revision" : "4de8be41dba304192e87dc0a11e0aa39e72aa2e8", + "version" : "2.1.1" } }, { @@ -41,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections.git", "state" : { - "revision" : "8c0c0a8b49e080e54e5e328cc552821ff07cd341", - "version" : "1.2.1" + "revision" : "a0cb0954ecb21e4e31b0070e6ed5674e8556685a", + "version" : "1.6.0" } }, { @@ -86,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/ChimeHQ/TextStory", "state" : { - "revision" : "91df6fc9bd817f9712331a4a3e826f7bdc823e1d", - "version" : "0.9.1" + "revision" : "a52db1a2eca74d37c8c19bf3165416941632ed45", + "version" : "0.9.2" } }, { diff --git a/Package.swift b/Package.swift index da9e3832c..bf292f9b0 100644 --- a/Package.swift +++ b/Package.swift @@ -15,10 +15,11 @@ let package = Package( ], dependencies: [ // A fast, efficient, text view for code. - .package( - url: "https://github.com/CodeEditApp/CodeEditTextView.git", - from: "0.12.1" - ), +// .package( +// url: "https://github.com/CodeEditApp/CodeEditTextView.git", +// from: "0.12.1" +// ), + .package(name: "CodeEditTextView", path: "../CodeEditTextViewRemaster"), // tree-sitter languages .package( url: "https://github.com/CodeEditApp/CodeEditLanguages.git", diff --git a/Sources/CodeEditSourceEditor/Controller/TextScrollView.swift b/Sources/CodeEditSourceEditor/Controller/TextScrollView.swift new file mode 100644 index 000000000..98de52bbc --- /dev/null +++ b/Sources/CodeEditSourceEditor/Controller/TextScrollView.swift @@ -0,0 +1,26 @@ +// +// TextScrollView.swift +// CodeEditSourceEditor +// + +import AppKit + +/// The editor's scroll view. Its content insets pad the text above the first line and below the last, and a click +/// in that padding lands on the scroll view itself, which ignores it. Those clicks go to the text view instead, which +/// places the caret on the nearest line: the first line above the text, the end of the document below it. +final class TextScrollView: NSScrollView { + override init(frame frameRect: NSRect) { + super.init(frame: frameRect) + documentCursor = .iBeam + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func hitTest(_ point: NSPoint) -> NSView? { + let hit = super.hitTest(point) + guard hit === self, let documentView else { return hit } + return documentView + } +} diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift index ed2791d27..e410c6de9 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+Cursor.swift @@ -7,8 +7,27 @@ import Foundation import AppKit +import CodeEditTextView extension TextViewController { + /// Resolves a 1-indexed column on `line` to a UTF-16 offset, clamped to the line's content. Restored state and + /// LSP positions can be out of range, and must not spill onto the next line. + func characterOffset(in line: TextLineStorage.TextLinePosition, forColumn column: Int) -> Int { + let range = line.range + let string = textView.textStorage.string as NSString + var contentLength = range.length + if contentLength > 0 { + let last = string.character(at: range.max - 1) + if last == 0x0A || last == 0x0D { // \n or \r + contentLength -= 1 + if last == 0x0A, contentLength > 0, string.character(at: range.max - 2) == 0x0D { // \r\n + contentLength -= 1 + } + } + } + return range.location + min(max(column - 1, 0), contentLength) + } + /// Sets new cursor positions. /// - Parameter positions: The positions to set. Lines and columns are 1-indexed. public func setCursorPositions(_ positions: [CursorPosition], scrollToVisible: Bool = false) { @@ -26,16 +45,14 @@ extension TextViewController { newSelectedRanges.append(NSRange(location: 0, length: 0)) } else if let linePosition = textView.layoutManager.textLineForIndex(position.start.line - 1) { // If this is a valid line, set the new position - let startCharacter = linePosition.range.lowerBound + min( - linePosition.range.upperBound, - position.start.column - 1 - ) + let startCharacter = characterOffset(in: linePosition, forColumn: position.start.column) if let end = position.end, let endLine = textView.layoutManager.textLineForIndex(end.line - 1) { - let endCharacter = endLine.range.lowerBound + min( - endLine.range.upperBound, - end.column - 1 - ) - newSelectedRanges.append(NSRange(start: startCharacter, end: endCharacter)) + let endCharacter = characterOffset(in: endLine, forColumn: end.column) + // An end before the start would make a negative-length range. + newSelectedRanges.append(NSRange( + start: min(startCharacter, endCharacter), + end: max(startCharacter, endCharacter) + )) } else { newSelectedRanges.append(NSRange(location: startCharacter, length: 0)) } @@ -96,13 +113,14 @@ extension TextViewController { let linePosition = textView.layoutManager.textLineForIndex(position.start.line - 1) else { return nil } - if position.end != nil { - range = NSRange( - location: linePosition.range.location + position.start.column, - length: linePosition.range.max - ) + let startOffset = characterOffset(in: linePosition, forColumn: position.start.column) + if let endPos = position.end, + endPos.line > 0, endPos.column > 0, + let endLinePosition = textView.layoutManager.textLineForIndex(endPos.line - 1) { + let endOffset = characterOffset(in: endLinePosition, forColumn: endPos.column) + range = NSRange(start: min(startOffset, endOffset), end: max(startOffset, endOffset)) } else { - range = NSRange(location: linePosition.range.location + position.start.column, length: 0) + range = NSRange(location: startOffset, length: 0) } } diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+EmphasizeBracket.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+EmphasizeBracket.swift index c4c8633b1..664a3e22f 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+EmphasizeBracket.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+EmphasizeBracket.swift @@ -9,6 +9,10 @@ import AppKit import CodeEditTextView extension TextViewController { + /// How far from the cursor to scan for a matching bracket, in UTF-16 units. Anchored on the cursor rather than the + /// visible range, which is empty until the text view's first layout. + private static let bracketSearchDistance = 4096 + /// Emphasizes bracket pairs using the current selection. internal func emphasizeSelectionPairs() { guard let bracketPairEmphasis = configuration.appearance.bracketPairEmphasis else { return } @@ -37,7 +41,7 @@ extension TextViewController { pair.0, pair.1, from: range.location, - limit: min((textView.visibleTextRange ?? .zero).max + 4096, textView.documentRange.max), + limit: range.location + Self.bracketSearchDistance, reverse: false ) { emphasizeCharacter(characterIndex) @@ -52,7 +56,7 @@ extension TextViewController { pair.1, pair.0, from: range.location - 1, - limit: max((textView.visibleTextRange?.location ?? 0) - 4096, textView.documentRange.location), + limit: range.location - Self.bracketSearchDistance, reverse: true ) { emphasizeCharacter(characterIndex) @@ -82,36 +86,39 @@ extension TextViewController { /// - from: The index to start from. This should not include the start character. Eg given `"{ }"` looking forward /// the index should be `1` /// - limit: A limiting index to stop at. When `reverse` is `true`, this is the minimum index. When `false` this - /// is the maximum index. + /// is the maximum index. Clamped to the document. /// - reverse: Set to `true` to walk backwards from `from`. /// - Returns: The index of the found closing pair, if any. internal func findClosingPair(_ close: String, _ open: String, from: Int, limit: Int, reverse: Bool) -> Int? { - // Walk the text, counting each close. When we find an open that makes closeCount < 0, return that index. - var options: NSString.EnumerationOptions = .byCaretPositions - if reverse { - options = options.union(.reverse) - } - var closeCount = 0 - var index: Int? - textView.textStorage.mutableString.enumerateSubstrings( - in: reverse ? - NSRange(location: limit, length: from - limit) : - NSRange(location: from, length: limit - from), - options: options, - using: { substring, range, _, stop in - if substring == close { + // Brackets are single UTF-16 units, so the window is scanned as raw code units. + guard let closeUnit = close.utf16.first, let openUnit = open.utf16.first else { return nil } + let documentRange = textView.documentRange + let lowerBound = max(reverse ? limit : from, documentRange.location) + let upperBound = min(reverse ? from : limit, documentRange.max) + guard lowerBound < upperBound else { return nil } + let length = upperBound - lowerBound + let string = textView.textStorage.string as NSString + + return withUnsafeTemporaryAllocation(of: unichar.self, capacity: length) { buffer -> Int? in + guard let units = buffer.baseAddress else { return nil } + string.getCharacters(units, range: NSRange(location: lowerBound, length: length)) + // Count each close. The open that takes the count below zero is the match. + var closeCount = 0 + let step = reverse ? -1 : 1 + var offset = reverse ? length - 1 : 0 + while offset >= 0 && offset < length { + if units[offset] == closeUnit { closeCount += 1 - } else if substring == open { + } else if units[offset] == openUnit { closeCount -= 1 + if closeCount < 0 { + return lowerBound + offset + } } - - if closeCount < 0 { - index = range.location - stop.pointee = true - } + offset += step } - ) - return index + return nil + } } /// Adds a temporary emphasis effect to the character at the given location. diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift index caca6be0c..aeac2ebc5 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+Lifecycle.swift @@ -29,7 +29,7 @@ extension TextViewController { override public func loadView() { super.loadView() - scrollView = NSScrollView() + scrollView = TextScrollView() scrollView.documentView = textView gutterView = GutterView( @@ -147,6 +147,7 @@ extension TextViewController { self.gutterView.frame.origin.y = self.textView.frame.origin.y - self.scrollView.contentInsets.top self.gutterView.needsDisplay = true self.gutterView.foldingRibbon.needsDisplay = true + self.gutterView.gitChangeIndicator.needsDisplay = true self.reformattingGuideView?.updatePosition(in: self) self.scrollView.needsLayout = true } @@ -165,7 +166,7 @@ extension TextViewController { func setUpAppearanceChangedObserver() { NSApp.publisher(for: \.effectiveAppearance) - .receive(on: RunLoop.main) + .receive(on: DispatchQueue.main) .sink { [weak self] newValue in guard let self = self else { return } diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift index e59e5c10e..4087ce275 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift @@ -58,6 +58,13 @@ extension TextViewController { scrollView.contentInsets.bottom += additionalTextInsets?.bottom ?? 0 minimapView.scrollView.contentInsets.top += additionalTextInsets?.top ?? 0 minimapView.scrollView.contentInsets.bottom += additionalTextInsets?.bottom ?? 0 + // That padding is still readable area, so the text view mustn't treat it as covered by chrome. + textView.contentInsetPadding = NSEdgeInsets( + top: additionalTextInsets?.top ?? 0, + left: 0, + bottom: additionalTextInsets?.bottom ?? 0, + right: 0 + ) // Inset the top by the find panel height let findInset: CGFloat = if findViewController?.viewModel.isShowingFindPanel ?? false { diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index fb95c81c1..4e71aa1ec 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -35,7 +35,7 @@ public class TextViewController: NSViewController { var reformattingGuideView: ReformattingGuideView! /// Middleman between the text view to our invisible characters config, with knowledge of things like the - /// /// user's theme and indent option to help correctly draw invisible character placeholders. + /// user's theme and indent option to help correctly draw invisible character placeholders. var invisibleCharactersCoordinator: InvisibleCharactersCoordinator var minimapXConstraint: NSLayoutConstraint? @@ -197,6 +197,14 @@ public class TextViewController: NSViewController { var foldProvider: LineFoldProvider + /// The current git change indicators to display in the gutter. + /// Set this from the host app when dirty diff data changes. + public var gutterChanges: [GutterChange] = [] { + didSet { + gutterView?.gitChangeIndicator.changes = gutterChanges + } + } + /// Filters used when applying edits.. var textFilters: [TextFormation.Filter] = [] diff --git a/Sources/CodeEditSourceEditor/Extensions/DispatchQueue+dispatchMainIfNot.swift b/Sources/CodeEditSourceEditor/Extensions/DispatchQueue+dispatchMainIfNot.swift index 7012e2757..bd8166946 100644 --- a/Sources/CodeEditSourceEditor/Extensions/DispatchQueue+dispatchMainIfNot.swift +++ b/Sources/CodeEditSourceEditor/Extensions/DispatchQueue+dispatchMainIfNot.swift @@ -7,8 +7,7 @@ import Foundation -/// Helper methods for dispatching (sync or async) on the main queue only if the calling thread is not already the -/// main queue. +/// Helper methods for dispatching on the main queue only if the calling thread is not already the main queue. extension DispatchQueue { /// Executes the work item on the main thread, dispatching asynchronously if the thread is not the main thread. @@ -22,16 +21,4 @@ extension DispatchQueue { } } } - - /// Executes the work item on the main thread, keeping control on the calling thread until the work item is - /// executed if not already on the main thread. - /// - Parameter item: The work item to execute. - /// - Returns: The value of the work item. - static func waitMainIfNot(_ item: () -> T) -> T { - if Thread.isMainThread { - return item() - } else { - return DispatchQueue.main.sync(execute: item) - } - } } diff --git a/Sources/CodeEditSourceEditor/Extensions/NSTextStorage+Snapshot.swift b/Sources/CodeEditSourceEditor/Extensions/NSTextStorage+Snapshot.swift new file mode 100644 index 000000000..7aab0f880 --- /dev/null +++ b/Sources/CodeEditSourceEditor/Extensions/NSTextStorage+Snapshot.swift @@ -0,0 +1,17 @@ +// +// NSTextStorage+Snapshot.swift +// CodeEditSourceEditor +// + +import AppKit + +extension NSTextStorage { + /// An immutable copy of the text, safe to read from any thread while the storage keeps changing. + /// + /// `string as NSString` is the live backing string, which edits mutate in place. Its copy is O(1): the backing + /// string is copy-on-write. + var stringSnapshot: NSString { + let live = string as NSString + return live.copy() as? NSString ?? NSString(string: live) + } +} diff --git a/Sources/CodeEditSourceEditor/Extensions/TextView+/TextView+createReadBlock.swift b/Sources/CodeEditSourceEditor/Extensions/TextView+/TextView+createReadBlock.swift deleted file mode 100644 index 2e00dff36..000000000 --- a/Sources/CodeEditSourceEditor/Extensions/TextView+/TextView+createReadBlock.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// TextView+createReadBlock.swift -// CodeEditSourceEditor -// -// Created by Khan Winter on 5/20/23. -// - -import Foundation -import CodeEditTextView -import SwiftTreeSitter - -extension TextView { - /// Creates a block for safely reading data into a parser's read block. - /// - /// If the thread is the main queue, executes synchronously. - /// Otherwise it will block the calling thread and execute the block on the main queue, returning control to the - /// calling queue when the block is finished running. - /// - /// - Returns: A new block for reading contents for tree-sitter. - func createReadBlock() -> Parser.ReadBlock { - return { [weak self] byteOffset, _ in - let workItem: () -> Data? = { - let limit = self?.documentRange.length ?? 0 - let location = byteOffset / 2 - let end = min(location + (TreeSitterClient.Constants.charsToReadInBlock), limit) - if location > end || self == nil { - // Ignore and return nothing, tree-sitter's internal tree can be incorrect in some situations. - return nil - } - let range = NSRange(location.. SwiftTreeSitter.Predicate.TextProvider { - return { [weak self] range, _ in - let workItem: () -> String? = { - self?.textStorage.substring(from: range) - } - return DispatchQueue.waitMainIfNot(workItem) - } - } -} diff --git a/Sources/CodeEditSourceEditor/Gutter/GitChangeIndicatorView.swift b/Sources/CodeEditSourceEditor/Gutter/GitChangeIndicatorView.swift new file mode 100644 index 000000000..89295da33 --- /dev/null +++ b/Sources/CodeEditSourceEditor/Gutter/GitChangeIndicatorView.swift @@ -0,0 +1,588 @@ +// +// GitChangeIndicatorView.swift +// CodeEditSourceEditor +// +// Created by Abe Malla on 4/10/26. +// + +import AppKit +import CodeEditTextView + +// swiftlint:disable type_body_length + +/// Displays git change indicators in the gutter, positioned to the left of line numbers. +/// +/// This view draws colored bars/dots aligned with text lines to indicate added, modified, and deleted regions. +/// On hover, the indicator thickens and horizontal rules appear at the top and bottom of the hovered change block. +public final class GitChangeIndicatorView: NSView { + // MARK: - Constants + + /// The width of the indicator bar in its normal (non-hovered) state. + static let barWidth: CGFloat = 4.0 + + /// The width of the indicator bar when hovered. + static let barWidthHovered: CGFloat = 5.5 + + /// Width of the git change indicator column in the gutter. + static let totalWidth: CGFloat = 14.0 + + /// The diameter of the deleted-line dot indicator. + static let dotDiameter: CGFloat = 4.5 + + /// The diameter of the deleted-line dot when hovered. + static let dotDiameterHovered: CGFloat = 6.5 + + /// Duration of the hover animation in seconds. + private static let animationDuration: TimeInterval = 0.12 + + /// Frames per second for the hover animation timer. + private static let animationFPS: TimeInterval = 1.0 / 60.0 + + // MARK: - Properties + + private weak var textView: TextView? + + /// The current set of gutter changes to render. Setting this triggers a redraw + /// and revalidates any active hover overlay. + var changes: [GutterChange] = [] { + didSet { + needsDisplay = true + revalidateHover() + } + } + + // MARK: - Theme Colors + + @Invalidating(.display) + var addedColor: NSColor = NSColor( + light: NSColor(srgbRed: 0.267, green: 0.690, blue: 0.345, alpha: 1.0), + dark: NSColor(srgbRed: 0.267, green: 0.690, blue: 0.345, alpha: 1.0) + ) + + @Invalidating(.display) + var modifiedColor: NSColor = NSColor( + light: NSColor(srgbRed: 0.196, green: 0.533, blue: 0.886, alpha: 1.0), + dark: NSColor(srgbRed: 0.310, green: 0.565, blue: 0.886, alpha: 1.0) + ) + + @Invalidating(.display) + var deletedColor: NSColor = NSColor( + light: NSColor(srgbRed: 0.196, green: 0.533, blue: 0.886, alpha: 1.0), + dark: NSColor(srgbRed: 0.310, green: 0.565, blue: 0.886, alpha: 1.0) + ) + + // MARK: - Hover State + + /// The change currently being rendered (remains set during the reverse/exit animation). + private var hoverChange: GutterChange? + + /// Animation progress: 0.0 = fully un-hovered, 1.0 = fully hovered. + private var hoverProgress: CGFloat = 0.0 + + /// The repeating timer driving the forward or reverse animation. + private var hoverTimer: Timer? + + /// The overlay layer used to draw horizontal rules spanning the full editor width. + private var hoverOverlayLayer: CALayer? + + /// The overlay layer that draws horizontal rules across the gutter area (above the gutter background). + /// This layer is added to self.layer; because `clipsToBounds = false` it can extend beyond the indicator + /// view's bounds into the line-number/folding-ribbon area, clipped only at the GutterView's bounds. + private var hoverGutterOverlayLayer: CALayer? + + // MARK: - NSView Overrides + + override public var isFlipped: Bool { true } + + // MARK: - Initialization + + init(textView: TextView?) { + self.textView = textView + super.init(frame: .zero) + wantsLayer = true + layerContentsRedrawPolicy = .onSetNeedsDisplay + clipsToBounds = false + setupTrackingArea() + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + deinit { + hoverTimer?.invalidate() + hoverOverlayLayer?.removeFromSuperlayer() + hoverGutterOverlayLayer?.removeFromSuperlayer() + } + + // MARK: - Tracking Area + + private var currentTrackingArea: NSTrackingArea? + + private func setupTrackingArea() { + if let existing = currentTrackingArea { + removeTrackingArea(existing) + } + let area = NSTrackingArea( + rect: bounds, + options: [.mouseMoved, .mouseEnteredAndExited, .activeInKeyWindow, .inVisibleRect], + owner: self, + userInfo: nil + ) + addTrackingArea(area) + currentTrackingArea = area + } + + override public func updateTrackingAreas() { + super.updateTrackingAreas() + setupTrackingArea() + } + + override public func resetCursorRects() { + addCursorRect(bounds, cursor: .arrow) + } + + // MARK: - Drawing + + override public func draw(_ dirtyRect: NSRect) { + guard let context = NSGraphicsContext.current?.cgContext, + let textView else { return } + + context.saveGState() + + // Widen the dirty rect to account for view zone whitespace that pushes indicators downward + // during fold animations. Without this, bars offset by a view zone may be outside the original + // dirty rect and get clipped away. + let totalZoneHeight = textView.layoutManager?.viewZones.totalHeight ?? 0 + let widened = dirtyRect.insetBy(dx: 0, dy: -totalZoneHeight) + context.clip(to: widened) + + // Center the bar horizontally within the indicator column. + let centerX = frame.width / 2.0 + + for change in changes { + let barInfo = barRect(for: change, centerX: centerX, textView: textView) + guard let barInfo, barInfo.rect.intersects(widened) else { continue } + + let isHovered = hoverChange == change + let progress = isHovered ? hoverProgress : 0.0 + + let color: NSColor + switch change.type { + case .added: + color = addedColor + case .modified: + color = modifiedColor + case .deleted: + color = deletedColor + } + + context.setFillColor(color.withAlphaComponent(0.5).cgColor) + + switch change.type { + case .added, .modified: + drawBar( + context: context, + rect: barInfo.rect, + progress: progress, + centerX: centerX, + outlineColor: color.cgColor, + ) + case .deleted: + // Position the dot at the top of the line (the boundary where content was deleted) + drawDot( + context: context, + centerY: barInfo.rect.minY, + centerX: centerX, + progress: progress, + outlineColor: color.cgColor + ) + } + } + + context.restoreGState() + } + + /// Information about a change's visual rect in the view. + private struct BarInfo { + let rect: NSRect + } + + /// Computes the visual rect for a change indicator. + private func barRect( + for change: GutterChange, + centerX: CGFloat, + textView: TextView + ) -> BarInfo? { + let lineRange = change.lineRange + guard !lineRange.isEmpty else { return nil } + + let firstLineIndex = lineRange.lowerBound + let lastLineIndex = lineRange.upperBound - 1 + + guard let firstLine = textView.layoutManager.textLineForIndex(firstLineIndex), + let lastLine = textView.layoutManager.textLineForIndex(lastLineIndex) else { + return nil + } + + // Add view zone whitespace so git indicators slide in lockstep with line numbers + // and text content when a zone shrinks/grows during a fold animation. + let viewZones = textView.layoutManager.viewZones + let hasViewZones = !viewZones.zones.isEmpty + let firstWhitespace = hasViewZones + ? viewZones.whitespaceHeightBeforeLine(firstLineIndex) : 0 + let lastWhitespace = hasViewZones + ? viewZones.whitespaceHeightBeforeLine(lastLineIndex) : 0 + + let topY = firstLine.yPos + firstWhitespace + let bottomY = lastLine.yPos + lastWhitespace + lastLine.height + + let barW: CGFloat + switch change.type { + case .added, .modified: + barW = Self.barWidth + case .deleted: + barW = Self.dotDiameter + } + + let rect = NSRect( + x: centerX - barW / 2.0, + y: topY, + width: barW, + height: max(bottomY - topY, 1.0) + ) + + return BarInfo(rect: rect) + } + + /// Draws a vertical bar indicator (for added/modified changes) with a subtle outline. + private func drawBar( + context: CGContext, + rect: NSRect, + progress: CGFloat, + centerX: CGFloat, + outlineColor: CGColor + ) { + let normalWidth = Self.barWidth + let hoveredWidth = Self.barWidthHovered + let currentWidth = normalWidth + (hoveredWidth - normalWidth) * progress + + let adjustedRect = NSRect( + x: centerX - currentWidth / 2.0, + y: rect.minY, + width: currentWidth, + height: rect.height + ) + + let cornerRadius = currentWidth / 2.0 + let path = CGPath(roundedRect: adjustedRect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil) + + // Fill + context.addPath(path) + context.fillPath() + + // Subtle lighter outline + context.setStrokeColor(outlineColor) + context.setLineWidth(1.0) + context.addPath(path) + context.strokePath() + } + + /// Draws a dot indicator (for deleted lines) with a subtle outline. + private func drawDot( + context: CGContext, + centerY: CGFloat, + centerX: CGFloat, + progress: CGFloat, + outlineColor: CGColor + ) { + let normalDiameter = Self.dotDiameter + let hoveredDiameter = Self.dotDiameterHovered + let currentDiameter = normalDiameter + (hoveredDiameter - normalDiameter) * progress + + let dotRect = NSRect( + x: centerX - currentDiameter / 2.0, + y: centerY - currentDiameter / 2.0, + width: currentDiameter, + height: currentDiameter + ) + + // Fill + context.fillEllipse(in: dotRect) + + // Subtle lighter outline + context.setStrokeColor(outlineColor) + context.setLineWidth(0.75) + context.strokeEllipse(in: dotRect) + } + + // MARK: - Hover Overlay + + /// Updates the hover overlay to show/hide horizontal rules and tinted background around the hovered change. + /// + /// Two overlay layers are required because the GutterView clips its own sublayer tree + /// (`layer.masksToBounds = true`), so a single layer cannot span both the gutter and text areas: + /// - `hoverGutterOverlayLayer`: sublayer of `self.layer` — draws above the gutter background, + /// from the bar's left edge to where that background ends. Past it the text overlay shows through. + /// - `hoverOverlayLayer`: sublayer of the document view's layer — covers the full text content area. + /// The portion behind the opaque gutter is hidden so only the text-area portion is visible. + /// Both layers share the same drawing logic via ``configureOverlayLayer``. + private func updateHoverOverlay() { + guard let change = hoverChange, hoverProgress > 0 else { + hoverGutterOverlayLayer?.removeFromSuperlayer() + hoverGutterOverlayLayer = nil + hoverOverlayLayer?.removeFromSuperlayer() + hoverOverlayLayer = nil + return + } + + guard let textView, + let scrollView = textView.enclosingScrollView, + let gutterView = superview else { return } + + let centerX = frame.width / 2.0 + guard let barInfo = barRect(for: change, centerX: centerX, textView: textView) else { return } + + let ruleThickness: CGFloat = 1.0 + let topY = barInfo.rect.minY - (ruleThickness / 2.0) + let bottomY = barInfo.rect.maxY + (ruleThickness / 2.0) + let color: NSColor + switch change.type { + case .added: color = addedColor + case .modified, .deleted: color = modifiedColor + } + + // Gutter overlay: from the bar's left edge to where the gutter's background ends. + let barLeftEdge = centerX - Self.barWidth / 2.0 + 1.0 + let backgroundMaxX = (gutterView as? GutterView)?.backgroundMaxX ?? gutterView.frame.width + let gutterOverlayWidth = backgroundMaxX - barLeftEdge + if hoverGutterOverlayLayer == nil { + let layer = makeOverlayLayer() + self.layer?.addSublayer(layer) + hoverGutterOverlayLayer = layer + } + if let gutterOverlay = hoverGutterOverlayLayer { + configureOverlayLayer( + gutterOverlay, change: change, + topY: topY, bottomY: bottomY, + originX: barLeftEdge, width: gutterOverlayWidth, + ruleThickness: ruleThickness, color: color + ) + } + + // Text overlay: full width of the document view. + let documentView = scrollView.documentView ?? textView + let documentWidth = documentView.frame.width + if hoverOverlayLayer == nil { + let layer = makeOverlayLayer() + documentView.layer?.addSublayer(layer) + hoverOverlayLayer = layer + } + if let textOverlay = hoverOverlayLayer { + configureOverlayLayer( + textOverlay, change: change, + topY: topY, bottomY: bottomY, + originX: 0, width: documentWidth, + ruleThickness: ruleThickness, color: color + ) + } + } + + /// Returns a new CALayer for use as a hover overlay container. + private func makeOverlayLayer() -> CALayer { + let layer = CALayer() + layer.zPosition = 100 + layer.actions = ["position": NSNull(), "bounds": NSNull(), "sublayers": NSNull(), "backgroundColor": NSNull()] + return layer + } + + /// Populates an overlay container layer with a tinted background fill and border rules. + /// + /// For `.deleted` changes, draws a single hairline rule at the deletion boundary. + /// For `.added`/`.modified` changes, draws a semi-transparent tinted fill bounded by + /// two hairline rules. + private func configureOverlayLayer( + _ container: CALayer, + change: GutterChange, + topY: CGFloat, + bottomY: CGFloat, + originX: CGFloat, + width: CGFloat, + ruleThickness: CGFloat, + color: NSColor + ) { + container.sublayers?.forEach { $0.removeFromSuperlayer() } + + if change.type == .deleted { + // Single hairline at the deletion boundary + container.frame = CGRect( + x: originX, y: topY - ruleThickness / 2.0, + width: width, height: ruleThickness + ) + let rule = CALayer() + rule.backgroundColor = color.cgColor + rule.frame = CGRect(x: 0, y: 0, width: width, height: ruleThickness) + container.addSublayer(rule) + } else { + let height = bottomY - topY + container.frame = CGRect(x: originX, y: topY, width: width, height: height) + + // Tinted background fill between the rules + let background = CALayer() + background.backgroundColor = color.withAlphaComponent(0.02).cgColor + background.frame = CGRect(x: 0, y: 0, width: width, height: height) + container.addSublayer(background) + + // Top border rule + let topRule = CALayer() + topRule.backgroundColor = color.cgColor + topRule.frame = CGRect(x: 0, y: 0, width: width, height: ruleThickness) + container.addSublayer(topRule) + + // Bottom border rule + let bottomRule = CALayer() + bottomRule.backgroundColor = color.cgColor + bottomRule.frame = CGRect(x: 0, y: height - ruleThickness, width: width, height: ruleThickness) + container.addSublayer(bottomRule) + } + } + + // MARK: - Mouse Events + + override public func mouseMoved(with event: NSEvent) { + let point = convert(event.locationInWindow, from: nil) + updateHoverForPoint(point) + } + + override public func mouseExited(with event: NSEvent) { + clearHover() + } + + override public func scrollWheel(with event: NSEvent) { + super.scrollWheel(with: event) + // Re-evaluate hover after scroll + let point = convert(event.locationInWindow, from: nil) + updateHoverForPoint(point) + } + + /// Determines which change (if any) the mouse is hovering over and updates the hover state. + private func updateHoverForPoint(_ point: NSPoint) { + guard let textView else { + clearHover() + return + } + + let centerX = frame.width / 2.0 + + // Find which change the y position falls within + for change in changes { + guard let barInfo = barRect(for: change, centerX: centerX, textView: textView) else { continue } + + // Expand hit-test rect to make it easier to hover (full width of the view, generous vertical padding) + let hitRect = NSRect( + x: 0, + y: barInfo.rect.minY - 2, + width: frame.width, + height: barInfo.rect.height + 4 + ) + + if hitRect.contains(point) { + if hoverChange != change { + setHoveredChange(change) + } + return + } + } + + clearHover() + } + + /// Sets the currently hovered change and starts the forward (enter) animation. + private func setHoveredChange(_ change: GutterChange) { + hoverTimer?.invalidate() + + // Switching to a different change: snap progress to zero and start fresh. + if hoverChange != change { + hoverProgress = 0.0 + hoverChange = change + needsDisplay = true + } + + guard hoverProgress < 1.0 else { return } + + let startProgress = hoverProgress + let startTime = CACurrentMediaTime() + let totalDuration = Self.animationDuration + + hoverTimer = Timer.scheduledTimer(withTimeInterval: Self.animationFPS, repeats: true) { [weak self] timer in + guard let self else { timer.invalidate(); return } + let elapsed = CACurrentMediaTime() - startTime + let newProgress = min(startProgress + CGFloat(elapsed / totalDuration), 1.0) + self.hoverProgress = newProgress + self.needsDisplay = true + self.updateHoverOverlay() + if newProgress >= 1.0 { timer.invalidate() } + } + } + + /// Starts the reverse (exit) animation, decrementing progress back to zero. + /// The hovered change reference is kept until progress reaches zero so the + /// bar animates back before disappearing. + private func clearHover() { + hoverTimer?.invalidate() + + guard hoverProgress > 0 else { + hoverChange = nil + needsDisplay = true + updateHoverOverlay() + return + } + + let startProgress = hoverProgress + let startTime = CACurrentMediaTime() + // Reverse duration proportional to how much of the animation is left, + // so the bar always exits at the same visual speed it entered. + let reverseDuration = Self.animationDuration * Double(startProgress) + + hoverTimer = Timer.scheduledTimer(withTimeInterval: Self.animationFPS, repeats: true) { [weak self] timer in + guard let self else { timer.invalidate(); return } + let elapsed = CACurrentMediaTime() - startTime + let newProgress = max(startProgress - CGFloat(elapsed / reverseDuration), 0.0) + self.hoverProgress = newProgress + self.needsDisplay = true + self.updateHoverOverlay() + if newProgress <= 0 { + timer.invalidate() + self.hoverChange = nil + } + } + } + + /// Re-evaluates the hover state after the `changes` array is updated. + /// Rather than trying to match the old change by identity, we re-run hit + /// testing at the current mouse location. This handles changes that grew, + /// shrank, split, or merged while the cursor stayed in place. + private func revalidateHover() { + guard hoverChange != nil else { return } + + // Get current mouse position in our coordinate space + guard let window, let mouseScreenPoint = NSApp.currentEvent?.locationInWindow ?? Optional(window.mouseLocationOutsideOfEventStream) else { + return + } + let localPoint = convert(mouseScreenPoint, from: nil) + + // If mouse is outside our bounds, clear hover + guard bounds.contains(localPoint) else { + hoverTimer?.invalidate() + hoverProgress = 0 + hoverChange = nil + needsDisplay = true + updateHoverOverlay() + return + } + + // Re-run hit testing — updateHoverForPoint will set/clear hover as appropriate + updateHoverForPoint(localPoint) + } +} diff --git a/Sources/CodeEditSourceEditor/Gutter/GutterChangeData.swift b/Sources/CodeEditSourceEditor/Gutter/GutterChangeData.swift new file mode 100644 index 000000000..5c7658c75 --- /dev/null +++ b/Sources/CodeEditSourceEditor/Gutter/GutterChangeData.swift @@ -0,0 +1,37 @@ +// +// GutterChangeData.swift +// CodeEditSourceEditor +// +// Created by Abe Malla on 4/10/26. +// + +import AppKit + +/// Represents a single contiguous change region in the gutter. +/// +/// This is the data type that the source editor package uses to render git change indicators. +/// The source editor package is git-agnostic - the host app provides these values. +public struct GutterChange: Equatable, Sendable { + /// The type of change for a gutter indicator. + public enum ChangeType: Sendable { + /// Lines were added. + case added + /// Lines were modified. + case modified + /// Lines were deleted at this position. + case deleted + } + + /// The type of change. + public let type: ChangeType + + /// The range of 0-indexed line numbers in the modified (current) document that this change covers. + /// + /// For `deleted` changes, this is a single-element range indicating the line *after* which the deletion occurred. + public let lineRange: Range + + public init(type: ChangeType, lineRange: Range) { + self.type = type + self.lineRange = lineRange + } +} diff --git a/Sources/CodeEditSourceEditor/Gutter/GutterView.swift b/Sources/CodeEditSourceEditor/Gutter/GutterView.swift index 8be57ec62..8bc4e14c6 100644 --- a/Sources/CodeEditSourceEditor/Gutter/GutterView.swift +++ b/Sources/CodeEditSourceEditor/Gutter/GutterView.swift @@ -52,7 +52,7 @@ public class GutterView: NSView { } @Invalidating(.display) - var edgeInsets: EdgeInsets = EdgeInsets(leading: 20, trailing: 12) + var edgeInsets: EdgeInsets = EdgeInsets(leading: 0, trailing: 12) @Invalidating(.display) var backgroundEdgeInsets: EdgeInsets = EdgeInsets(leading: 0, trailing: 8) @@ -81,6 +81,14 @@ public class GutterView: NSView { } } + /// Toggle the visibility of the git change indicators in the gutter. + @Invalidating(.display) + public var showGitChangeIndicators: Bool = true { + didSet { + gitChangeIndicator.isHidden = !showGitChangeIndicators + } + } + private weak var textView: TextView? private weak var delegate: GutterViewDelegate? private var maxLineNumberWidth: CGFloat = 0 @@ -103,6 +111,18 @@ public class GutterView: NSView { /// The view that draws the fold decoration in the gutter. var foldingRibbon: LineFoldRibbonView + /// The view that draws git change indicators (blue/green bars) in the gutter. + var gitChangeIndicator: GitChangeIndicatorView + + /// Syntax helper for determining the required space for the git change indicator. + private var gitChangeIndicatorWidth: CGFloat { + if gitChangeIndicator.isHidden { + 0.0 + } else { + GitChangeIndicatorView.totalWidth + } + } + /// Syntax helper for determining the required space for the folding ribbon. private var foldingRibbonWidth: CGFloat { if foldingRibbon.isHidden { @@ -112,20 +132,35 @@ public class GutterView: NSView { } } + /// Where the gutter's background ends. + var backgroundMaxX: CGFloat { + (frame.width - backgroundEdgeInsets.trailing - foldingRibbonWidth).rounded() + } + /// The gutter's y positions start at the top of the document and increase as it moves down the screen. override public var isFlipped: Bool { true } - /// We override this variable so we can update the ``foldingRibbon``'s frame to match the gutter. + /// We override this variable so we can update subview frames to match the gutter. override public var frame: NSRect { get { super.frame } set { super.frame = newValue + + // Git change indicator sits in its own narrow column at the leading edge. + gitChangeIndicator.frame = NSRect( + x: 0, + y: 0.0, + width: gitChangeIndicator.isHidden ? 0 : GitChangeIndicatorView.totalWidth, + height: newValue.height + ) + + // Folding ribbon is positioned at the trailing edge, after the glyph margin foldingRibbon.frame = NSRect( - x: newValue.width - edgeInsets.trailing - foldingRibbonWidth + foldingRibbonPadding, + x: newValue.width - foldingRibbonWidth, y: 0.0, width: foldingRibbonWidth, height: newValue.height @@ -161,6 +196,7 @@ public class GutterView: NSView { self.delegate = delegate foldingRibbon = LineFoldRibbonView(controller: controller) + gitChangeIndicator = GitChangeIndicatorView(textView: controller.textView) super.init(frame: .zero) clipsToBounds = true @@ -169,6 +205,7 @@ public class GutterView: NSView { translatesAutoresizingMaskIntoConstraints = false layer?.masksToBounds = true + addSubview(gitChangeIndicator) addSubview(foldingRibbon) NotificationCenter.default.addObserver( @@ -204,7 +241,8 @@ public class GutterView: NSView { maxLineLength = lineStorageDigits } - let newWidth = maxLineNumberWidth + edgeInsets.horizontal + foldingRibbonWidth + let newWidth = gitChangeIndicatorWidth + maxLineNumberWidth + edgeInsets.horizontal + + foldingRibbonWidth if frame.size.width != newWidth { frame.size.width = newWidth delegate?.gutterViewWidthDidUpdate() @@ -218,7 +256,7 @@ public class GutterView: NSView { private func drawBackground(_ context: CGContext, dirtyRect: NSRect) { guard let backgroundColor else { return } let minX = max(backgroundEdgeInsets.leading, dirtyRect.minX) - let maxX = min(frame.width - backgroundEdgeInsets.trailing - foldingRibbonWidth, dirtyRect.maxX) + let maxX = min(backgroundMaxX, dirtyRect.maxX) let width = maxX - minX context.saveGState() @@ -240,10 +278,15 @@ public class GutterView: NSView { context.saveGState() var highlightedLines: Set = [] - context.setFillColor(selectedLineColor.cgColor) - let xPos = backgroundEdgeInsets.leading - let width = frame.width - backgroundEdgeInsets.trailing + // Line highlight starts after the git change indicator column, with a rounded leading edge, and ends where + // the background does. Past it the text view's own highlight shows through; drawing over that too would stack + // the two translucent fills and darken the band around the folding ribbon. + let highlightLeading = gitChangeIndicatorWidth + backgroundEdgeInsets.leading + let highlightWidth = backgroundMaxX - highlightLeading + + let viewZones = textView.layoutManager.viewZones + let hasViewZones = !viewZones.zones.isEmpty for selection in selectionManager.textSelections where selection.range.isEmpty { guard let line = textView.layoutManager.textLineForOffset(selection.range.location), @@ -252,13 +295,22 @@ public class GutterView: NSView { continue } highlightedLines.insert(line.data.id) - context.fill( - CGRect( - x: xPos, - y: line.yPos, - width: width, - height: line.height - ).pixelAligned + // Line storage Y doesn't include view zone whitespace; the text view layout adds it in to get the + // visible Y. The gutter renders alongside the text, so it must add the same offset — otherwise gutter + // highlights snap to the zero-zone position while the text view is mid-animation. + let whitespaceOffset = hasViewZones ? viewZones.whitespaceHeightBeforeLine(line.index) : 0 + let rect = CGRect( + x: highlightLeading, + y: line.yPos + whitespaceOffset, + width: highlightWidth, + height: line.height + ).pixelAligned + + // Round only the leading (left) corners — the text view draws the right side + LineHighlightDrawing.fillLeadingRoundedRect( + rect, + color: selectedLineColor.cgColor, + in: context ) } @@ -282,12 +334,32 @@ public class GutterView: NSView { } } + // IndexSet.intersects uses half-open ranges, so a cursor positioned exactly at + // textView.length (end of file) never intersects any line — both the zero-length + // trailing line (file ends with \n) and the last non-empty line (no trailing \n) + // fail the check. Resolve the correct line once up front so we can match by ID + // in the loop without accidentally highlighting an adjacent line that shares the + // same boundary offset. + let eofLineID: UUID? = selectionRangeMap.contains(textView.length) + ? textView.layoutManager.textLineForOffset(textView.length)?.data.id + : nil + context.saveGState() context.clip(to: dirtyRect) + let viewZones = textView.layoutManager.viewZones + let hasViewZones = !viewZones.zones.isEmpty + // Line storage Y doesn't include zone whitespace, so we have to widen the query to cover any line whose + // `documentY = storedY + whitespaceBefore(index)` could land inside dirtyRect. Subtracting the total + // whitespace is a conservative lower bound; the per-line visibility check happens via the dirtyRect clip. + let queryMinY = hasViewZones ? max(0, dirtyRect.minY - viewZones.totalHeight) : dirtyRect.minY + let queryMaxY = dirtyRect.maxY + context.textMatrix = CGAffineTransform(scaleX: 1, y: -1) - for linePosition in textView.layoutManager.linesStartingAt(dirtyRect.minY, until: dirtyRect.maxY) { - if selectionRangeMap.intersects(integersIn: linePosition.range) { + for linePosition in textView.layoutManager.linesStartingAt(queryMinY, until: queryMaxY) { + let isSelected = selectionRangeMap.intersects(integersIn: linePosition.range) + || linePosition.data.id == eofLineID + if isSelected { attributes[.foregroundColor] = selectedLineTextColor ?? textColor } else { attributes[.foregroundColor] = textColor @@ -301,9 +373,14 @@ public class GutterView: NSView { let lineNumberWidth = CTLineGetTypographicBounds(ctLine, &ascent, nil, nil) let fontHeightDifference = ((fragment?.height ?? 0) - fontLineHeight) / 4 - let yPos = linePosition.yPos + ascent + (fragment?.heightDifference ?? 0)/2 + fontHeightDifference - // Leading padding + (width - linewidth) - let xPos = edgeInsets.leading + (maxLineNumberWidth - lineNumberWidth) + // Add view zone whitespace so line numbers animate with the text when a zone shrinks/grows mid-fold. + let whitespaceOffset = hasViewZones + ? viewZones.whitespaceHeightBeforeLine(linePosition.index) + : 0 + let fragmentHeightDifference = (fragment?.heightDifference ?? 0)/2 + let yPos = linePosition.yPos + whitespaceOffset + ascent + fragmentHeightDifference + fontHeightDifference + // Leading padding + git indicator width + (width - linewidth) + let xPos = gitChangeIndicatorWidth + edgeInsets.leading + (maxLineNumberWidth - lineNumberWidth) ContextSetHiddenSmoothingStyle(context, 16) diff --git a/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView+Draw.swift b/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView+Draw.swift index 581379369..c93f9af78 100644 --- a/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView+Draw.swift +++ b/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView+Draw.swift @@ -19,9 +19,17 @@ extension LineFoldRibbonView { override func draw(_ dirtyRect: NSRect) { guard let context = NSGraphicsContext.current?.cgContext, - let layoutManager = model?.controller?.textView.layoutManager, - // Find the visible lines in the rect AppKit is asking us to draw. - let rangeStart = layoutManager.textLineForPosition(dirtyRect.minY), + let layoutManager = model?.controller?.textView.layoutManager else { + return + } + + // Line storage Y doesn't include view zone whitespace. Widen the query range so we + // catch lines whose visible Y (storageY + whitespace) lands inside dirtyRect. + let viewZones = layoutManager.viewZones + let hasViewZones = !viewZones.zones.isEmpty + let queryMinY = hasViewZones ? max(0, dirtyRect.minY - viewZones.totalHeight) : dirtyRect.minY + + guard let rangeStart = layoutManager.textLineForPosition(queryMinY), let rangeEnd = layoutManager.textLineForPosition(dirtyRect.maxY) else { return } @@ -122,8 +130,17 @@ extension LineFoldRibbonView { in context: CGContext, using layoutManager: TextLayoutManager ) { - let minYPosition = foldInfo.startLine.yPos - let maxYPosition = foldInfo.endLine.yPos + foldInfo.endLine.height + // Add view zone whitespace so fold ribbons slide in lockstep with line numbers + // and text content when a zone shrinks/grows during a fold animation. + let viewZones = layoutManager.viewZones + let hasViewZones = !viewZones.zones.isEmpty + let startWhitespace = hasViewZones + ? viewZones.whitespaceHeightBeforeLine(foldInfo.startLine.index) : 0 + let endWhitespace = hasViewZones + ? viewZones.whitespaceHeightBeforeLine(foldInfo.endLine.index) : 0 + + let minYPosition = foldInfo.startLine.yPos + startWhitespace + let maxYPosition = foldInfo.endLine.yPos + endWhitespace + foldInfo.endLine.height let foldRect = NSRect(x: 0, y: minYPosition + 1, width: 7, height: maxYPosition - minYPosition - 2) if foldInfo.fold.isCollapsed { diff --git a/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView.swift b/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView.swift index e2b9d54f0..5833cc184 100644 --- a/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView.swift +++ b/Sources/CodeEditSourceEditor/LineFolding/View/LineFoldRibbonView.swift @@ -9,6 +9,8 @@ import Foundation import AppKit import CodeEditTextView +// swiftlint:disable type_body_length file_length + /// Displays the code folding ribbon in the ``GutterView``. /// /// This view draws its contents manually. This was chosen over managing views on a per-fold basis, which would come @@ -93,6 +95,36 @@ class LineFoldRibbonView: NSView { fatalError("init(coder:) has not been implemented") } + override func viewDidChangeEffectiveAppearance() { + super.viewDidChangeEffectiveAppearance() + // CGColor values resolved from dynamic NSColors are static snapshots. Re-resolve them + // so the ribbon picks up the correct light/dark variant after an appearance switch. + markerColor = NSColor( + light: NSColor(deviceWhite: 0.0, alpha: 0.1), + dark: NSColor(deviceWhite: 1.0, alpha: 0.2) + ).cgColor + markerBorderColor = NSColor( + light: NSColor(deviceWhite: 1.0, alpha: 0.4), + dark: NSColor(deviceWhite: 0.0, alpha: 0.4) + ).cgColor + hoverFillColor = NSColor( + light: NSColor(deviceWhite: 1.0, alpha: 1.0), + dark: NSColor(deviceWhite: 0.17, alpha: 1.0) + ).cgColor + hoverBorderColor = NSColor( + light: NSColor(deviceWhite: 0.8, alpha: 1.0), + dark: NSColor(deviceWhite: 0.4, alpha: 1.0) + ).cgColor + foldedIndicatorColor = NSColor( + light: NSColor(deviceWhite: 0.0, alpha: 0.3), + dark: NSColor(deviceWhite: 1.0, alpha: 0.6) + ).cgColor + foldedIndicatorChevronColor = NSColor( + light: NSColor(deviceWhite: 1.0, alpha: 1.0), + dark: NSColor(deviceWhite: 0.0, alpha: 1.0) + ).cgColor + } + override public func resetCursorRects() { // Don't use an iBeam in this view addCursorRect(bounds, cursor: .arrow) @@ -120,26 +152,642 @@ class LineFoldRibbonView: NSView { override func mouseDown(with event: NSEvent) { let clickPoint = convert(event.locationInWindow, from: nil) - guard let layoutManager = model?.controller?.textView.layoutManager, + guard let controller = model?.controller, + let textView = controller.textView, + let layoutManager = textView.layoutManager, event.type == .leftMouseDown, let lineNumber = layoutManager.textLineForPosition(clickPoint.y)?.index, - let fold = model?.getCachedFoldAt(lineNumber: lineNumber), - let firstLineInFold = layoutManager.textLineForOffset(fold.range.lowerBound) else { + let fold = model?.getCachedFoldAt(lineNumber: lineNumber) else { + super.mouseDown(with: event) + return + } + + // If a collapse/expand is currently animating for this fold, jump it to its final state + // before deciding which direction this click goes. This is what prevents a rapid second + // click from stacking another overlay on top of the first — it forces the toggle to be + // based on settled (post-finalization) state, not the transient mid-animation state. + finalizeActiveAnimation(for: fold) + + // Re-fetch line position — finalizing an animation mutates line heights, view zones, and + // attachments, so yPos/range may have shifted. + guard let firstLineInFold = layoutManager.textLineForOffset(fold.range.lowerBound) else { super.mouseDown(with: event) return } - if let attachment = findAttachmentFor(fold: fold, firstLineRange: firstLineInFold.range) { - layoutManager.attachments.remove(atOffset: attachment.range.location) + let isCurrentlyCollapsed = findAttachmentFor(fold: fold, firstLineRange: firstLineInFold.range) != nil + + if isCurrentlyCollapsed { + expandFold(fold: fold, firstLineInFold: firstLineInFold, event: event) } else { - let charWidth = model?.controller?.font.charWidth ?? 1.0 - let placeholder = LineFoldPlaceholder(delegate: model, fold: fold, charWidth: charWidth) - layoutManager.attachments.add(placeholder, for: NSRange(fold.range)) + collapseFold(fold: fold, firstLineInFold: firstLineInFold, event: event) + } + } + + /// Duration of the paper-fold animation, in seconds. Every piece — paper fold, view-zone height, below-fold + /// slide, and ribbon refresh — is driven by this single duration so they always finish together. + private static let foldAnimationDuration: CFTimeInterval = 0.5 + + // MARK: - Collapse + + /// Collapses the fold with a paper-fold animation. + /// + /// The fold is committed immediately (so the real text view can authoritatively hold the collapsed state), + /// then a view zone of matching height is inserted so the below-fold content visually stays in place. The + /// zone shrinks over `foldAnimationDuration` seconds in lockstep with the paper-fold overlay's angle. When + /// the zone hits zero the real text view is already correctly laid out — the below-fold content finishes + /// the animation at its final position, so there is no "snap" when the overlay is removed. + /// + /// We do not snapshot or slide the below-fold content. The real text view does the sliding itself. + private func collapseFold( + fold: FoldRange, + firstLineInFold: TextLineStorage.TextLinePosition, + event: NSEvent + ) { + guard let controller = model?.controller, + let textView = controller.textView, + let scrollView = controller.scrollView, + let layoutManager = textView.layoutManager, + let gutterView = controller.gutterView else { return } + + // If a previous collapse/expand for this fold is still animating, finalize it so its + // overlay/zone/attachment state settles before we start a new one. Prevents overlays + // stacking on rapid clicks. + finalizeActiveAnimation(for: fold) + + guard let metrics = computeFoldMetrics( + fold: fold, firstLineInFold: firstLineInFold, layoutManager: layoutManager + ), metrics.foldHeight > 0 else { + performCollapse(fold: fold, event: event) + return + } + + // Snapshot the fold region at its current (expanded) state so the paper fold can render over it. + // The snapshot is clipped to the visible viewport + guard let snapshotResult = captureFoldSnapshot( + docY: metrics.foldRegionMinY, height: metrics.foldHeight, controller: controller + ) else { + performCollapse(fold: fold, event: event) + return + } + + // Commit the collapse now. Real line storage heights go to zero for the fold lines, the placeholder + // attachment is installed, and the ribbon flips to its collapsed indicator. + performCollapse(fold: fold, event: event) + + // A view zone of matching height restores the visual spacing. Without it, the below-fold content + // would jump up to its collapsed position immediately; with it, the below content sits in place and + // rides the zone height down to 0 over the animation. + let zoneID = layoutManager.viewZones.addZone( + ViewZone(afterLineNumber: firstLineInFold.index, heightInPoints: metrics.foldHeight) + ) + + // Force layout so the zone is reflected in line positions before we mount the overlay. + textView.needsLayout = true + textView.layoutSubtreeIfNeeded() + + // The paper-fold layer covers only the visible portion of the fold. The view zone handles the + // full fold height so below-fold content slides correctly even when the fold extends off-screen. + guard let mount = mountOverlay( + scrollView: scrollView, + snapshot: snapshotResult.snapshot, + foldDocY: snapshotResult.capturedDocY, + foldHeight: snapshotResult.capturedHeight, + totalWidth: snapshotResult.totalWidth + ) else { + layoutManager.viewZones.removeZone(id: zoneID) + return + } + + let foldKey = NSRange(fold.range) + let driver = runFoldAnimation( + direction: .collapse, + foldHeight: metrics.foldHeight, + zoneID: zoneID, + mount: mount, + layoutManager: layoutManager, + textView: textView, + gutterView: gutterView, + onComplete: { [weak self, weak gutterView] in + layoutManager.viewZones.removeZone(id: zoneID) + mount.foldLayer.cleanup() + mount.overlay.removeFromSuperview() + gutterView?.needsDisplay = true + self?.activeAnimations.removeValue(forKey: foldKey) + self?.mouseMoved(with: event) + } + ) + activeAnimations[foldKey] = driver + } + + // MARK: - Expand + + /// Expands the fold with a paper-fold animation. + /// + /// The placeholder attachment is kept in place for the duration of the animation — this is what holds the + /// real fold lines at zero height. A view zone grows from 0 to `foldHeight` over the animation, and the + /// below-fold content rides that zone down to its final (expanded) position. When the animation finishes, + /// the attachment is removed (fold line heights restored to natural) and the zone removed simultaneously — + /// the two changes net to zero vertical shift, so there is no visible snap. + /// + /// Before the animation begins we do a brief dance: remove the attachment, force layout to measure the + /// natural heights, capture the snapshot at the expanded positions, then re-install the attachment. This + /// all happens synchronously within the mouseDown handler, so the screen never paints an intermediate state. + private func expandFold( + fold: FoldRange, + firstLineInFold: TextLineStorage.TextLinePosition, + event: NSEvent + ) { + guard let controller = model?.controller, + let textView = controller.textView, + let scrollView = controller.scrollView, + let layoutManager = textView.layoutManager, + let gutterView = controller.gutterView else { + return + } + + // If a previous collapse/expand for this fold is still animating, finalize it so its + // overlay/zone/attachment state settles before we start a new one. Must happen BEFORE we + // look for the attachment, because the in-flight animation may still be holding it. + finalizeActiveAnimation(for: fold) + + guard let attachment = findAttachmentFor(fold: fold, firstLineRange: firstLineInFold.range) else { + return + } + + let attachmentOffset = attachment.range.location + + // Flip the ribbon's indicator to "expanded" immediately so it matches the state the user just clicked to. + model?.foldCache.toggleCollapse(forFold: fold) + gutterView.needsDisplay = true + + // Temporarily uncommit so the layout system fills in natural heights — we need them both for measuring + // foldHeight and for rendering an expanded-state snapshot. + layoutManager.attachments.remove(atOffset: attachmentOffset) + textView.needsLayout = true + textView.layoutSubtreeIfNeeded() + + // Re-fetch the first line — its height hasn't changed, but there's no harm in refreshing the position. + guard let updatedFirstLine = layoutManager.textLineForOffset(fold.range.lowerBound), + let metrics = computeFoldMetrics( + fold: fold, firstLineInFold: updatedFirstLine, layoutManager: layoutManager + ), metrics.foldHeight > 0 else { + // Nothing to animate — just leave the attachment off and let layout finish. + gutterView.needsDisplay = true + mouseMoved(with: event) + return + } + + // The snapshot is clipped to the visible viewport — off-screen lines aren't captured. + guard let snapshotResult = captureFoldSnapshot( + docY: metrics.foldRegionMinY, height: metrics.foldHeight, controller: controller + ) else { + // Snapshot failed — leave the attachment off, which means the fold is now expanded without animation. + gutterView.needsDisplay = true + mouseMoved(with: event) + return + } + + // Re-install the placeholder so fold line heights snap back to zero. This is the state the animation + // starts from: fold region visually collapsed, below-fold content at `foldRegionMinY`. + let charWidth = controller.font.charWidth + let placeholder = LineFoldPlaceholder(delegate: model, fold: fold, charWidth: charWidth) + layoutManager.attachments.add(placeholder, for: NSRange(fold.range)) + + // A view zone grows from 0 to foldHeight over the animation, pushing the below-fold content down. + let zoneID = layoutManager.viewZones.addZone( + ViewZone(afterLineNumber: updatedFirstLine.index, heightInPoints: 0) + ) + + textView.needsLayout = true + textView.layoutSubtreeIfNeeded() + + // The paper-fold layer covers only the visible portion of the fold. The view zone handles the + // full fold height so below-fold content slides correctly even when the fold extends off-screen. + guard let mount = mountOverlay( + scrollView: scrollView, + snapshot: snapshotResult.snapshot, + foldDocY: snapshotResult.capturedDocY, + foldHeight: snapshotResult.capturedHeight, + totalWidth: snapshotResult.totalWidth + ) else { + // Couldn't mount the overlay — finish expansion synchronously by removing the attachment we just added. + layoutManager.viewZones.removeZone(id: zoneID) + layoutManager.attachments.remove(atOffset: fold.range.lowerBound) + textView.needsLayout = true + textView.layoutSubtreeIfNeeded() + mouseMoved(with: event) + return + } + + let foldKey = NSRange(fold.range) + let driver = runFoldAnimation( + direction: .expand, + foldHeight: metrics.foldHeight, + zoneID: zoneID, + mount: mount, + layoutManager: layoutManager, + textView: textView, + gutterView: gutterView, + onComplete: { [weak self, weak gutterView] in + // Remove attachment (fold lines restore to natural) and zone (removes the pushed-down space) + // in the same turn. Net vertical shift: zero — nothing snaps. + layoutManager.attachments.remove(atOffset: fold.range.lowerBound) + layoutManager.viewZones.removeZone(id: zoneID) + textView.needsLayout = true + textView.layoutSubtreeIfNeeded() + mount.foldLayer.cleanup() + mount.overlay.removeFromSuperview() + gutterView?.needsDisplay = true + self?.activeAnimations.removeValue(forKey: foldKey) + self?.mouseMoved(with: event) + } + ) + activeAnimations[foldKey] = driver + } + + // MARK: - Animation Driver + + /// Direction of a fold animation, used to select the heightScale curve. + private enum FoldAnimationDirection { + case collapse // 1 → 0 + case expand // 0 → 1 + } + + /// Self-contained per-frame animation clock. Drives a closure each tick with the current + /// normalized height scale (1 = fully open, 0 = fully closed), so every caller can share the + /// same curve, duration, and lifecycle. + /// + /// Uses a repeating Timer on `.common` mode so scrolling and event tracking don't pause the + /// animation — at this package's deployment target (macOS 13) `CADisplayLink` isn't yet + /// available. + private final class FoldAnimationDriver { + private let duration: CFTimeInterval + private let direction: FoldAnimationDirection + private let onTick: (CGFloat) -> Void + private let onComplete: () -> Void + private let startTime: CFTimeInterval + private var timer: Timer? + private var isFinished = false + + init( + duration: CFTimeInterval, + direction: FoldAnimationDirection, + onTick: @escaping (CGFloat) -> Void, + onComplete: @escaping () -> Void + ) { + self.duration = duration + self.direction = direction + self.onTick = onTick + self.onComplete = onComplete + self.startTime = CACurrentMediaTime() + } + + func start() { + // Fire a zero-time tick so the initial state is applied before the next paint — the screen + // never shows an un-animated frame. + tick() + guard !isFinished else { return } + // Strong self capture: the timer retains the driver until `tick()` invalidates it on completion. + // Without this the driver would deallocate as soon as `runFoldAnimation` returns (nothing else + // holds it), the timer's `[weak self]` would be nil on first fire, and the animation would + // never tick — leaving the view zone at full height and the paper overlay flat. + let timer = Timer(timeInterval: 1.0 / 120.0, repeats: true) { [self] _ in + self.tick() + } + self.timer = timer + RunLoop.main.add(timer, forMode: .common) + } + + private func tick() { + guard !isFinished else { return } + let elapsed = CACurrentMediaTime() - startTime + let progress = min(max(elapsed / duration, 0), 1) + let eased = Self.easeInOutCubic(progress) + let heightScale: CGFloat = (direction == .collapse) ? (1 - eased) : eased + onTick(heightScale) + if progress >= 1 { + isFinished = true + timer?.invalidate() + timer = nil + onComplete() + } + } + + /// Cancels the timer and jumps straight to the final state via `onComplete`. Used when the + /// user re-clicks the same fold mid-animation — we finalize the current animation (so the + /// overlay/zone/attachment are cleaned up) before the caller starts the new one. + func finishImmediately() { + guard !isFinished else { return } + isFinished = true + timer?.invalidate() + timer = nil + onComplete() + } + + private static func easeInOutCubic(_ t: CFTimeInterval) -> CGFloat { + return t < 0.5 + ? CGFloat(4 * t * t * t) + : CGFloat(1 - pow(-2 * t + 2, 3) / 2) + } + } + + /// Spins up a `FoldAnimationDriver` that updates the view zone, paper-fold angle, and text layout + /// each tick. Ensures the projected paper-fold height (`foldHeight · cos(θ)`) tracks the zone + /// height exactly, so the two visual elements always align. + /// + /// Returns the driver so the caller can register it in `activeAnimations` and cancel it if the + /// user clicks the same fold again while it's in flight. + // swiftlint:disable:next function_parameter_count + @discardableResult + private func runFoldAnimation( + direction: FoldAnimationDirection, + foldHeight: CGFloat, + zoneID: UUID, + mount: MountedFold, + layoutManager: TextLayoutManager, + textView: TextView, + gutterView: GutterView, + onComplete: @escaping () -> Void + ) -> FoldAnimationDriver { + // The driver is retained by the RunLoop via its timer until it completes. + let driver = FoldAnimationDriver( + duration: Self.foldAnimationDuration, + direction: direction, + onTick: { [weak textView, weak gutterView] heightScale in + let clampedScale = min(max(heightScale, 0), 1) + // θ = acos(heightScale) keeps the paper's projected height `foldHeight·cos(θ)` exactly + // equal to the view-zone height. The real below-fold content (pushed down by the zone) + // always meets the bottom edge of the visible paper fold. + let theta = acos(clampedScale) + mount.foldLayer.setFoldAngle(theta) + layoutManager.viewZones.updateZoneHeight(id: zoneID, newHeight: foldHeight * clampedScale) + textView?.needsLayout = true + textView?.layoutSubtreeIfNeeded() + // The gutter reads view zone whitespace offsets in its draw routine, so forcing a redraw + // here makes its line numbers slide in lockstep with the text as the zone shrinks/grows. + gutterView?.needsDisplay = true + gutterView?.displayIfNeeded() + }, + onComplete: onComplete + ) + driver.start() + return driver + } + + // MARK: - In-Flight Animation Tracking + + /// Tracks animations currently in flight, keyed by fold range. Used so a rapid second click on + /// the same fold cancels (finalizes) the first animation rather than stacking a second overlay + /// on top of the first. + private var activeAnimations: [NSRange: FoldAnimationDriver] = [:] + + /// If a fold has an animation in flight, run its completion immediately. The completion cleans + /// up the overlay/zone/attachment so the state is settled before the caller starts a new + /// animation. + private func finalizeActiveAnimation(for fold: FoldRange) { + let key = NSRange(fold.range) + guard let driver = activeAnimations[key] else { return } + driver.finishImmediately() + // onComplete (registered in the collapse/expand paths) clears the entry via + // `activeAnimations.removeValue`, so we don't need to clear it here. + } + + // MARK: - Fold Animation Helpers + + /// Cache of measurements needed to drive the animation. Computing these requires walking the fold + /// range once, so we bundle them into a struct rather than recomputing. + private struct FoldAnimationMetrics { + /// First document Y the paper-fold occupies (the bottom edge of the line just above the fold). + let foldRegionMinY: CGFloat + /// Height of the paper fold at full extension (equals the sum of the natural heights of every + /// line whose height goes to zero when collapsed). + let foldHeight: CGFloat + } + + /// Snapshot data for a fold animation capture. + private struct FoldSnapshot { + let snapshot: CGImage + let totalWidth: CGFloat + let capturedDocY: CGFloat + let capturedHeight: CGFloat + } + + /// Measures everything we need to know about the fold region in one pass. Must be called with the + /// fold lines at their natural (expanded) heights — the caller must force layout first for expand. + private func computeFoldMetrics( + fold: FoldRange, + firstLineInFold: TextLineStorage.TextLinePosition, + layoutManager: TextLayoutManager + ) -> FoldAnimationMetrics? { + // The first line in `fold.range` always stays at full height — it displays the placeholder inline. + // The fold region visually starts just below this line's bottom edge, which gives us a coordinate + // system where `foldHeight == sum of the heights of every line that gets zeroed`. + let foldRegionMinY = firstLineInFold.yPos + firstLineInFold.height + + var totalHeight: CGFloat = 0 + var lastLine: TextLineStorage.TextLinePosition = firstLineInFold + var iter = layoutManager.lineStorage.linesInRange(NSRange(fold.range)).makeIterator() + // First line is the first-line-in-fold; drop it. + _ = iter.next() + while let line = iter.next() { + totalHeight += line.height + lastLine = line + } + + // Matches `TextAttachmentManager.add`'s trailing-line case: when the fold range ends exactly on a + // line boundary (and isn't at document end), the line *after* the range also gets zeroed, so it's + // part of the animated region. + let rangeMax = fold.range.upperBound + if rangeMax != layoutManager.lineStorage.length, + lastLine.range.max == rangeMax, + let trailingLine = layoutManager.lineStorage.getLine(atOffset: rangeMax), + trailingLine.height != 0 { + totalHeight += trailingLine.height + } + + guard totalHeight > 0 else { return nil } + return FoldAnimationMetrics(foldRegionMinY: foldRegionMinY, foldHeight: totalHeight) + } + + /// Mounted pieces of a fold animation: the scroll-view-relative overlay and the single paper + /// fold layer hosted inside it. + private struct MountedFold { + let overlay: FoldAnimationOverlay + let foldLayer: PaperFoldAnimationLayer + } + + /// Builds and mounts a `FoldAnimationOverlay` with a paper-fold layer positioned over the + /// text-view area only. The gutter's line numbers are drawn live (with view-zone whitespace + /// offsets) and animate in lockstep with the text, so they do not need to be snapshotted. + /// Returns nil if mounting fails. + private func mountOverlay( + scrollView: NSScrollView, + snapshot: CGImage, + foldDocY: CGFloat, + foldHeight: CGFloat, + totalWidth: CGFloat + ) -> MountedFold? { + let overlay = FoldAnimationOverlay(scrollView: scrollView) + // Normal subview (not floating): a floating overlay gets repositioned by the scroll view + // on each scroll event, which offsets the fold away from the captured region. The gutter + // is a floating subview and draws on top of this overlay — but the fold layer is + // positioned past the gutter's trailing edge, so the two never overlap. + scrollView.addSubview(overlay, positioned: .above, relativeTo: nil) + guard let overlayLayer = overlay.layer else { + overlay.removeFromSuperview() + return nil + } + + let scale = overlay.window?.backingScaleFactor ?? 2.0 + let overlayFoldY = overlay.documentYToOverlayY(foldDocY) + + // Fold layer spans the text width, starting past the gutter so it doesn't clash with + // the live line numbers. + let foldRect = NSRect( + x: 0.0, y: overlayFoldY, width: totalWidth, height: foldHeight + ) + let foldLayer = PaperFoldAnimationLayer( + snapshot: snapshot, foldRect: foldRect, backingScale: scale + ) + overlayLayer.addSublayer(foldLayer) + + return MountedFold(overlay: overlay, foldLayer: foldLayer) + } + + /// Transparent overlay view sitting above the text view (but below the floating gutter) during + /// a fold animation. Hosts the paper-fold layer. + /// + /// Lives in scroll-view coordinates (not document coordinates). When the user scrolls + /// mid-animation, we translate the hosted layer via `sublayerTransform` so the fold stays + /// aligned with the folding region. + private final class FoldAnimationOverlay: NSView { + private weak var scrollView: NSScrollView? + private var scrollObserver: NSObjectProtocol? + /// The scroll origin Y captured when layers were positioned. Deltas from this are applied as + /// a translation on `sublayerTransform` so the fold stays attached to its document Y. + private let initialScrollOriginY: CGFloat + + override var isFlipped: Bool { true } + override var wantsDefaultClipping: Bool { false } + + init(scrollView: NSScrollView) { + self.scrollView = scrollView + self.initialScrollOriginY = scrollView.contentView.bounds.origin.y + super.init(frame: scrollView.bounds) + wantsLayer = true + layer?.masksToBounds = false + autoresizingMask = [.width, .height] + + // If the user scrolls mid-animation, shift the overlay layer so fold + sliding content + // stay anchored to the text they represent. Using `queue: nil` ensures the handler + // runs synchronously during the clip view's bounds change — `queue: .main` would + // enqueue the block for the next run-loop iteration, causing a visible one-frame lag. + scrollObserver = NotificationCenter.default.addObserver( + forName: NSView.boundsDidChangeNotification, + object: scrollView.contentView, + queue: nil + ) { [weak self] _ in + self?.syncScrollOffset() + } } + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + deinit { + if let scrollObserver { + NotificationCenter.default.removeObserver(scrollObserver) + } + } + + /// Converts a document Y coordinate into this overlay's (scroll-view-relative) coords. Uses + /// the scroll origin captured at init so the conversion stays consistent with the coordinate + /// space the animation was anchored in — even if the user scrolls while the animation is in + /// flight, positions remain correct relative to the fold. + func documentYToOverlayY(_ docY: CGFloat) -> CGFloat { + return docY - initialScrollOriginY + } + + private func syncScrollOffset() { + guard let layer, let scrollView else { return } + CATransaction.begin() + CATransaction.setDisableActions(true) + let delta = initialScrollOriginY - scrollView.contentView.bounds.origin.y + layer.sublayerTransform = CATransform3DMakeTranslation(0, delta, 0) + CATransaction.commit() + } + } + + /// Captures the text-area portion of the fold region as a single bitmap. The gutter is not + /// captured because its line numbers animate live via view-zone whitespace offsets. + /// + /// The capture region is clipped to the scroll view's visible rect. Content outside the + /// viewport isn't rendered into bitmaps reliably and can be enormous for large folds. + /// + /// Returns a `FoldSnapshot` struct containing the snapshot, the total width, and the clipped document-Y / height so callers + /// can position the paper-fold layer over the captured region rather than the full fold. + private func captureFoldSnapshot( + docY: CGFloat, + height: CGFloat, + controller: TextViewController + ) -> FoldSnapshot? { + guard let textView = controller.textView, + let gutterView = controller.gutterView, + let scrollView = controller.scrollView else { return nil } + + // Clip the capture region to what's actually visible in the viewport. + let visibleRect = scrollView.contentView.bounds + let clippedMinY = max(docY, visibleRect.minY) + let clippedMaxY = min(docY + height, visibleRect.maxY) + let clippedHeight = clippedMaxY - clippedMinY + + let gutterWidth = gutterView.bounds.width + let scrollWidth = scrollView.bounds.width + let textWidth = scrollWidth - gutterWidth + let textX = textView.visibleRect.origin.x + gutterWidth + + guard textWidth > 0, clippedHeight > 0 else { return nil } + + let gutterRect = NSRect(x: 0, y: clippedMinY, width: gutterWidth, height: clippedHeight) + let textRect = NSRect(x: textX, y: clippedMinY, width: textWidth, height: clippedHeight) + + guard let gutterSnap = PaperFoldAnimationLayer.captureSnapshot(of: gutterRect, in: gutterView), + let textSnap = PaperFoldAnimationLayer.captureSnapshot(of: textRect, in: textView) else { + return nil + } + + let totalSize = CGSize(width: scrollWidth, height: clippedHeight) + guard let composite = PaperFoldAnimationLayer.compositeSnapshot( + left: gutterSnap, + leftWidth: gutterWidth, + right: textSnap, + totalSize: totalSize + ) else { return nil } + + return FoldSnapshot( + snapshot: composite, + totalWidth: scrollWidth, + capturedDocY: clippedMinY, + capturedHeight: clippedHeight + ) + } + + /// Performs the actual collapse state change (adding attachment, toggling, re-layout). + private func performCollapse(fold: FoldRange, event: NSEvent) { + guard let controller = model?.controller, + let textView = controller.textView, + let layoutManager = textView.layoutManager else { return } + + let charWidth = controller.font.charWidth + let placeholder = LineFoldPlaceholder(delegate: model, fold: fold, charWidth: charWidth) + layoutManager.attachments.add(placeholder, for: NSRange(fold.range)) + model?.foldCache.toggleCollapse(forFold: fold) - model?.controller?.textView.needsLayout = true - model?.controller?.gutterView.needsDisplay = true + textView.needsLayout = true + controller.gutterView.needsDisplay = true mouseMoved(with: event) } diff --git a/Sources/CodeEditSourceEditor/Minimap/MinimapView.swift b/Sources/CodeEditSourceEditor/Minimap/MinimapView.swift index afd97dee7..530c5f8ef 100644 --- a/Sources/CodeEditSourceEditor/Minimap/MinimapView.swift +++ b/Sources/CodeEditSourceEditor/Minimap/MinimapView.swift @@ -267,6 +267,7 @@ public class MinimapView: FlippedNSView { } override public func hitTest(_ point: NSPoint) -> NSView? { + if isHidden { return nil } guard let point = superview?.convert(point, to: self) else { return nil } // For performance, don't hitTest the layout fragment views, but make sure the `documentVisibleView` is // hittable. diff --git a/Sources/CodeEditSourceEditor/RangeStore/RangeStore+Coalesce.swift b/Sources/CodeEditSourceEditor/RangeStore/RangeStore+Coalesce.swift index d6fb93259..f48c50443 100644 --- a/Sources/CodeEditSourceEditor/RangeStore/RangeStore+Coalesce.swift +++ b/Sources/CodeEditSourceEditor/RangeStore/RangeStore+Coalesce.swift @@ -23,7 +23,8 @@ extension RangeStore { } index = findIndex(at: range.lowerBound).index - if index > _guts.startIndex && index < _guts.endIndex && _guts.count > 1 { + // Strictly inside the rope implies a preceding run. Avoid `_guts.count`, which walks every run. + if index > _guts.startIndex && index < _guts.endIndex { index = _guts.index(before: index) coalesceRunAfter(index: &index) } diff --git a/Sources/CodeEditSourceEditor/RangeStore/RangeStore.swift b/Sources/CodeEditSourceEditor/RangeStore/RangeStore.swift index 5a1d63367..effcd1c6b 100644 --- a/Sources/CodeEditSourceEditor/RangeStore/RangeStore.swift +++ b/Sources/CodeEditSourceEditor/RangeStore/RangeStore.swift @@ -138,7 +138,7 @@ extension RangeStore { _guts.removeSubrange(range, in: OffsetMetric()) } - if _guts.count > 0 { + if !_guts.isEmpty { // Coalesce nearby items if necessary. coalesceNearby(range: Range(lowerBound: range.lowerBound, length: newLength)) } diff --git a/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor+Coordinator.swift b/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor+Coordinator.swift index fe7420f95..55331f149 100644 --- a/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor+Coordinator.swift +++ b/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor+Coordinator.swift @@ -19,6 +19,11 @@ extension SourceEditor { var text: TextAPI @Binding var editorState: SourceEditorState + /// The scroll position last reported to, or applied from, ``editorState``. A state position equal to it came + /// from the text view and is not re-applied: during a resize the clip view's origin moves while the binding + /// lags a runloop turn behind, and scrolling back to the stale value would fight the resize. + var lastSentScrollPosition: CGPoint? + private(set) var highlightProviders: [any HighlightProviding] private var cancellables: Set = [] @@ -141,6 +146,7 @@ extension SourceEditor { return } let currentPosition = controller.scrollView.contentView.bounds.origin + lastSentScrollPosition = currentPosition if editorState.scrollPosition != currentPosition { updateState { $0.scrollPosition = currentPosition } } diff --git a/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift b/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift index ed3ee508b..c7753b09f 100644 --- a/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift +++ b/Sources/CodeEditSourceEditor/SourceEditor/SourceEditor.swift @@ -139,7 +139,7 @@ public struct SourceEditor: NSViewControllerRepresentable { context.coordinator.isUpdateFromTextView = false } else { context.coordinator.isUpdatingFromRepresentable = true - updateControllerWithState(state, controller: controller) + updateControllerWithState(state, controller: controller, coordinator: context.coordinator) context.coordinator.isUpdatingFromRepresentable = false } @@ -159,13 +159,21 @@ public struct SourceEditor: NSViewControllerRepresentable { return } - private func updateControllerWithState(_ state: SourceEditorState, controller: TextViewController) { + private func updateControllerWithState( + _ state: SourceEditorState, + controller: TextViewController, + coordinator: Coordinator + ) { if let cursorPositions = state.cursorPositions, cursorPositions != state.cursorPositions { controller.setCursorPositions(cursorPositions) } + // Skip positions the text view reported itself (see `Coordinator.lastSentScrollPosition`). let scrollView = controller.scrollView - if let scrollPosition = state.scrollPosition, scrollPosition != scrollView?.contentView.bounds.origin { + if let scrollPosition = state.scrollPosition, + scrollPosition != coordinator.lastSentScrollPosition, + scrollPosition != scrollView?.contentView.bounds.origin { + coordinator.lastSentScrollPosition = scrollPosition controller.scrollView.scroll(controller.scrollView.contentView, to: scrollPosition) controller.scrollView.reflectScrolledClipView(controller.scrollView.contentView) controller.gutterView.needsDisplay = true diff --git a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift index 6ca03f2a7..33d5f9782 100644 --- a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift +++ b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Appearance.swift @@ -87,6 +87,7 @@ extension SourceEditorConfiguration { controller.textView.font = font controller.textView.typingAttributes = controller.attributesFor(nil) controller.gutterView.font = font.rulerFont + controller.invisibleCharactersCoordinator.font = font needsHighlighterInvalidation = true } @@ -167,9 +168,14 @@ extension SourceEditorConfiguration { .windowBackgroundColor } + controller.gutterView.gitChangeIndicator.addedColor = theme.gutterAddedColor + controller.gutterView.gitChangeIndicator.modifiedColor = theme.gutterModifiedColor + controller.gutterView.gitChangeIndicator.deletedColor = theme.gutterDeletedColor + controller.minimapView.setTheme(theme) controller.reformattingGuideView?.theme = theme controller.textView.typingAttributes = controller.attributesFor(nil) + controller.invisibleCharactersCoordinator.theme = theme } /// Finds the preferred use theme background. diff --git a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Behavior.swift b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Behavior.swift index 28255f14d..81c1cd95d 100644 --- a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Behavior.swift +++ b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Behavior.swift @@ -50,6 +50,7 @@ extension SourceEditorConfiguration { if oldConfig?.indentOption != indentOption { controller.setUpTextFormation() + controller.invisibleCharactersCoordinator.indentOption = indentOption } if oldConfig?.reformatAtColumn != reformatAtColumn { diff --git a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Peripherals.swift b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Peripherals.swift index b77cc719f..ae469fb1c 100644 --- a/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Peripherals.swift +++ b/Sources/CodeEditSourceEditor/SourceEditorConfiguration/SourceEditorConfiguration+Peripherals.swift @@ -28,13 +28,17 @@ extension SourceEditorConfiguration { /// non-standard quote character: `“ (0x201C)`. public var warningCharacters: Set + /// Whether to show git change indicators in the gutter. + public var showGitChangeIndicators: Bool + public init( showGutter: Bool = true, showMinimap: Bool = true, showReformattingGuide: Bool = false, showFoldingRibbon: Bool = true, invisibleCharactersConfiguration: InvisibleCharactersConfiguration = .empty, - warningCharacters: Set = [] + warningCharacters: Set = [], + showGitChangeIndicators: Bool = true ) { self.showGutter = showGutter self.showMinimap = showMinimap @@ -42,6 +46,7 @@ extension SourceEditorConfiguration { self.showFoldingRibbon = showFoldingRibbon self.invisibleCharactersConfiguration = invisibleCharactersConfiguration self.warningCharacters = warningCharacters + self.showGitChangeIndicators = showGitChangeIndicators } @MainActor @@ -67,6 +72,10 @@ extension SourceEditorConfiguration { controller.gutterView.showFoldingRibbon = showFoldingRibbon } + if oldConfig?.showGitChangeIndicators != showGitChangeIndicators { + controller.gutterView.showGitChangeIndicators = showGitChangeIndicators + } + if oldConfig?.invisibleCharactersConfiguration != invisibleCharactersConfiguration { controller.invisibleCharactersCoordinator.configuration = invisibleCharactersConfiguration } diff --git a/Sources/CodeEditSourceEditor/Theme/EditorTheme.swift b/Sources/CodeEditSourceEditor/Theme/EditorTheme.swift index c44cfc96f..792d2cd12 100644 --- a/Sources/CodeEditSourceEditor/Theme/EditorTheme.swift +++ b/Sources/CodeEditSourceEditor/Theme/EditorTheme.swift @@ -42,6 +42,15 @@ public struct EditorTheme: Equatable { public var characters: Attribute public var comments: Attribute + // MARK: - Gutter Change Indicator Colors + + /// The color for "added" change indicators in the gutter. + public var gutterAddedColor: NSColor + /// The color for "modified" change indicators in the gutter. + public var gutterModifiedColor: NSColor + /// The color for "deleted" change indicators in the gutter. + public var gutterDeletedColor: NSColor + public init( text: Attribute, insertionPoint: NSColor, @@ -58,7 +67,10 @@ public struct EditorTheme: Equatable { numbers: Attribute, strings: Attribute, characters: Attribute, - comments: Attribute + comments: Attribute, + gutterAddedColor: NSColor? = nil, + gutterModifiedColor: NSColor? = nil, + gutterDeletedColor: NSColor? = nil ) { self.text = text self.insertionPoint = insertionPoint @@ -76,6 +88,11 @@ public struct EditorTheme: Equatable { self.strings = strings self.characters = characters self.comments = comments + + // Default gutter colors + self.gutterAddedColor = gutterAddedColor ?? NSColor(srgbRed: 0.267, green: 0.690, blue: 0.345, alpha: 1.0) + self.gutterModifiedColor = gutterModifiedColor ?? NSColor(srgbRed: 0.196, green: 0.533, blue: 0.886, alpha: 1.0) + self.gutterDeletedColor = gutterDeletedColor ?? NSColor(srgbRed: 0.196, green: 0.533, blue: 0.886, alpha: 1.0) } /// Maps a capture type to the attributes for that capture determined by the theme. diff --git a/Sources/CodeEditSourceEditor/TreeSitter/LanguageLayer.swift b/Sources/CodeEditSourceEditor/TreeSitter/LanguageLayer.swift index 89d934914..fd55eec15 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/LanguageLayer.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/LanguageLayer.swift @@ -158,7 +158,8 @@ public class LanguageLayer: Hashable { } wasLongParse = true } - newTree = DispatchQueue.waitMainIfNot { parser.parse(tree: tree, readBlock: readBlock) } + // The read block reads a text snapshot, so the parse runs on the calling thread. + newTree = parser.parse(tree: tree, readBlock: readBlock) } if wasLongParse { diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Edit.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Edit.swift index 7d78e0c6c..afd3147f0 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Edit.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Edit.swift @@ -15,10 +15,14 @@ extension TreeSitterClient { /// Concurrency note: This method checks for task cancellation between layer edits, after editing all layers, and /// before setting the client's state. /// - /// - Parameter edit: The edit to apply to the internal tree sitter state. + /// - Parameters: + /// - edit: The edit to apply to the internal tree sitter state. + /// - snapshot: The text after the edit (and after every pending edit). /// - Returns: The set of ranges invalidated by the edit operation. - func applyEdit(edit: InputEdit) -> IndexSet { - guard let state = state?.copy(), let readBlock, let readCallback else { return IndexSet() } + func applyEdit(edit: InputEdit, snapshot: TreeSitterTextSnapshot) -> IndexSet { + guard let state = state?.copy() else { return IndexSet() } + let readBlock = snapshot.readBlock + let readCallback = snapshot.readCallback let pendingEdits = pendingEdits.value() // Grab pending edits. let edits = pendingEdits + [edit] @@ -60,8 +64,9 @@ extension TreeSitterClient { if Task.isCancelled { return IndexSet() } - self.state = state // Apply the copied state - self.pendingEdits.mutate { edits in // Clear the queue + self.state = state + self.readCallback = readCallback + self.pendingEdits.mutate { edits in edits = [] } diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Highlight.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Highlight.swift index ff8f23f48..e1349118e 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Highlight.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient+Highlight.swift @@ -57,8 +57,6 @@ extension TreeSitterClient { return [] } - // This needs to be on the main thread since we're going to use the `textProvider` in - // the `highlightsFromCursor` method, which uses the textView's text storage. guard let queryCursor = layer.languageQuery?.execute(node: rootNode, in: tree) else { return [] } @@ -77,8 +75,8 @@ extension TreeSitterClient { return highlights } - /// Resolves a query cursor to the highlight ranges it contains. - /// **Must be called on the main thread** + /// Resolves a query cursor to the highlight ranges it contains. Predicates read the text snapshot of the current + /// state (``readCallback``), so this runs on any thread. /// - Parameters: /// - cursor: The cursor to resolve. /// - includedRange: The range to include highlights from. @@ -87,10 +85,10 @@ extension TreeSitterClient { cursor: QueryCursor, includedRange: NSRange ) -> [HighlightRange] { - guard let readCallback else { return [] } + guard let textProvider = readCallback else { return [] } var ranges: [NSRange: Int] = [:] return cursor - .resolve(with: .init(textProvider: readCallback)) // Resolve our cursor against the query + .resolve(with: .init(textProvider: textProvider)) // Resolve our cursor against the query .flatMap { $0.captures } .reversed() // SwiftTreeSitter returns captures in the reverse order of what we need to filter with. .compactMap { capture in diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient.swift index 44a0a3473..bf174922c 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterClient.swift @@ -10,6 +10,7 @@ import CodeEditTextView import CodeEditLanguages import SwiftTreeSitter import OSLog +import AppKit /// # TreeSitterClient /// @@ -35,11 +36,14 @@ public final class TreeSitterClient: HighlightProviding { // MARK: - Properties - /// A callback to use to efficiently fetch portions of text. - var readBlock: Parser.ReadBlock? + /// Reads text for query predicates, from the snapshot the current ``state`` was parsed from. Atomic: setup and + /// edits replace it while queries read it on another thread. + var readCallback: SwiftTreeSitter.Predicate.TextProvider? { + get { currentReadCallback.value() } + set { currentReadCallback.mutate { $0 = newValue } } + } - /// A callback used to fetch text for queries. - var readCallback: SwiftTreeSitter.Predicate.TextProvider? + private let currentReadCallback = Atomic(nil) /// The internal tree-sitter layer tree object. var state: TreeSitterState? @@ -78,6 +82,10 @@ public final class TreeSitterClient: HighlightProviding { /// The maximum length a document can be before all queries and edits must be processed asynchronously. public static var maxSyncContentLength: Int = 1_000_000 + /// The maximum document length that will be parsed synchronously during initial setup. + /// Documents at or below this length are parsed on the main thread during setUp for instant highlights. + public static var maxSyncInitialParseLength: Int = 500_000 + /// The maximum length a query can be before it must be performed asynchronously. public static var maxSyncQueryLength: Int = 4096 @@ -114,24 +122,32 @@ public final class TreeSitterClient: HighlightProviding { public func setUp(textView: TextView, codeLanguage: CodeLanguage) { Self.logger.debug("TreeSitterClient setting up with language: \(codeLanguage.id.rawValue, privacy: .public)") - let readBlock = textView.createReadBlock() - let readCallback = textView.createReadCallback() - self.readBlock = readBlock - self.readCallback = readCallback + let snapshot = TreeSitterTextSnapshot(textStorage: textView.textStorage) + self.readCallback = snapshot.readCallback - let operation = { [weak self] in + executor.cancelAll(below: .all) + + let documentLength = textView.documentRange.length + + if documentLength <= Constants.maxSyncInitialParseLength || forceSyncOperation { + // Small documents: parse synchronously on the main thread. + // Highlights are queryable immediately — no flash. let state = TreeSitterState( codeLanguage: codeLanguage, - readCallback: readCallback, - readBlock: readBlock + readCallback: snapshot.readCallback, + readBlock: snapshot.readBlock ) - self?.state = state - } - - executor.cancelAll(below: .all) - if forceSyncOperation { - executor.execSync(operation) + self.state = state } else { + // Larger documents: parse asynchronously from the snapshot, without touching the main thread. + let operation = { [weak self] in + let state = TreeSitterState( + codeLanguage: codeLanguage, + readCallback: snapshot.readCallback, + readBlock: snapshot.readBlock + ) + self?.state = state + } executor.execAsync(priority: .reset, operation: operation, onCancel: {}) } } @@ -157,8 +173,10 @@ public final class TreeSitterClient: HighlightProviding { return } + // The parse reads the text as of this edit, on whichever thread runs it. + let snapshot = TreeSitterTextSnapshot(textStorage: textView.textStorage) let operation = { [weak self] in - return self?.applyEdit(edit: edit) ?? IndexSet() + return self?.applyEdit(edit: edit, snapshot: snapshot) ?? IndexSet() } let longEdit = range.length > Constants.maxSyncEditLength @@ -177,7 +195,11 @@ public final class TreeSitterClient: HighlightProviding { executor.cancelAll(below: .reset) // Cancel all edits, add it to the pending edit queue executor.execAsync( priority: .edit, - operation: { completion(.success(operation())) }, + operation: { + // Runs on the executor's thread; `completion` is main-actor code. + let invalidatedRanges = operation() + DispatchQueue.dispatchMainIfNot { completion(.success(invalidatedRanges)) } + }, onCancel: { [weak self] in self?.pendingEdits.mutate { edits in edits.append(edit) diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterExecutor.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterExecutor.swift index fbba65741..bf5d96651 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterExecutor.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterExecutor.swift @@ -53,6 +53,10 @@ final package class TreeSitterExecutor { private let lock = NSLock() private var queuedTasks: [QueueItem] = [] + /// Test hook, awaited at the start of every async operation before it checks whether it may run or was cancelled. + /// Lets tests keep operations pending while they cancel them. + var asyncStartGate: (@Sendable () async -> Void)? + enum Error: Swift.Error { case syncUnavailable } @@ -95,9 +99,29 @@ final package class TreeSitterExecutor { lock.lock() defer { lock.unlock() } let id = UUID() + let startGate = asyncStartGate let task = Task(priority: .userInitiated) { // __This executes outside the outer lock's control__ + if let startGate { + await startGate() + } + // Fast path: if we can execute immediately, do so without any delay + if self.lock.withLock({ canTaskExec(id: id, priority: priority) }) { + guard !Task.isCancelled else { + removeTask(id) + onCancel() + return + } + operation() + if Task.isCancelled { + onCancel() + } + removeTask(id) + return + } + + // Slow path: need to wait for our turn while self.lock.withLock({ !canTaskExec(id: id, priority: priority) }) { - // Instead of yielding, sleeping frees up the CPU due to time off the CPU and less lock contention + // Use a shorter initial check, then back off try? await Task.sleep(for: TreeSitterClient.Constants.taskSleepDuration) guard !Task.isCancelled else { removeTask(id) @@ -142,16 +166,22 @@ final package class TreeSitterExecutor { } /// Allow concurrent ``TreeSitterExecutor/Priority/access`` operations to run. Thread safe. + /// Access operations can run concurrently with each other, but not with edit/reset operations. private func canTaskExec(id: UUID, priority: Priority) -> Bool { + // Non-access operations must wait for their turn (first in queue) if priority != .access { return queuedTasks.first?.id == id } + // Access operations can run concurrently, unless there's a higher priority operation pending for task in queuedTasks { if task.priority != .access { + // There's a non-access operation in the queue, access operations must wait return false - } else { - return task.id == id + } + if task.id == id { + // We found ourselves before any blocking operation + return true } } assertionFailure("Task asking if it can exec but it's not in the queue.") diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterState.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterState.swift index eb50c058a..17afc9483 100644 --- a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterState.swift +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterState.swift @@ -80,6 +80,8 @@ public final class TreeSitterState { layers[0].parser.timeout = 0.0 layers[0].tree = layers[0].parser.parse(tree: nil as Tree?, readBlock: readBlock) + guard layers[0].tree != nil else { return } + var layerSet = Set(arrayLiteral: layers[0]) var touchedLayers = Set() diff --git a/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterTextSnapshot.swift b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterTextSnapshot.swift new file mode 100644 index 000000000..f3d24221c --- /dev/null +++ b/Sources/CodeEditSourceEditor/TreeSitter/TreeSitterTextSnapshot.swift @@ -0,0 +1,48 @@ +// +// TreeSitterTextSnapshot.swift +// CodeEditSourceEditor +// + +import AppKit +import SwiftTreeSitter + +/// An immutable copy of a document's text, with the tree-sitter callbacks that read it from any thread. +/// +/// Taking one is O(1) (see ``NSTextStorage/stringSnapshot``). Take it on the main thread, when the text matches the +/// edits the parse will apply. +struct TreeSitterTextSnapshot { + let text: NSString + + init(textStorage: NSTextStorage) { + self.text = textStorage.stringSnapshot + } + + /// Reads up to ``TreeSitterClient/Constants/charsToReadInBlock`` UTF-16 units at a byte offset. + var readBlock: Parser.ReadBlock { + let text = text + let length = text.length + return { byteOffset, _ in + let location = byteOffset / 2 + let end = min(location + TreeSitterClient.Constants.charsToReadInBlock, length) + guard location < end else { return nil } + // Raw code units: a block boundary can split a surrogate pair, which a `String` round trip would lose. + var data = Data(count: (end - location) * MemoryLayout.stride) + data.withUnsafeMutableBytes { buffer in + text.getCharacters( + buffer.baseAddress!.assumingMemoryBound(to: unichar.self), + range: NSRange(location: location, length: end - location) + ) + } + return data + } + } + + /// Reads the text of a range, for query predicates and injections. + var readCallback: SwiftTreeSitter.Predicate.TextProvider { + let text = text + return { range, _ in + guard range.location >= 0, NSMaxRange(range) <= text.length else { return nil } + return text.substring(with: range) + } + } +} diff --git a/Sources/CodeEditSourceEditor/Utils/CursorPosition.swift b/Sources/CodeEditSourceEditor/Utils/CursorPosition.swift index 805b874f2..ad20a6892 100644 --- a/Sources/CodeEditSourceEditor/Utils/CursorPosition.swift +++ b/Sources/CodeEditSourceEditor/Utils/CursorPosition.swift @@ -30,7 +30,7 @@ public struct CursorPosition: Sendable, Codable, Equatable, Hashable { self.column = column } - var isPositive: Bool { line > 0 && column > 0 } + public var isPositive: Bool { line > 0 && column > 0 } } /// Initialize a cursor position. diff --git a/Sources/CodeEditSourceEditor/Utils/PaperFoldAnimationLayer.swift b/Sources/CodeEditSourceEditor/Utils/PaperFoldAnimationLayer.swift new file mode 100644 index 000000000..53637d645 --- /dev/null +++ b/Sources/CodeEditSourceEditor/Utils/PaperFoldAnimationLayer.swift @@ -0,0 +1,392 @@ +// +// PaperFoldAnimationLayer.swift +// CodeEditTextView +// +// Created by Abe Malla on 4/11/26. +// + +import AppKit +import QuartzCore + +/// A reusable Core Animation layer that performs a physically-accurate "paper fold" animation. +/// +/// ## Geometry +/// +/// The fold simulates a real sheet of paper bending in half. The two halves keep their full +/// length at every stage of the animation — only the crease changes position: +/// +/// The layer uses CA's default bottom-up Y internally, so `y = foldHeight` corresponds visually to +/// the **line above** the fold region and `y = 0` to the **line below**. +/// +/// - The **bottom half** is anchored at its top edge and pinned at `y = foldHeight` — the boundary +/// above the fold region (the line just above the fold, which stays put). It rotates around that +/// edge, sending its crease end backward into `-z`. +/// - The **top half** is anchored at its bottom edge. Its position slides *upward* along Y as the +/// fold closes: `y = foldHeight − projected = foldHeight · (1 − cos θ)`. It rotates around that +/// edge, also sending its crease end backward into `-z`. +/// - Because both halves are the same length and rotate by the same angle symmetrically, the two +/// crease ends always meet. At `θ = 0` the paper is flat and the outer edges are `foldHeight` +/// apart; at `θ = π/2` the paper is fully folded and both outer edges meet at `y = foldHeight` +/// — the line above — while the line below rides the view-zone shrink upward to meet them. +/// +/// A gentle perspective (`m34 = -1/1200`) makes the receding crease read as depth. The +/// perspective vanishing point is re-centered each frame at the visible midpoint +/// (`projected/2`) so the crease always appears exactly centered between the two halves, +/// regardless of fold angle. +/// +/// ## Shading +/// +/// A dark overlay on each half ramps up proportional to `sin(θ)`, matching the lighting cue in +/// Xcode's native fold: the rotated surface receives less light, so it reads darker. +/// +/// ## Clip +/// +/// A shape-layer mask clips the layer to the paper's projected height (`foldHeight · cos(θ)`). +/// This keeps the halves from spilling outside the shrinking footprint. +/// +/// ## Driving the animation +/// +/// Unlike a conventional `CAAnimation`-based layer, this one is driven directly by a caller via +/// ``setFoldAngle(_:)``. Drive it from a `CADisplayLink` in lockstep with any other animations +/// (e.g. text-view line-height transitions) so the pieces stay visually synchronized. +/// +/// ## Usage +/// +/// ```swift +/// let layer = PaperFoldAnimationLayer(snapshot: image, foldRect: rect, backingScale: 2) +/// parent.addSublayer(layer) +/// // Each display-link tick: +/// layer.setFoldAngle(currentTheta) +/// // When done: +/// layer.cleanup() +/// ``` +public final class PaperFoldAnimationLayer: CALayer { + + // MARK: - Sublayers + + /// Upper half of the paper. Pinned at its top edge (the boundary with the line above the + /// fold). Rotates around that top edge so its crease-end recedes into `-z`. + private let topHalf = CATransformLayer() + /// Lower half of the paper. Pinned at its bottom edge (the boundary with the line below + /// the fold). Rotates around that bottom edge. + private let bottomHalf = CATransformLayer() + + /// Image-backed layers showing the top/bottom halves of the snapshot. + private let topContent = CALayer() + private let bottomContent = CALayer() + + /// Dark shade overlays. Their opacity is driven by `sin(θ)` so rotated surfaces darken + /// predictably — the lighting cue that makes the depth change readable. + private let topShade = CALayer() + private let bottomShade = CALayer() + + /// Shape-layer mask that tracks the paper's projected 2D height — clips the layer down as + /// the fold closes so the halves don't spill beyond their logical footprint. + private let clipMask = CAShapeLayer() + + // MARK: - Public + + /// Height of the fold region at angle 0 (paper fully flat). + public let foldHeight: CGFloat + + /// Backing scale for pixel-accurate rendering on Retina. Propagated to every image-backed + /// sublayer so snapshots render at native resolution. + public var backingScale: CGFloat { + didSet { applyBackingScale() } + } + + /// Maximum opacity of the dark shade overlay on the bottom half at full fold (0…1). + public var maxBottomShadeOpacity: Float = 0.16 + + /// Maximum opacity of the dark shade overlay on the top half at full fold (0…1). + public var maxTopShadeOpacity: Float = 0.12 + + // MARK: - Init + + /// Creates a paper-fold layer. + /// + /// - Parameters: + /// - snapshot: A full-width bitmap of the fold region. The top half of the image maps to + /// the top half of the fold, the bottom half to the bottom half. + /// - foldRect: The rect (in the parent layer's coordinate space) where the fold sits at + /// angle 0. Assumes a flipped (top-down Y) coord system — the same one an AppKit + /// flipped NSView would use. + /// - backingScale: The screen scale. Pass `window.backingScaleFactor` for correct Retina + /// rendering. + public init( + snapshot: CGImage, + foldRect: NSRect, + backingScale: CGFloat = NSScreen.main?.backingScaleFactor ?? 2.0 + ) { + self.foldHeight = foldRect.height + self.backingScale = backingScale + super.init() + + self.frame = foldRect + self.isGeometryFlipped = true + self.masksToBounds = false + self.contentsScale = backingScale + self.actions = Self.allDisabledActions + + applyPerspective() + buildHalves(snapshot: snapshot) + buildShading() + buildMask() + setFoldAngle(0) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override init(layer: Any) { + if let other = layer as? PaperFoldAnimationLayer { + self.foldHeight = other.foldHeight + self.backingScale = other.backingScale + } else { + self.foldHeight = 0 + self.backingScale = 2.0 + } + super.init(layer: layer) + } + + // MARK: - Setup + + private func applyPerspective() { + // Small m34 gives a subtle perspective — the crease reads as receding without visibly + // distorting the text. 1/1200 matches Xcode's gentle foreshortening. + var perspective = CATransform3DIdentity + perspective.m34 = -1.0 / 1200.0 + self.sublayerTransform = perspective + } + + private func buildHalves(snapshot: CGImage) { + let halfHeight = foldHeight / 2.0 + let width = bounds.width + let halfSize = CGSize(width: width, height: halfHeight) + let centerX = width / 2.0 + + // --- Top half: anchored at its TOP edge (outer edge of the fold). + // In flipped geometry, anchor y=0 is at the top edge. + // Position places the anchor at (centerX, 0) in the container — the top of the fold region. + topHalf.anchorPoint = CGPoint(x: 0.5, y: 0.0) + topHalf.position = CGPoint(x: centerX, y: 0) + topHalf.bounds = CGRect(origin: .zero, size: halfSize) + topHalf.isDoubleSided = false + topHalf.actions = Self.allDisabledActions + + // `contentsRect` samples the image in its native (bottom-left origin) unit space — geometry + // flipping on the layer doesn't affect sampling. So the TOP half of a CGImage (top rows of + // pixels) corresponds to Y=0.5…1.0 in contentsRect, which is what the top-of-paper displays. + configureContent( + layer: topContent, + snapshot: snapshot, + bounds: halfSize, + contentsRect: CGRect(x: 0, y: 0.5, width: 1, height: 0.5) + ) + topHalf.addSublayer(topContent) + + // --- Bottom half: anchored at its BOTTOM edge (outer edge of the fold). + // anchor y=1.0 in flipped geometry = bottom edge. + // position.y is driven by setFoldAngle so the anchor tracks `foldHeight · cos(θ)`. + bottomHalf.anchorPoint = CGPoint(x: 0.5, y: 1.0) + bottomHalf.position = CGPoint(x: centerX, y: foldHeight) + bottomHalf.bounds = CGRect(origin: .zero, size: halfSize) + bottomHalf.isDoubleSided = false + bottomHalf.actions = Self.allDisabledActions + + // Bottom half of the snapshot (bottom rows of pixels = Y=0…0.5 in contentsRect unit space). + configureContent( + layer: bottomContent, + snapshot: snapshot, + bounds: halfSize, + contentsRect: CGRect(x: 0, y: 0, width: 1, height: 0.5) + ) + bottomHalf.addSublayer(bottomContent) + + addSublayer(topHalf) + addSublayer(bottomHalf) + } + + private func configureContent( + layer: CALayer, + snapshot: CGImage, + bounds: CGSize, + contentsRect: CGRect + ) { + layer.frame = CGRect(origin: .zero, size: bounds) + layer.contents = snapshot + layer.contentsRect = contentsRect + layer.contentsGravity = .resize + // Without matching contentsScale, a 2× CGImage renders at 1× density on Retina → + // visibly blurry. Always match the backing scale. + layer.contentsScale = backingScale + layer.isGeometryFlipped = true + layer.masksToBounds = true + layer.actions = Self.allDisabledActions + } + + private func buildShading() { + for shade in [topShade, bottomShade] { + shade.backgroundColor = NSColor.black.cgColor + shade.opacity = 0 + shade.contentsScale = backingScale + shade.actions = Self.allDisabledActions + } + topShade.frame = topContent.frame + bottomShade.frame = bottomContent.frame + topHalf.addSublayer(topShade) + bottomHalf.addSublayer(bottomShade) + } + + private func buildMask() { + clipMask.frame = bounds + clipMask.path = CGPath(rect: bounds, transform: nil) + clipMask.actions = Self.allDisabledActions + self.mask = clipMask + } + + private func applyBackingScale() { + self.contentsScale = backingScale + topContent.contentsScale = backingScale + bottomContent.contentsScale = backingScale + topShade.contentsScale = backingScale + bottomShade.contentsScale = backingScale + } + + // MARK: - Driving the fold + + /// Drives the fold to the given angle. Call on every display-link tick. + /// - Parameter theta: 0 = flat (fully open), `π/2` = fully folded (closed). + public func setFoldAngle(_ theta: CGFloat) { + let cosT = cos(theta) + let sinT = sin(theta) + let projected = foldHeight * cosT + + CATransaction.begin() + CATransaction.setDisableActions(true) + + // Re-center the perspective vanishing point at the visible midpoint each frame. + // + // Core Animation applies `sublayerTransform` relative to the layer's bounds center + // (foldHeight/2), which is fixed. As the visible area shrinks to `projected`, the + // static center diverges from the visible midpoint, causing asymmetric foreshortening + // that shifts the crease away from center. The combined transform + // T(0, δ) · P · T(0, -δ) where δ = (foldHeight - projected) / 2 + // re-centers the projection at (width/2, foldHeight - projected/2) — the midpoint of the + // visible paper, which sits at the TOP of the layer's bounds (since the bottom half is + // pinned at `y = foldHeight` and the top half slides up toward it). + var persp = CATransform3DIdentity + persp.m34 = -1.0 / 1200.0 + let delta = (foldHeight - projected) / 2.0 + persp.m32 = persp.m34 * delta + self.sublayerTransform = persp + + // Top half rotates around its anchor (its bottom edge). Negative θ sends its top edge + // (the crease end) into -z — "fold back, away from the viewer." Its position slides + // *upward* along Y as the fold closes — the bottom of the fold region closes toward + // the stationary top. + topHalf.transform = CATransform3DMakeRotation(-theta, 1, 0, 0) + topHalf.position = CGPoint(x: bounds.width / 2, y: foldHeight - projected) + + // Bottom half rotates around its anchor (its top edge, pinned at `y = foldHeight` — + // the line above the fold). Positive θ sends its bottom edge (the crease end) into -z + // — symmetric with the top half. + bottomHalf.transform = CATransform3DMakeRotation(theta, 1, 0, 0) + + // The two halves are lit differently as they fold: + // - Bottom half folds underneath and faces progressively away from the viewer → darkens + // heavily using sin(θ), peaking at full fold. + // - Top half stays facing the viewer and only picks up a slight crease shadow, scaled + // by sin²(θ) so it remains subtle until near-fully-folded. + topShade.opacity = maxTopShadeOpacity * Float(sinT * sinT) + bottomShade.opacity = maxBottomShadeOpacity * Float(sinT) + + // Clip down to the paper's projected 2D height. The visible paper hugs the TOP of the + // layer's bounds (both halves converge at `y = foldHeight`, the line above), so the + // mask rect starts at `y = foldHeight - projected` and grows upward. + let clipHeight = max(0, projected) + clipMask.path = CGPath( + rect: CGRect(x: 0, y: foldHeight - clipHeight, width: bounds.width, height: clipHeight), + transform: nil + ) + + CATransaction.commit() + } + + /// Removes the layer from its parent. Safe to call even if it was never added. + public func cleanup() { + removeFromSuperlayer() + } + + // MARK: - Snapshot helpers + + /// Captures a bitmap of `rect` from `view`. Suitable for feeding into + /// ``init(snapshot:foldRect:backingScale:)``. + public static func captureSnapshot(of rect: NSRect, in view: NSView) -> CGImage? { + guard rect.width > 0 && rect.height > 0 else { return nil } + guard let bitmapRep = view.bitmapImageRepForCachingDisplay(in: rect) else { return nil } + view.cacheDisplay(in: rect, to: bitmapRep) + return bitmapRep.cgImage + } + + /// Stitches two images horizontally (left | right) into a single bitmap. Useful for + /// combining a gutter snapshot and a text-view snapshot into a single image covering the + /// full editor width. + public static func compositeSnapshot( + left: CGImage, + leftWidth: CGFloat, + right: CGImage, + totalSize: CGSize, + scale: CGFloat = NSScreen.main?.backingScaleFactor ?? 2.0 + ) -> CGImage? { + let pixelWidth = Int((totalSize.width * scale).rounded()) + let pixelHeight = Int((totalSize.height * scale).rounded()) + let leftPixelWidth = Int((leftWidth * scale).rounded()) + + guard pixelWidth > 0, pixelHeight > 0 else { return nil } + + // BGRA (little-endian premultiplied first) is the GPU-native format. Avoids a CoreGraphics + // conversion pass on the layer. + let bitmapInfo = CGImageAlphaInfo.premultipliedFirst.rawValue + | CGBitmapInfo.byteOrder32Little.rawValue + + guard let ctx = CGContext( + data: nil, + width: pixelWidth, + height: pixelHeight, + bitsPerComponent: 8, + bytesPerRow: 0, + space: CGColorSpaceCreateDeviceRGB(), + bitmapInfo: bitmapInfo + ) else { return nil } + + let leftRect = CGRect(x: 0, y: 0, width: leftPixelWidth, height: pixelHeight) + let rightRect = CGRect( + x: leftPixelWidth, + y: 0, + width: pixelWidth - leftPixelWidth, + height: pixelHeight + ) + ctx.interpolationQuality = .none + ctx.draw(left, in: leftRect) + ctx.draw(right, in: rightRect) + return ctx.makeImage() + } + + // MARK: - Helpers + + /// Disables implicit animations on every property we mutate per-frame. Without this, + /// CoreAnimation would try to cross-fade each change, fighting our display-link updates. + private static let allDisabledActions: [String: CAAction] = [ + "transform": NSNull(), + "position": NSNull(), + "bounds": NSNull(), + "opacity": NSNull(), + "contents": NSNull(), + "path": NSNull(), + "backgroundColor": NSNull(), + "hidden": NSNull() + ] +} diff --git a/Tests/CodeEditSourceEditorTests/Controller/TextViewControllerTests.swift b/Tests/CodeEditSourceEditorTests/Controller/TextViewControllerTests.swift index bef6615bd..c5392e34c 100644 --- a/Tests/CodeEditSourceEditorTests/Controller/TextViewControllerTests.swift +++ b/Tests/CodeEditSourceEditorTests/Controller/TextViewControllerTests.swift @@ -328,6 +328,31 @@ final class TextViewControllerTests: XCTestCase { ) } + func test_findClosingPairClampsLimitsToTheDocument() { + controller.textView.string = "{ {} }" + XCTAssertEqual(controller.findClosingPair("{", "}", from: 1, limit: 10_000, reverse: false), 5) + XCTAssertEqual(controller.findClosingPair("}", "{", from: 5, limit: -10_000, reverse: true), 0) + XCTAssertNil(controller.findClosingPair("{", "}", from: 4, limit: 2, reverse: false), "inverted window") + } + + func test_cursorColumnsClampToTheirLine() { + controller.setText("ab\r\ncd\nef") + func selectedRange(_ position: CursorPosition) -> NSRange? { + controller.setCursorPositions([position]) + return controller.textView.selectionManager.textSelections.first?.range + } + + // Past the end of a line: the end of its content, before the line ending. + XCTAssertEqual(selectedRange(CursorPosition(line: 1, column: 100)), NSRange(location: 2, length: 0)) + XCTAssertEqual(selectedRange(CursorPosition(line: 2, column: 100)), NSRange(location: 6, length: 0)) + XCTAssertEqual(selectedRange(CursorPosition(line: 3, column: 100)), NSRange(location: 9, length: 0)) + + // An end before the start is normalized. + let reversed = CursorPosition(start: .init(line: 2, column: 100), end: .init(line: 1, column: 1)) + XCTAssertEqual(selectedRange(reversed), NSRange(location: 0, length: 6)) + XCTAssertEqual(controller.resolveCursorPosition(reversed)?.range, NSRange(location: 0, length: 6)) + } + // MARK: Set Text func test_setText() { diff --git a/Tests/CodeEditSourceEditorTests/NSTextStorageSnapshotTests.swift b/Tests/CodeEditSourceEditorTests/NSTextStorageSnapshotTests.swift new file mode 100644 index 000000000..d91c66626 --- /dev/null +++ b/Tests/CodeEditSourceEditorTests/NSTextStorageSnapshotTests.swift @@ -0,0 +1,28 @@ +// +// NSTextStorageSnapshotTests.swift +// CodeEditSourceEditor +// + +import Testing +import AppKit +@testable import CodeEditSourceEditor + +/// Work that reads the text off the main thread needs a snapshot that edits leave alone. `string as NSString` is not +/// one: it is the live backing string. +struct NSTextStorageSnapshotTests { + @Test + func snapshotIsUnaffectedByLaterEdits() { + let text = (0..<2_000).map { "line \($0)" }.joined(separator: "\n") + let storage = NSTextStorage(string: text) + let snapshot = storage.stringSnapshot + let live = storage.string as NSString + + storage.replaceCharacters(in: NSRange(location: 10, length: 5), with: "edited") + storage.replaceCharacters(in: NSRange(location: 0, length: 0), with: "prefix ") + + #expect(snapshot as String == text) + #expect(storage.string.hasPrefix("prefix ")) + // The hazard the snapshot exists for: the bridged string changes with the storage. + #expect(live.length == storage.length) + } +} diff --git a/Tests/CodeEditSourceEditorTests/TreeSitterClientTests.swift b/Tests/CodeEditSourceEditorTests/TreeSitterClientTests.swift index d2446cc9a..4d6ea4574 100644 --- a/Tests/CodeEditSourceEditorTests/TreeSitterClientTests.swift +++ b/Tests/CodeEditSourceEditorTests/TreeSitterClientTests.swift @@ -1,9 +1,44 @@ import XCTest import CodeEditTextView +import SwiftTreeSitter @testable import CodeEditSourceEditor // swiftlint:disable all +/// Holds a ``TreeSitterExecutor``'s async operations at their start until opened (see +/// ``TreeSitterExecutor/asyncStartGate``), so operations a test cancels are still pending when it does. +private final class ExecutorGate: @unchecked Sendable { + private let lock = NSLock() + private var isOpen = false + private var waiting: [CheckedContinuation] = [] + + func hold(_ client: TreeSitterClient) { + client.executor.asyncStartGate = { [self] in await wait() } + } + + func wait() async { + await withCheckedContinuation { continuation in + lock.lock() + if isOpen { + lock.unlock() + continuation.resume() + } else { + waiting.append(continuation) + lock.unlock() + } + } + } + + func open() { + lock.lock() + isOpen = true + let continuations = waiting + waiting = [] + lock.unlock() + continuations.forEach { $0.resume() } + } +} + final class TreeSitterClientTests: XCTestCase { private var maxSyncContentLength: Int = 0 @@ -53,6 +88,67 @@ final class TreeSitterClientTests: XCTestCase { XCTAssertEqual(layerCount, 1, "Client set up too many layers") } + /// Edits to documents over `maxSyncContentLength` run on the executor's thread (`setUp` makes every document + /// count as long); their completion must still be delivered on the main thread. + @MainActor + func test_asyncEditCompletionRunsOnMainThread() async { + let client = Mock.treeSitterClient() + let textView = Mock.textView() + client.setUp(textView: textView, codeLanguage: .swift) + + let setUp = XCTestExpectation(description: "Setup occurs") + Task.detached { + while client.state == nil { + try await Task.sleep(for: .seconds(0.1)) + } + setUp.fulfill() + } + await fulfillment(of: [setUp], timeout: 5.0) + + let completed = XCTestExpectation(description: "Edit completes on the main thread") + performEdit(textView: textView, client: client, string: "1", range: NSRange(location: 0, length: 0)) { result in + XCTAssertTrue(Thread.isMainThread, "applyEdit's completion ran off the main thread") + if case .failure(let error) = result { + XCTFail("Edit failed: \(error)") + } + completed.fulfill() + } + await fulfillment(of: [completed], timeout: 5.0) + } + + /// Asynchronous edits parse from a text snapshot, so they finish while the main thread is blocked. + @MainActor + func test_asyncEditFinishesWhileTheMainThreadIsBlocked() throws { + let client = Mock.treeSitterClient() + let textView = Mock.textView() + client.setUp(textView: textView, codeLanguage: .swift) + let stateBefore = try XCTUnwrap(client.state, "small documents are parsed during setUp") + + let insertion = "let value = 1\n" + let length = (insertion as NSString).length + client.willApplyEdit(textView: textView, range: NSRange(location: 0, length: 0)) + textView.replaceString(in: NSRange(location: 0, length: 0), with: insertion) + client.applyEdit(textView: textView, range: NSRange(location: 0, length: length), delta: length) { _ in } + + // Block the main thread (no run loop, no main queue) until the edit commits its new state. + let deadline = Date().addingTimeInterval(10) + while client.state === stateBefore && Date() < deadline { + usleep(1_000) + } + XCTAssertFalse(client.state === stateBefore, "the edit did not finish without the main thread") + + let fresh = Mock.treeSitterClient() + fresh.setUp(textView: TextView(string: textView.string), codeLanguage: .swift) + XCTAssertNotNil(primaryTree(client)) + XCTAssertEqual(primaryTree(client), primaryTree(fresh)) + } + + /// The primary language layer's syntax tree as an S-expression. + func primaryTree(_ client: TreeSitterClient) -> String? { + guard let state = client.state else { return nil } + return state.layers.first { $0.id == state.primaryLayer.id }?.tree?.rootNode?.sExpressionString + } + func resultIsCancel(_ result: Result) -> Bool { if case let .failure(error) = result { if case HighlightProvidingError.operationCancelled = error { @@ -66,6 +162,8 @@ final class TreeSitterClientTests: XCTestCase { func test_editsDuringSetup() { let client = Mock.treeSitterClient() let textView = Mock.textView() + let gate = ExecutorGate() + gate.hold(client) client.setUp(textView: textView, codeLanguage: .swift) @@ -100,6 +198,8 @@ final class TreeSitterClientTests: XCTestCase { } } + // Every cancellation has been issued; let the queued operations run. + gate.open() wait(for: [cancelledQuery, cancelledEdit, successEdit], timeout: 5.0) } @@ -107,6 +207,8 @@ final class TreeSitterClientTests: XCTestCase { func test_multipleSetupsCancelAllOperations() async { let client = Mock.treeSitterClient() let textView = Mock.textView() + let gate = ExecutorGate() + gate.hold(client) // First setup, wrong language client.setUp(textView: textView, codeLanguage: .c) @@ -133,6 +235,7 @@ final class TreeSitterClientTests: XCTestCase { // Second setup, which should cancel all previous operations client.setUp(textView: textView, codeLanguage: .swift) + gate.open() let finalSetupExpectation = XCTestExpectation(description: "Final setup should complete successfully.") @@ -157,6 +260,8 @@ final class TreeSitterClientTests: XCTestCase { let client = Mock.treeSitterClient() let textView = Mock.textView() textView.setText("asadajkfijio;amfjamc;aoijaoajkvarpfjo;sdjlkj") + let gate = ExecutorGate() + gate.hold(client) client.setUp(textView: textView, codeLanguage: .swift) @@ -191,6 +296,7 @@ final class TreeSitterClientTests: XCTestCase { } } + gate.open() wait(for: editExpectations + [finalEditExpectation], timeout: 5.0) } }