summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-02-07 11:49:11 -0500
committerUnknwon <u@gogs.io>2016-02-07 11:49:11 -0500
commitf15a2f9b25bbec130915fec5b33c7e49dc7cf57d (patch)
tree1ba3aa079c4f931a6fef87693cf83e731a323bcc /models
parent3b102a71a2cef4a3cdb748bbc6e0499fdec5e3b4 (diff)
parent2bfb8bb5fdebb4ae02c83a271e8eb24bc68afec1 (diff)
downloadgitea-f15a2f9b25bbec130915fec5b33c7e49dc7cf57d.tar.gz
gitea-f15a2f9b25bbec130915fec5b33c7e49dc7cf57d.zip
Merge pull request #2528 from andreynering/diff-sintax-highlight-733
Enable syntax highlighting on diff view
Diffstat (limited to 'models')
-rw-r--r--models/git_diff.go9
-rw-r--r--models/git_diff_test.go100
2 files changed, 59 insertions, 50 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index e8bfe61027..9796ef59f8 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -26,6 +26,7 @@ import (
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/process"
+ "github.com/gogits/gogs/modules/template/highlight"
)
type DiffLineType uint8
@@ -160,12 +161,20 @@ type DiffFile struct {
IsBin bool
IsRenamed bool
Sections []*DiffSection
+ HighlightClass string
}
func (diffFile *DiffFile) GetType() int {
return int(diffFile.Type)
}
+func (diffFile *DiffFile) GetHighlightClass() string {
+ if diffFile.HighlightClass == "" {
+ diffFile.HighlightClass = highlight.FileNameToHighlightClass(diffFile.Name)
+ }
+ return diffFile.HighlightClass
+}
+
type Diff struct {
TotalAddition, TotalDeletion int
Files []*DiffFile
diff --git a/models/git_diff_test.go b/models/git_diff_test.go
index 4084814ec8..3a1312ca56 100644
--- a/models/git_diff_test.go
+++ b/models/git_diff_test.go
@@ -1,70 +1,70 @@
package models
import (
- dmp "github.com/sergi/go-diff/diffmatchpatch"
- "html/template"
- "testing"
+ dmp "github.com/sergi/go-diff/diffmatchpatch"
+ "html/template"
+ "testing"
)
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
- if s1 != string(s2) {
- t.Errorf("%s should be equal %s", s2, s1)
- }
+ if s1 != string(s2) {
+ t.Errorf("%s should be equal %s", s2, s1)
+ }
}
func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
- if d1 != d2 {
- t.Errorf("%v should be equal %v", d1, d2)
- }
+ if d1 != d2 {
+ t.Errorf("%v should be equal %v", d1, d2)
+ }
}
func TestDiffToHTML(t *testing.T) {
- assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
- dmp.Diff{dmp.DiffEqual, "foo "},
- dmp.Diff{dmp.DiffInsert, "bar"},
- dmp.Diff{dmp.DiffDelete, " baz"},
- dmp.Diff{dmp.DiffEqual, " biz"},
- }, DIFF_LINE_ADD))
+ assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
+ dmp.Diff{dmp.DiffEqual, "foo "},
+ dmp.Diff{dmp.DiffInsert, "bar"},
+ dmp.Diff{dmp.DiffDelete, " baz"},
+ dmp.Diff{dmp.DiffEqual, " biz"},
+ }, DIFF_LINE_ADD))
- assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
- dmp.Diff{dmp.DiffEqual, "foo "},
- dmp.Diff{dmp.DiffDelete, "bar"},
- dmp.Diff{dmp.DiffInsert, " baz"},
- dmp.Diff{dmp.DiffEqual, " biz"},
- }, DIFF_LINE_DEL))
+ assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
+ dmp.Diff{dmp.DiffEqual, "foo "},
+ dmp.Diff{dmp.DiffDelete, "bar"},
+ dmp.Diff{dmp.DiffInsert, " baz"},
+ dmp.Diff{dmp.DiffEqual, " biz"},
+ }, DIFF_LINE_DEL))
}
// test if GetLine is return the correct lines
func TestGetLine(t *testing.T) {
- ds := DiffSection{Lines: []*DiffLine{
- &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN},
- &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN},
- &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN},
- &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL},
- &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN},
- &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL},
- &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL},
- &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL},
- &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL},
- &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD},
- &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN},
- &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN},
- }}
+ ds := DiffSection{Lines: []*DiffLine{
+ &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN},
+ &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN},
+ &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN},
+ &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL},
+ &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN},
+ &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL},
+ &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL},
+ &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL},
+ &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL},
+ &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD},
+ &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN},
+ &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN},
+ }}
- assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
- assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
+ assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
}