Skip to content

Commit

Permalink
Merge pull request #478 from scholtzan/diff-annotations
Browse files Browse the repository at this point in the history
Diff annotations
  • Loading branch information
nangtrongvuon authored Dec 8, 2019
2 parents e12dff6 + 30ae69c commit 89b8739
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Sources/XiEditor/Annotation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ import Foundation
enum AnnotationType: String {
case selection
case find
case added
case deleted
case modified
}

extension AnnotationType {
static let all = [AnnotationType.selection, AnnotationType.find]
static let all = [AnnotationType.selection,
AnnotationType.find,
AnnotationType.added,
AnnotationType.deleted,
AnnotationType.modified]
}

/// Represents an annotation (eg. selection, find highlight).
Expand Down
1 change: 0 additions & 1 deletion Sources/XiEditor/Core/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct UpdateParams {
}

self.annotations = annotations

self.ops = updates
self.pristine = update["pristine"] as? Bool ?? false
}
Expand Down
32 changes: 32 additions & 0 deletions Sources/XiEditor/EditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func colorToArgb(_ color: NSColor) -> UInt32 {
final class EditView: NSView, NSTextInputClient, TextPlaneDelegate {
var lastRevisionRendered = 0
var gutterXPad: CGFloat = 8
var gutterAnnotationWidth = 3
var deletedAnnotationHeight = 3
var gutterCache: GutterCache?

weak var dataSource: EditViewDataSource!
Expand Down Expand Up @@ -447,6 +449,12 @@ final class EditView: NSView, NSTextInputClient, TextPlaneDelegate {
case AnnotationType.find:
let queryId = annotation.payload?["id"] as! Int
return highlightsArgb[queryId % highlightsArgb.count]
case AnnotationType.added:
return colorToArgb(dataSource.theme.gutterAdded!)
case AnnotationType.deleted:
return colorToArgb(dataSource.theme.gutterDeleted!)
case AnnotationType.modified:
return colorToArgb(dataSource.theme.gutterModified!)
}
}

Expand Down Expand Up @@ -594,6 +602,8 @@ final class EditView: NSView, NSTextInputClient, TextPlaneDelegate {
}
}

let gutterAnnotations = [AnnotationType.added, AnnotationType.deleted, AnnotationType.modified]

// gutter drawing
// Note: drawing the gutter background after the text effectively clips the text. This
// is a bit of a hack, and some optimization might be possible with real clipping
Expand All @@ -611,6 +621,28 @@ final class EditView: NSView, NSTextInputClient, TextPlaneDelegate {

let x = dataSource.gutterWidth - (gutterXPad + CGFloat(gutterTL.width))
let y0 = yOff + dataSource.textMetrics.ascent + linespace * CGFloat(lineIx)

for annotationType in gutterAnnotations {
var lineAnnotations = annotationsForLines[lineIx]![annotationType]!
while let lineAnnotation = lineAnnotations.next() {
let color = annotationColor(for: lineAnnotation.annotation)

if annotationType == AnnotationType.deleted {
renderer.drawSolidRect(x: GLfloat(dataSource.gutterWidth - CGFloat(gutterAnnotationWidth)),
y: GLfloat(yOff + dataSource.textMetrics.ascent + linespace * CGFloat(lineIx - 1)),
width: GLfloat(gutterAnnotationWidth),
height: GLfloat(deletedAnnotationHeight),
argb: color)
} else {
renderer.drawSolidRect(x: GLfloat(dataSource.gutterWidth - CGFloat(gutterAnnotationWidth)),
y: GLfloat(yOff + linespace * CGFloat(lineIx)),
width: GLfloat(gutterAnnotationWidth),
height: GLfloat(linespace),
argb: color)
}
}
}

renderer.drawLine(line: gutterTL, x0: GLfloat(x), y0: GLfloat(y0))
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/XiEditor/LineCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ fileprivate class LineCacheState<T>: UnfairLock {
annotations = AnnotationStore(from: params.annotations)

let inval = InvalSet()

if params.ops.isEmpty {
// do not invalidate lines if only there are no update operations, e.g. when only updating annotations
return inval
}

let oldHeight = height
var newInvalidBefore = 0
var newLines: [Line<T>?] = []
Expand Down Expand Up @@ -210,6 +210,7 @@ fileprivate class LineCacheState<T>: UnfairLock {
if height < oldHeight {
inval.addRange(start: height, end: oldHeight)
}

return inval
}

Expand Down
18 changes: 15 additions & 3 deletions Sources/XiEditor/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct Theme {
/// Only used when the `highlight_line` setting is set to `true`.
let lineHighlight: NSColor?


/// Background color of regions matching the current search.
let findHighlights: [NSColor]?
/// Background color of regions matching the current search.
Expand All @@ -55,6 +54,13 @@ struct Theme {

/// The color of the shadow used when a text area can be horizontally scrolled.
let shadow: NSColor?

/// Color for added lines in gutter.
let gutterAdded: NSColor?
/// Color for deleted lines in gutter.
let gutterDeleted: NSColor?
/// Color for modified lines in gutter.
let gutterModified: NSColor?
}

extension Theme {
Expand All @@ -72,7 +78,10 @@ extension Theme {
selectionBorder: nil,
inactiveSelection: NSColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0),
inactiveSelectionForeground: .selectedTextColor,
shadow: nil
shadow: nil,
gutterAdded: NSColor(red: 0.2, green: 0.8, blue: 0.1, alpha: 1.0),
gutterDeleted: NSColor(red: 0.8, green: 0.1, blue: 0.2, alpha: 1.0),
gutterModified: NSColor(red: 0.8, green: 0.7, blue: 0.1, alpha: 1.0)
)
}

Expand Down Expand Up @@ -109,7 +118,10 @@ extension Theme {
selectionBorder: selection_border ?? defaults.selectionBorder,
inactiveSelection: inactive_selection ?? defaults.inactiveSelection,
inactiveSelectionForeground: inactive_selection_foreground ?? defaults.inactiveSelectionForeground,
shadow: shadow ?? defaults.shadow)
shadow: shadow ?? defaults.shadow,
gutterAdded: defaults.gutterAdded,
gutterDeleted: defaults.gutterDeleted,
gutterModified: defaults.gutterModified)
}

/// Helper function to generate highlight colors for multiple search queries. This is required because custom fields cannot be retrieved from themes. Therefore, it is not possible to define multiple highlight colors.
Expand Down

0 comments on commit 89b8739

Please sign in to comment.