Commits

Jens Alfke  committed cc411a4

Added color highlighting of diffs.

  • Participants
  • Parent commits 050f77b

Comments (0)

Files changed (4)

File Murky.xcodeproj/project.pbxproj

 		27AA81C70FADF83000D4FCBB /* MYUtilities_Debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27AA81C50FADF83000D4FCBB /* MYUtilities_Debug.xcconfig */; };
 		27AA81C80FADF83000D4FCBB /* MYUtilities_Release.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 27AA81C60FADF83000D4FCBB /* MYUtilities_Release.xcconfig */; };
 		27B042D40CA9A1390090DC04 /* RevisionGraphColumn.m in Sources */ = {isa = PBXBuildFile; fileRef = 27B042D30CA9A1390090DC04 /* RevisionGraphColumn.m */; };
+		27C1C936104EF9F400781C99 /* SourceHighlighting.m in Sources */ = {isa = PBXBuildFile; fileRef = 27C1C935104EF9F400781C99 /* SourceHighlighting.m */; };
 		27C656880FAE01A200CFB909 /* MYErrorUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 27C656850FAE01A200CFB909 /* MYErrorUtils.m */; };
 		27C656890FAE01A200CFB909 /* MYTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 27C656870FAE01A200CFB909 /* MYTask.m */; };
 		27C656B90FAE046B00CFB909 /* MYDirectoryWatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = 27C656B70FAE046B00CFB909 /* MYDirectoryWatcher.m */; };
 		27AA81C60FADF83000D4FCBB /* MYUtilities_Release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = MYUtilities_Release.xcconfig; sourceTree = "<group>"; };
 		27B042D20CA9A1390090DC04 /* RevisionGraphColumn.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RevisionGraphColumn.h; sourceTree = "<group>"; };
 		27B042D30CA9A1390090DC04 /* RevisionGraphColumn.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RevisionGraphColumn.m; sourceTree = "<group>"; };
+		27C1C934104EF9F400781C99 /* SourceHighlighting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SourceHighlighting.h; sourceTree = "<group>"; };
+		27C1C935104EF9F400781C99 /* SourceHighlighting.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SourceHighlighting.m; sourceTree = "<group>"; };
 		27C656840FAE01A200CFB909 /* MYErrorUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MYErrorUtils.h; sourceTree = "<group>"; };
 		27C656850FAE01A200CFB909 /* MYErrorUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MYErrorUtils.m; sourceTree = "<group>"; };
 		27C656860FAE01A200CFB909 /* MYTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MYTask.h; sourceTree = "<group>"; };
 				277C51C00C56709800E60690 /* ImageAndTextCell.h */,
 				277C51C10C56709800E60690 /* ImageAndTextCell.m */,
 				73CA824710135EC90081F0D8 /* Terminal.h */,
+				27C1C934104EF9F400781C99 /* SourceHighlighting.h */,
+				27C1C935104EF9F400781C99 /* SourceHighlighting.m */,
 			);
 			name = Support;
 			path = Source;
 				27C656890FAE01A200CFB909 /* MYTask.m in Sources */,
 				27C656B90FAE046B00CFB909 /* MYDirectoryWatcher.m in Sources */,
 				27E772930FB09EA7006504EF /* MYWindowUtils.m in Sources */,
