diff --git a/Sources/SwiftDocC/Model/Rendering/References/FileReference.swift b/Sources/SwiftDocC/Model/Rendering/References/FileReference.swift index e43a7a456a..d34668aea9 100644 --- a/Sources/SwiftDocC/Model/Rendering/References/FileReference.swift +++ b/Sources/SwiftDocC/Model/Rendering/References/FileReference.swift @@ -39,6 +39,9 @@ public struct FileReference: RenderReference, Equatable { /// The line highlights for this file. public var highlights: [LineHighlighter.Highlight] = [] + /// The delete line highlights for this file. + public var deleteHighlights: [LineHighlighter.Highlight] = [] + /// Creates a new file reference. /// /// - Parameters: @@ -54,7 +57,8 @@ public struct FileReference: RenderReference, Equatable { fileType: String, syntax: String, content: [String], - highlights: [LineHighlighter.Highlight] = [] + highlights: [LineHighlighter.Highlight] = [], + deleteHighlights: [LineHighlighter.Highlight] = [] ) { self.identifier = identifier self.fileName = fileName @@ -62,6 +66,7 @@ public struct FileReference: RenderReference, Equatable { self.syntax = syntax self.content = content self.highlights = highlights + self.deleteHighlights = deleteHighlights } public init(from decoder: Decoder) throws { @@ -73,6 +78,7 @@ public struct FileReference: RenderReference, Equatable { syntax = try values.decode(String.self, forKey: .syntax) content = try values.decode([String].self, forKey: .content) highlights = try values.decodeIfPresent([LineHighlighter.Highlight].self, forKey: .highlights) ?? [] + deleteHighlights = (try? values.decodeIfPresent([LineHighlighter.Highlight].self, forKey: .deleteHighlights)) ?? [] } } diff --git a/Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift b/Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift index b3812146b9..f1ed3046ad 100644 --- a/Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift +++ b/Sources/SwiftDocC/Model/Rendering/RenderNodeTranslator.swift @@ -129,6 +129,7 @@ public struct RenderNodeTranslator: SemanticVisitor { // Add the highlights to the file references. for result in highlightsPerFile { fileReferences[result.file.path]?.highlights = result.highlights + fileReferences[result.file.path]?.deleteHighlights = result.deleteHighlights } return TutorialSectionsRenderSection.Section(title: tutorialSection.title, contentSection: introduction, stepsSection: stepsContent, anchor: urlReadableFragment(tutorialSection.title)) diff --git a/Sources/SwiftDocC/Model/Rendering/Tutorial/LineHighlighter.swift b/Sources/SwiftDocC/Model/Rendering/Tutorial/LineHighlighter.swift index f3a6c720d2..52757164fe 100644 --- a/Sources/SwiftDocC/Model/Rendering/Tutorial/LineHighlighter.swift +++ b/Sources/SwiftDocC/Model/Rendering/Tutorial/LineHighlighter.swift @@ -56,6 +56,9 @@ public struct LineHighlighter { /// The highlights to apply when displaying this file. let highlights: [Highlight] + + /// The delete highlights to apply when displaying this file. + let deleteHighlights: [Highlight] } /** @@ -121,7 +124,7 @@ public struct LineHighlighter { private func lineHighlights(old: ResourceReference, new: ResourceReference) -> Result { // Retrieve the contents of the current file and the file we're comparing against. guard let oldLines = lines(of: old), let newLines = lines(of: new) else { - return Result(file: new, highlights: []) + return Result(file: new, highlights: [], deleteHighlights: []) } let diff = newLines.difference(from: oldLines) @@ -134,14 +137,21 @@ public struct LineHighlighter { return Highlight(line: offset + 1) } - return Result(file: new, highlights: highlights) + let deleteHighlights = diff.removals.compactMap { removal -> Highlight? in + guard case .remove(let offset, _, _) = removal else { return nil } + // Use 1-based indexing for line numbers. + // TODO: Collect intra-line diffs. + return Highlight(line: offset + 1) + } + + return Result(file: new, highlights: highlights, deleteHighlights: deleteHighlights) } /// Returns the line highlights between two ``Code`` elements. private func lineHighlights(old: Code?, new: Code) -> Result { if let previousFileOverride = new.previousFileReference { guard !new.shouldResetDiff else { - return Result(file: new.fileReference, highlights: []) + return Result(file: new.fileReference, highlights: [], deleteHighlights: []) } return lineHighlights(old: previousFileOverride, new: new.fileReference) } @@ -149,7 +159,7 @@ public struct LineHighlighter { guard let old = old, old.fileName == new.fileName, !new.shouldResetDiff else { - return Result(file: new.fileReference, highlights: []) + return Result(file: new.fileReference, highlights: [], deleteHighlights: []) } return lineHighlights(old: old.fileReference, new: new.fileReference)