Skip to content

Commit 4fc4f6e

Browse files
authored
Refactor "editorconfig" (#26391)
There are 2 kinds of ".Editorconfig" in code, one is `JSON string` for the web edtior, another is `*editorconfig.Editorconfig` for the file rendering (used by `TabSizeClass`) This PR distinguish them with different names. And by the way, change the default tab size from 8 to 4, I think few people would like to use 8-size tabs nowadays.
1 parent 20f47bb commit 4fc4f6e

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

modules/templates/util_misc.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package templates
55

66
import (
77
"context"
8-
"fmt"
98
"html/template"
109
"mime"
1110
"path/filepath"
11+
"strconv"
1212
"strings"
1313
"time"
1414

@@ -174,23 +174,12 @@ func FilenameIsImage(filename string) bool {
174174
return strings.HasPrefix(mimeType, "image/")
175175
}
176176

177-
func TabSizeClass(ec any, filename string) string {
178-
var (
179-
value *editorconfig.Editorconfig
180-
ok bool
181-
)
177+
func TabSizeClass(ec *editorconfig.Editorconfig, filename string) string {
182178
if ec != nil {
183-
if value, ok = ec.(*editorconfig.Editorconfig); !ok || value == nil {
184-
return "tab-size-8"
185-
}
186-
def, err := value.GetDefinitionForFilename(filename)
187-
if err != nil {
188-
log.Error("tab size class: getting definition for filename: %v", err)
189-
return "tab-size-8"
190-
}
191-
if def.TabWidth > 0 {
192-
return fmt.Sprintf("tab-size-%d", def.TabWidth)
179+
def, err := ec.GetDefinitionForFilename(filename)
180+
if err == nil && def.TabWidth >= 1 && def.TabWidth <= 16 {
181+
return "tab-size-" + strconv.Itoa(def.TabWidth)
193182
}
194183
}
195-
return "tab-size-8"
184+
return "tab-size-4"
196185
}

routers/web/repo/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
191191
ctx.Data["last_commit"] = ctx.Repo.CommitID
192192
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
193193
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
194-
ctx.Data["Editorconfig"] = GetEditorConfig(ctx, treePath)
194+
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, treePath)
195195

196196
ctx.HTML(http.StatusOK, tplEditFile)
197197
}
@@ -242,7 +242,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
242242
ctx.Data["last_commit"] = ctx.Repo.CommitID
243243
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
244244
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
245-
ctx.Data["Editorconfig"] = GetEditorConfig(ctx, form.TreePath)
245+
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, form.TreePath)
246246

247247
if ctx.HasError() {
248248
ctx.HTML(http.StatusOK, tplEditFile)

templates/repo/editor/edit.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{range $i, $v := .TreeNames}}
1616
<div class="divider"> / </div>
1717
{{if eq $i $l}}
18-
<input id="file-name" value="{{$v}}" placeholder="{{$.locale.Tr "repo.editor.name_your_file"}}" data-editorconfig="{{$.Editorconfig}}" required autofocus>
18+
<input id="file-name" value="{{$v}}" placeholder="{{$.locale.Tr "repo.editor.name_your_file"}}" data-editorconfig="{{$.EditorconfigJson}}" required autofocus>
1919
<span data-tooltip-content="{{$.locale.Tr "repo.editor.filename_help"}}">{{svg "octicon-info"}}</span>
2020
{{else}}
2121
<span class="section"><a href="{{$.BranchLink}}/{{index $.TreePaths $i | PathEscapeSegments}}">{{$v}}</a></span>

0 commit comments

Comments
 (0)