From 56ae853ca0f7255cc669d802b741922fd8670c3b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 7 May 2023 17:49:46 +0800 Subject: [PATCH] Simplify template helper functions (#24570) To avoid bloating the template helper functions, some functions could be provided by type methods. And the new code `data-line-type="{{.GetHTMLDiffLineType}}"` reads better than `data-line-type="{{DiffLineTypeToStr .GetType}}"` After the fix, screenshots (the same as before):
![image](https://user-images.githubusercontent.com/2114189/236657918-20ce01e0-1192-443e-aeb4-6b3fe1aa2102.png) ![image](https://user-images.githubusercontent.com/2114189/236657950-ee19727f-a1fc-4133-afc7-e5d1a8c1783f.png)
--- modules/templates/helper.go | 1 - modules/templates/util_misc.go | 13 ------------- services/gitdiff/gitdiff.go | 13 +++++++++++++ templates/repo/diff/blob_excerpt.tmpl | 4 ++-- templates/repo/diff/section_split.tmpl | 6 +++--- templates/repo/diff/section_unified.tmpl | 4 ++-- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 4abd94d46e..7460f99fea 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -179,7 +179,6 @@ func NewFuncMap() template.FuncMap { // ----------------------------------------------------------------- // misc - "DiffLineTypeToStr": DiffLineTypeToStr, "ShortSha": base.ShortSha, "ActionContent2Commits": ActionContent2Commits, "IsMultilineCommitMessage": IsMultilineCommitMessage, diff --git a/modules/templates/util_misc.go b/modules/templates/util_misc.go index 599a0942ce..d11251fcdf 100644 --- a/modules/templates/util_misc.go +++ b/modules/templates/util_misc.go @@ -122,19 +122,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits { return push } -// DiffLineTypeToStr returns diff line type name -func DiffLineTypeToStr(diffType int) string { - switch diffType { - case 2: - return "add" - case 3: - return "del" - case 4: - return "tag" - } - return "same" -} - // MigrationIcon returns a SVG name matching the service an issue/comment was migrated from func MigrationIcon(hostname string) string { switch hostname { diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index c50e8137ab..73474cf248 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -104,6 +104,19 @@ func (d *DiffLine) GetType() int { return int(d.Type) } +// GetHTMLDiffLineType returns the diff line type name for HTML +func (d *DiffLine) GetHTMLDiffLineType() string { + switch d.Type { + case DiffLineAdd: + return "add" + case DiffLineDel: + return "del" + case DiffLineSection: + return "tag" + } + return "same" +} + // CanComment returns whether a line can get commented func (d *DiffLine) CanComment() bool { return len(d.Comments) == 0 && d.Type != DiffLineSection diff --git a/templates/repo/diff/blob_excerpt.tmpl b/templates/repo/diff/blob_excerpt.tmpl index caa428dc28..7e11b33ba6 100644 --- a/templates/repo/diff/blob_excerpt.tmpl +++ b/templates/repo/diff/blob_excerpt.tmpl @@ -1,6 +1,6 @@ {{if $.IsSplitStyle}} {{range $k, $line := $.section.Lines}} - + {{if eq .GetType 4}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} @@ -44,7 +44,7 @@ {{end}} {{else}} {{range $k, $line := $.section.Lines}} - + {{if eq .GetType 4}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index e44c2a0f73..42aed85084 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -13,7 +13,7 @@ {{range $k, $line := $section.Lines}} {{$hasmatch := ne $line.Match -1}} {{if or (ne .GetType 2) (not $hasmatch)}} - + {{if eq .GetType 4}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} @@ -107,7 +107,7 @@ {{if and (eq .GetType 3) $hasmatch}} {{$match := index $section.Lines $line.Match}} {{if or (gt (len $line.Comments) 0) (gt (len $match.Comments) 0)}} - + {{if gt (len $line.Comments) 0}} {{if eq $line.GetCommentSide "previous"}} @@ -133,7 +133,7 @@ {{end}} {{else if gt (len $line.Comments) 0}} - + {{if gt (len $line.Comments) 0}} {{if eq $line.GetCommentSide "previous"}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index cdd2c26f81..8bf331f6db 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -8,7 +8,7 @@ {{range $j, $section := $file.Sections}} {{range $k, $line := $section.Lines}} - + {{if eq .GetType 4}} {{if $.root.AfterCommitID}} @@ -55,7 +55,7 @@ {{end}} {{if gt (len $line.Comments) 0}} - + {{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}}