+				27C1C936104EF9F400781C99 /* SourceHighlighting.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

File Source/RepoController.m

 #import "HgUncommittedRevision.h"
 #import "HgTempDir.h"
 #import "RevisionGraphColumn.h"
+#import "SourceHighlighting.h"
 #import "MYTask.h"
 
 
 }
 
 
-static NSAttributedString* attributedStringForSourceCode( NSString *code ) {
-    if (!code) return nil;
-    NSFont *font = [NSFont fontWithName: @"Courier" size: 10.0f];
-    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
-    paraStyle.lineBreakMode = NSLineBreakByClipping;
-    NSDictionary *attrs = $dict({NSFontAttributeName, font},
-                                {NSParagraphStyleAttributeName, paraStyle});
-    return [[NSAttributedString alloc] initWithString: code
-                                           attributes: attrs];
-}
-    
-    
 - (void) _updateInfoView
 {
     NSAttributedString *text = nil;
                 } else {
                     str = error.localizedRecoverySuggestion;
                 }
-                text = attributedStringForSourceCode(str);
+                text = AttributedStringForSourceCode(str);
             }
             break;
         case kInfoDiffSegment:
                 NSString *str = [revision diffFile: file withRevision: revision.parent error: &error];
                 if (!str)
                     str = error.localizedRecoverySuggestion;
-                text = attributedStringForSourceCode(str);
+                NSMutableAttributedString *mtext = AttributedStringForSourceCode(str);
+                HighlightDiffs(mtext);
+                text = mtext;
             }
             break;
     }

File Source/SourceHighlighting.h

+//
+//  SourceHighlighting.h
+//  Murky
+//
+//  Created by Jens Alfke on 9/2/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+NSMutableAttributedString* AttributedStringForSourceCode (NSString *code);
+
+void HighlightDiffs (NSMutableAttributedString* text);

File Source/SourceHighlighting.m

+//
+//  SourceHighlighting.m
+//  Murky
+//
+//  Created by Jens Alfke on 9/2/09.
+//  Copyright 2009 Jens Alfke. All rights reserved.
+//
+
+#import "SourceHighlighting.h"
+
+
+NSMutableAttributedString* AttributedStringForSourceCode (NSString *code) {
+    if (!code) return nil;
+    NSFont *font = [NSFont fontWithName: @"Courier" size: 10.0f];
+    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
+    paraStyle.lineBreakMode = NSLineBreakByClipping;
+    NSDictionary *attrs = $dict({NSFontAttributeName, font},
+                                {NSParagraphStyleAttributeName, paraStyle});
+    return [[NSMutableAttributedString alloc] initWithString: code
+                                                  attributes: attrs];
+}
+
+
+void HighlightDiffs (NSMutableAttributedString* text) {
+    static NSDictionary *added, *deleted, *section;
+    if (!added)
+        added = $dict({NSForegroundColorAttributeName, 
+            [NSColor colorWithCalibratedHue: 150/360.0f saturation: 1.0f
+                                 brightness: 0.5f alpha: 1.0f]},
+                      {NSBackgroundColorAttributeName, 
+                          [NSColor colorWithCalibratedHue: 150/360.0f saturation: 0.05f
+                                               brightness: 1.0f alpha: 1.0f]} );
+    if (!deleted)
+        deleted = $dict({NSForegroundColorAttributeName, 
+            [NSColor colorWithCalibratedHue: 0.0f saturation: 0.6f
+                                 brightness: 0.8f alpha: 1.0f]},
+                        {NSBackgroundColorAttributeName, 
+                            [NSColor colorWithCalibratedHue: 0.0f saturation: 0.05f
+                                                 brightness: 1.0f alpha: 1.0f]} );
+    if (!section)
+        section = $dict({NSForegroundColorAttributeName, [NSColor whiteColor]},
+                        {NSBackgroundColorAttributeName, [NSColor darkGrayColor]} );
+    
+    [text addAttribute: NSForegroundColorAttributeName
+                 value: [NSColor grayColor] 
+                 range: NSMakeRange(0,text.length)];
+    NSUInteger nextStart, firstLineStart=0;
+    for (NSUInteger start = 0,lineNo = 0; start < text.length; start=nextStart,lineNo++) {
+        NSUInteger end;
+        [text.string getLineStart: &start end: &nextStart contentsEnd: &end 
+                         forRange: NSMakeRange(start,1)];
+        NSDictionary *attrs;
+        switch ([text.string characterAtIndex: start]) {
+            case '+':   attrs = added; break;
+            case '-':   attrs = deleted; break;
+            case '@':
+                attrs = section; 
+                if (firstLineStart == 0)
+                    firstLineStart = start;
+                break;
+            default:
+                attrs = nil; break;
+        }
+        if (attrs)
+            [text addAttributes: attrs range: NSMakeRange(start,nextStart-start)];
+    }
+    [text deleteCharactersInRange: NSMakeRange(0,firstLineStart)];
+}