From dbed39ba05fd498dad6cd3bf127c1f68056bf2ab Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 11 Aug 2016 21:07:09 -0300 Subject: [PATCH] On showing diff/file, use the tab_width specified on .editorconfig, if any (#3241) Closes #3182 --- modules/context/repo.go | 24 +++++++++++++ modules/template/template.go | 10 ++++++ public/css/gogs.css | 64 +++++++++++++++++++++++++++++++++++ public/less/_repository.less | 10 ++++++ routers/repo/commit.go | 7 ++++ routers/repo/pull.go | 14 ++++++++ routers/repo/view.go | 6 ++++ templates/repo/diff_box.tmpl | 2 +- templates/repo/view_file.tmpl | 2 +- 9 files changed, 137 insertions(+), 2 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index af1888148..2f6a5de7a 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -6,10 +6,12 @@ package context import ( "fmt" + "io/ioutil" "path" "strings" "github.com/Unknwon/com" + "gopkg.in/editorconfig/editorconfig-core-go.v1" "gopkg.in/macaron.v1" "github.com/gogits/git-module" @@ -69,6 +71,28 @@ func (r *Repository) HasAccess() bool { return r.AccessMode >= models.ACCESS_MODE_READ } +// GetEditorconfig returns the .editorconfig definition if found in the +// HEAD of the default repo branch. +func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) { + commit, err := r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch) + if err != nil { + return nil, err + } + treeEntry, err := commit.GetTreeEntryByPath(".editorconfig") + if err != nil { + return nil, err + } + reader, err := treeEntry.Blob().Data() + if err != nil { + return nil, err + } + data, err := ioutil.ReadAll(reader) + if err != nil { + return nil, err + } + return editorconfig.ParseBytes(data) +} + func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { // Non-fork repository will not return error in this method. if err := repo.GetBaseRepo(); err != nil { diff --git a/modules/template/template.go b/modules/template/template.go index 6e9b15b9b..1b5e0a11a 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -17,6 +17,7 @@ import ( "golang.org/x/net/html/charset" "golang.org/x/text/transform" + "gopkg.in/editorconfig/editorconfig-core-go.v1" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" @@ -109,6 +110,15 @@ func NewFuncMap() []template.FuncMap { mimeType := mime.TypeByExtension(filepath.Ext(filename)) return strings.HasPrefix(mimeType, "image/") }, + "TabSizeClass": func(ec *editorconfig.Editorconfig, filename string) string { + if ec != nil { + def := ec.GetDefinitionForFilename(filename) + if def.TabWidth > 0 { + return fmt.Sprintf("tab-size-%d", def.TabWidth) + } + } + return "tab-size-8" + }, }} } diff --git a/public/css/gogs.css b/public/css/gogs.css index c39c52ca2..b40ed0855 100644 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -2312,6 +2312,70 @@ footer .ui.language .menu { #delete-repo-modal .ui.message { width: 100%!important; } +.tab-size-1 { + tab-size: 1 !important; + -moz-tab-size: 1 !important; +} +.tab-size-2 { + tab-size: 2 !important; + -moz-tab-size: 2 !important; +} +.tab-size-3 { + tab-size: 3 !important; + -moz-tab-size: 3 !important; +} +.tab-size-4 { + tab-size: 4 !important; + -moz-tab-size: 4 !important; +} +.tab-size-5 { + tab-size: 5 !important; + -moz-tab-size: 5 !important; +} +.tab-size-6 { + tab-size: 6 !important; + -moz-tab-size: 6 !important; +} +.tab-size-7 { + tab-size: 7 !important; + -moz-tab-size: 7 !important; +} +.tab-size-8 { + tab-size: 8 !important; + -moz-tab-size: 8 !important; +} +.tab-size-9 { + tab-size: 9 !important; + -moz-tab-size: 9 !important; +} +.tab-size-10 { + tab-size: 10 !important; + -moz-tab-size: 10 !important; +} +.tab-size-11 { + tab-size: 11 !important; + -moz-tab-size: 11 !important; +} +.tab-size-12 { + tab-size: 12 !important; + -moz-tab-size: 12 !important; +} +.tab-size-13 { + tab-size: 13 !important; + -moz-tab-size: 13 !important; +} +.tab-size-14 { + tab-size: 14 !important; + -moz-tab-size: 14 !important; +} +.tab-size-15 { + tab-size: 15 !important; + -moz-tab-size: 15 !important; +} +.tab-size-16 { + tab-size: 16 !important; + -moz-tab-size: 16 !important; +} .organization { padding-top: 15px; padding-bottom: 80px; diff --git a/public/less/_repository.less b/public/less/_repository.less index 0fdc23df2..0eea2d778 100644 --- a/public/less/_repository.less +++ b/public/less/_repository.less @@ -1360,3 +1360,13 @@ width: 100%!important; } } + +// generate .tab-size-{i} from 1 to 16 +.generate-tab-size(16); +.generate-tab-size(@n, @i: 1) when (@i =< @n) { + .tab-size-@{i} { + tab-size: @i !important; + -moz-tab-size: @i !important; + } + .generate-tab-size(@n, (@i + 1)); +} diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 4eb37e6bd..c102ceae4 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -174,6 +174,13 @@ func Diff(ctx *context.Context) { } } + ec, err := ctx.Repo.GetEditorconfig() + if err != nil && !git.IsErrNotExist(err) { + ctx.Handle(500, "ErrGettingEditorconfig", err) + return + } + ctx.Data["Editorconfig"] = ec + ctx.Data["CommitID"] = commitID ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["Username"] = userName diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 8fb7ae183..77ee4bd34 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -371,6 +371,13 @@ func ViewPullFiles(ctx *context.Context) { return } + ec, err := ctx.Repo.GetEditorconfig() + if err != nil && !git.IsErrNotExist(err) { + ctx.Handle(500, "ErrGettingEditorconfig", err) + return + } + ctx.Data["Editorconfig"] = ec + headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name) ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["Username"] = pull.HeadUserName @@ -623,6 +630,13 @@ func CompareAndPullRequest(ctx *context.Context) { } } + ec, err := ctx.Repo.GetEditorconfig() + if err != nil && !git.IsErrNotExist(err) { + ctx.Handle(500, "ErrGettingEditorconfig", err) + return + } + ctx.Data["Editorconfig"] = ec + ctx.HTML(200, COMPARE_PULL) } diff --git a/routers/repo/view.go b/routers/repo/view.go index 9c495b701..e685597a5 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -225,6 +225,12 @@ func Home(ctx *context.Context) { ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName + ec, err := ctx.Repo.GetEditorconfig() + if err != nil && !git.IsErrNotExist(err) { + ctx.Handle(500, "ErrGettingEditorconfig", err) + return + } + ctx.Data["Editorconfig"] = ec var treenames []string paths := make([]string, 0) diff --git a/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl index 76a414d23..fefca9b60 100644 --- a/templates/repo/diff_box.tmpl +++ b/templates/repo/diff_box.tmpl @@ -53,7 +53,7 @@ {{else}} {{$highlightClass := $file.GetHighlightClass}} -
+

{{if $file.IsBin}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 0f42df492..76175334d 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -1,4 +1,4 @@ -
+

{{if .ReadmeExist}}