Tools__TextMergeInfo.st
branchjv
changeset 12218 8b88c30fb1e7
parent 12204 ba9ffe0fd036
child 12225 60dfd3fa018d
--- a/Tools__TextMergeInfo.st	Tue Apr 03 18:37:50 2012 +0100
+++ b/Tools__TextMergeInfo.st	Wed Apr 04 01:09:32 2012 +0100
@@ -43,6 +43,22 @@
 "
 ! !
 
+!TextMergeInfo methodsFor:'* uncategorized *'!
+
+conflictLineText
+
+    ^ '<conflict>' asText allBold colorizeAllWith: Color red.
+
+    "Created: / 03-04-2012 / 23:26:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+noSourceLineText
+
+    ^'<no source line>' asText colorizeAllWith: Color red.
+
+    "Created: / 03-04-2012 / 23:26:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !TextMergeInfo methodsFor:'accessing'!
 
 list
@@ -54,7 +70,13 @@
 !
 
 text
-    ^(list reject:[:l|l isNil]) asString
+    ^String  streamContents:[:s|
+        1 to: list size do:[:i|
+            (listInfos at: i) offset ~~ -1 ifTrue:[
+                s nextPutLine: (list at: i)
+            ]
+        ].
+    ].
 
     "Created: / 19-03-2012 / 14:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
@@ -63,7 +85,7 @@
 
 text1: text1 text2: text2 text3: text3
 
-    | t1c t2c  t3c merges lnr |
+    | t1c t2c  t3c diff3 diffs merges resolution lnr |
 
     list := StringCollection new.
     listInfos := OrderedCollection new.
