Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete highlights #589

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -54,14 +57,16 @@ 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
self.fileType = fileType
self.syntax = syntax
self.content = content
self.highlights = highlights
self.deleteHighlights = deleteHighlights
}

public init(from decoder: Decoder) throws {
Expand All @@ -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)) ?? []
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 14 additions & 4 deletions Sources/SwiftDocC/Model/Rendering/Tutorial/LineHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

/**
Expand Down Expand Up @@ -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)
Expand All @@ -134,22 +137,29 @@ 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: cleanup the comment (it's the same as above). Perhaps extract that little block into a func if it's reused?

// 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)
}

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)
Expand Down