Skip to content

Commit 56ae853

Browse files
authored
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): <details> ![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) </details>
1 parent 377e013 commit 56ae853

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

modules/templates/helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ func NewFuncMap() template.FuncMap {
179179

180180
// -----------------------------------------------------------------
181181
// misc
182-
"DiffLineTypeToStr": DiffLineTypeToStr,
183182
"ShortSha": base.ShortSha,
184183
"ActionContent2Commits": ActionContent2Commits,
185184
"IsMultilineCommitMessage": IsMultilineCommitMessage,

modules/templates/util_misc.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,6 @@ func ActionContent2Commits(act Actioner) *repository.PushCommits {
122122
return push
123123
}
124124

125-
// DiffLineTypeToStr returns diff line type name
126-
func DiffLineTypeToStr(diffType int) string {
127-
switch diffType {
128-
case 2:
129-
return "add"
130-
case 3:
131-
return "del"
132-
case 4:
133-
return "tag"
134-
}
135-
return "same"
136-
}
137-
138125
// MigrationIcon returns a SVG name matching the service an issue/comment was migrated from
139126
func MigrationIcon(hostname string) string {
140127
switch hostname {

services/gitdiff/gitdiff.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ func (d *DiffLine) GetType() int {
104104
return int(d.Type)
105105
}
106106

107+
// GetHTMLDiffLineType returns the diff line type name for HTML
108+
func (d *DiffLine) GetHTMLDiffLineType() string {
109+
switch d.Type {
110+
case DiffLineAdd:
111+
return "add"
112+
case DiffLineDel:
113+
return "del"
114+
case DiffLineSection:
115+
return "tag"
116+
}
117+
return "same"
118+
}
119+
107120
// CanComment returns whether a line can get commented
108121
func (d *DiffLine) CanComment() bool {
109122
return len(d.Comments) == 0 && d.Type != DiffLineSection

templates/repo/diff/blob_excerpt.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{if $.IsSplitStyle}}
22
{{range $k, $line := $.section.Lines}}
3-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
3+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}">
44
{{if eq .GetType 4}}
55
<td class="lines-num lines-num-old" data-line-num="{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}">
66
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
@@ -44,7 +44,7 @@
4444
{{end}}
4545
{{else}}
4646
{{range $k, $line := $.section.Lines}}
47-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}">
47+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}">
4848
{{if eq .GetType 4}}
4949
<td colspan="2" class="lines-num">
5050
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}

templates/repo/diff/section_split.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{range $k, $line := $section.Lines}}
1414
{{$hasmatch := ne $line.Match -1}}
1515
{{if or (ne .GetType 2) (not $hasmatch)}}
16-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
16+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
1717
{{if eq .GetType 4}}
1818
<td class="lines-num lines-num-old">
1919
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}}
@@ -107,7 +107,7 @@
107107
{{if and (eq .GetType 3) $hasmatch}}
108108
{{$match := index $section.Lines $line.Match}}
109109
{{if or (gt (len $line.Comments) 0) (gt (len $match.Comments) 0)}}
110-
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
110+
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
111111
<td class="add-comment-left" colspan="4">
112112
{{if gt (len $line.Comments) 0}}
113113
{{if eq $line.GetCommentSide "previous"}}
@@ -133,7 +133,7 @@
133133
</tr>
134134
{{end}}
135135
{{else if gt (len $line.Comments) 0}}
136-
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
136+
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
137137
<td class="add-comment-left" colspan="4">
138138
{{if gt (len $line.Comments) 0}}
139139
{{if eq $line.GetCommentSide "previous"}}

templates/repo/diff/section_unified.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</colgroup>
99
{{range $j, $section := $file.Sections}}
1010
{{range $k, $line := $section.Lines}}
11-
<tr class="{{DiffLineTypeToStr .GetType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{DiffLineTypeToStr .GetType}}">
11+
<tr class="{{.GetHTMLDiffLineType}}-code nl-{{$k}} ol-{{$k}}" data-line-type="{{.GetHTMLDiffLineType}}">
1212
{{if eq .GetType 4}}
1313
{{if $.root.AfterCommitID}}
1414
<td colspan="2" class="lines-num">
@@ -55,7 +55,7 @@
5555
{{end}}
5656
</tr>
5757
{{if gt (len $line.Comments) 0}}
58-
<tr class="add-comment" data-line-type="{{DiffLineTypeToStr .GetType}}">
58+
<tr class="add-comment" data-line-type="{{.GetHTMLDiffLineType}}">
5959
<td class="add-comment-left add-comment-right" colspan="5">
6060
{{template "repo/diff/conversation" dict "." $.root "comments" $line.Comments}}
6161
</td>

0 commit comments

Comments
 (0)