@@ -73,24 +95,40 @@
     t3c := (text3 ? #()) asStringCollection.
 
 
-    merges := Diff3 new
+    diff3 := Diff3 new
                     file0: t1c; "/Base version
                     file1: t2c; "/A
-                    file2: t3c; "/B
-                    merge.
+                    file2: t3c. "/B\
+    diffs := diff3 diffIndices.
+    merges := diff3 mergeIndices.
+
     lnr := 1.
-    merges do:[:merge|
-        merge key == #ok ifTrue:[
-            merge value do:[:line|
-                list add: line.
-                listInfos add: (LineInfo line: lnr resolution: #Merged ).
+    diffs with: merges do:[:diff :merge|
+        | lines |
+        diff isChunk ifTrue:[
+            lines := merge extractFromDiff: diff3.
+            1 to: diff length do:[:i|
+                list add: (lines at: i).
+                listInfos add: (LineInfo line: lnr resolution: #NoConflict conflict: diff offset: i ).
                 lnr := lnr + 1.
             ].
         ].
-        merge key == #conflict ifTrue:[
-            1 to: merge value length do:[:i|
-                list add:nil. "/no resolution now"
-                listInfos add: (LineInfo line: lnr resolution: #Conflict conflict: merge value offset: i).
+        diff isConflict ifTrue:[
+            1 to: diff length do:[:i|
+                merge isConflict ifTrue:[
+                    list add: self conflictLineText.
+                    listInfos add: (LineInfo line: lnr resolution: #Conflict conflict: diff offset: i).
+                ] ifFalse:[
+                    lines := merge extractFromDiff: diff3.
+                    resolution := merge extractResolution.
+                    i <= lines size ifTrue:[
+                        list add: (lines at: i).
+                        listInfos add: (LineInfo line: lnr resolution: resolution conflict: diff offset: i).
+                    ] ifFalse:[
+                        list add: self noSourceLineText.
+                        listInfos add: (LineInfo line: lnr resolution: resolution conflict: diff offset: -1).
+                    ].
+                ].
                 lnr := lnr + 1.
             ].
         ].
@@ -110,11 +148,39 @@
         (1 to: list size) collect:[:lineNr|
             (LineInfo line: lineNr resolution: #MergedUsingA )
         ].
-    self changed:#resulution
+    self changed:#resolution
 
     "Created: / 21-03-2012 / 12:03:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+mergeUsingA: textA interval: interval
+    "Given textA and section interval, merges the section
+     using textA."
+
+    | listA offset |
+    listA := textA asStringCollection.
+    offset := 0.
+    interval do:[:lineNr|
+        | info chunk line |
+        info := listInfos at: lineNr.
+        chunk := info conflict left.
+        offset < chunk length ifTrue:[
+            line := listA at: chunk offset + offset.            
+            info offset: offset + 1.
+        ] ifFalse:[
+            line := self noSourceLineText.
+            info offset: -1
+        ].
+        list at: lineNr put: line.
+        info resolution: #MergedUsingA.
+        offset := offset + 1.
+    ].
+
+    self changed:#resolution
+
+    "Created: / 04-04-2012 / 00:51:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 mergeUsingB: textB
 
     list := (textB ? #())  asStringCollection.
@@ -127,6 +193,34 @@
     "Created: / 21-03-2012 / 12:04:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+mergeUsingB: textA interval: interval
+    "Given textA and section interval, merges the section
+     using textA."
+
+    | listA offset |
+    listA := textA asStringCollection.
+    offset := 0.
+    interval do:[:lineNr|
+        | info chunk line |
+        info := listInfos at: lineNr.
+        chunk := info conflict right.
+        offset < chunk length ifTrue:[
+            line := listA at: chunk offset + offset.            
+            info offset: offset + 1.
+        ] ifFalse:[
+            line := self noSourceLineText.
+            info offset: -1
+        ].
+        list at: lineNr put: line.
+        info resolution: #MergedUsingB.
+        offset := offset + 1.
+    ].
+
+    self changed:#resolution
+
+    "Created: / 04-04-2012 / 01:01:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 mergeUsingBase: textBase
 
     list := (textBase ? #()) asStringCollection.
@@ -137,6 +231,34 @@
     self changed:#resulution
 
     "Created: / 21-03-2012 / 12:07:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+mergeUsingBase: textA interval: interval
+    "Given textA and section interval, merges the section
+     using textA."
+
+    | listA offset |
+    listA := textA asStringCollection.
+    offset := 0.
+    interval do:[:lineNr|
+        | info chunk line |
+        info := listInfos at: lineNr.
+        chunk := info conflict original.
+        offset < chunk length ifTrue:[
+            line := listA at: chunk offset + offset.            
+            info offset: offset + 1.
+        ] ifFalse:[
+            line := self noSourceLineText.
+            info offset: -1
+        ].
+        list at: lineNr put: line.
+        info resolution: #MergedUsingBase.
+        offset := offset + 1.
+    ].
+
+    self changed:#resolution
+
+    "Created: / 04-04-2012 / 01:01:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !TextMergeInfo methodsFor:'testing'!
@@ -175,14 +297,28 @@
 
 color
 
+    | color |
+
     self isMerged ifTrue:[ 
-        self isMergedUsingA ifTrue:[ ^Tools::TextDiff3Tool colorA].
-        self isMergedUsingB ifTrue:[ ^Tools::TextDiff3Tool colorB].
-        self isMergedUsingBase ifTrue:[ ^Tools::TextDiff3Tool colorBase].
-        ^ Tools::TextDiff3Tool colorMerged 
+        self isMergedUsingA ifTrue:[ 
+            color := Tools::TextDiff3Tool colorA
+        ] ifFalse:[self isMergedUsingB ifTrue:[ 
+                color := Tools::TextDiff3Tool colorB
+        ] ifFalse:[self isMergedUsingBase ifTrue:[ 
+            color := Tools::TextDiff3Tool colorBase
+        ] ifFalse:[
+            color :=  Tools::TextDiff3Tool colorMerged
+        ]]].
+        offset == -1 ifTrue:[
+            color := color lighter.
+        ].
+    ] ifFalse:[
+        self isConflict ifTrue:[ 
+            color :=  Tools::TextDiff3Tool colorConflict 
+        ].
     ].
-    self isConflict ifTrue:[ ^ Tools::TextDiff3Tool colorConflict ].
-    ^nil
+
+    ^color
 
     "Created: / 19-03-2012 / 15:05:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
@@ -214,6 +350,10 @@
     ^ offset
 !
 
+offset:something
+    offset := something.
+!
+
 resolution
     ^ resolution
 !
@@ -282,5 +422,5 @@
 !TextMergeInfo class methodsFor:'documentation'!
 
 version_SVN
-    ^ '$Id: Tools__TextMergeInfo.st 7951 2012-03-21 14:05:42Z vranyj1 $'
+    ^ '$Id: Tools__TextMergeInfo.st 7965 2012-04-04 00:09:32Z vranyj1 $'
 ! !