Skip to content

Commit f0e2f97

Browse files
committed
remove unnecessary code, add more comments
1 parent ca6a537 commit f0e2f97

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

services/gitdiff/gitdiff.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func diffToHTML(hcd *HighlightCodeDiff, diffs []diffmatchpatch.Diff, lineType Di
194194
buf := bytes.NewBuffer(nil)
195195
if hcd != nil {
196196
for _, tag := range hcd.lineWrapperTags {
197-
buf.WriteString(tag)
197+
buf.WriteString(tag) // restore the line wrapper tags <span class="line"> and <span class="cl">
198198
}
199199
}
200200
for _, diff := range diffs {
@@ -345,40 +345,37 @@ func (hcd *HighlightCodeDiff) convertToPlaceholders(highlightCode string) string
345345
}
346346

347347
var tagInMap string
348-
if tag[1] == '/' {
349-
// closed tag
348+
if tag[1] == '/' { // for closed tag
350349
if len(tagStack) == 0 {
351-
break // error, no open tag but see close tag
350+
break // invalid diff result, no open tag but see close tag
352351
}
353352
// make sure the closed tag in map is related to the open tag, to make the diff algorithm can match the open/closed tags
354-
// the closed tag should be "</span><!-- <span the-open> -->" for "<span the-open>"
353+
// the closed tag will be recorded in the map by key "</span><!-- <span the-open> -->" for "<span the-open>"
355354
tagInMap = tag + "<!-- " + tagStack[len(tagStack)-1] + "-->"
356355
tagStack = tagStack[:len(tagStack)-1]
357-
} else {
356+
} else { // for open tag
358357
tagInMap = tag
359358
tagStack = append(tagStack, tag)
360359
}
360+
361+
// remember the placeholder and tag in the map
361362
placeholder, ok := hcd.tagPlaceholderMap[tagInMap]
362363
if !ok {
363364
placeholder = hcd.nextPlaceholder()
364365
hcd.tagPlaceholderMap[tagInMap] = placeholder
365366
hcd.placeholderTagMap[placeholder] = tagInMap
366367
}
367-
res.WriteRune(placeholder)
368+
369+
res.WriteRune(placeholder) // use the placeholder to replace the tag
368370
}
369371
res.WriteString(s)
370372
return res.String()
371373
}
372374

373-
func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmatchpatch.Diff) (activeTag string) {
375+
func (hcd *HighlightCodeDiff) recoverOneDiff(diff *diffmatchpatch.Diff) {
374376
sb := strings.Builder{}
375377
var tagStack []string
376378

377-
if lastActiveTag != "" {
378-
tagStack = append(tagStack, lastActiveTag)
379-
sb.WriteString(lastActiveTag)
380-
}
381-
382379
for _, r := range diff.Text {
383380
tag, ok := hcd.placeholderTagMap[r]
384381
if !ok {
@@ -389,7 +386,7 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
389386
if tag[1] == '/' {
390387
tagToRecover = tag[:strings.IndexByte(tag, '>')+1]
391388
if len(tagStack) == 0 {
392-
continue // if no open tag, skip the closed tag
389+
continue // if no open tag yet, skip the closed tag
393390
}
394391
tagStack = tagStack[:len(tagStack)-1]
395392
} else {
@@ -400,8 +397,6 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
400397
}
401398

402399
if len(tagStack) > 0 {
403-
// at the moment, only one-level (non-nested) tag is supported, aka only the last level is used as active tag for next diff
404-
tagStack = tagStack[:len(tagStack)-1]
405400
// close all open tags
406401
for i := len(tagStack) - 1; i >= 0; i-- {
407402
tagToClose := tagStack[i]
@@ -416,13 +411,11 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
416411
}
417412

418413
diff.Text = sb.String()
419-
return activeTag
420414
}
421415

422416
func (hcd *HighlightCodeDiff) recoverFromPlaceholders(diffs []diffmatchpatch.Diff) {
423-
var lastActiveTag string
424417
for i := range diffs {
425-
lastActiveTag = hcd.recoverOneDiff(lastActiveTag, &diffs[i])
418+
hcd.recoverOneDiff(&diffs[i])
426419
}
427420
}
428421

0 commit comments

Comments
 (0)