mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
fix(ui): use tab width from .editorconfig when editing files (#11418)
Resolves #11411. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11418 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
parent
cbaf97b867
commit
efe52db86f
3 changed files with 18 additions and 6 deletions
|
|
@ -30,6 +30,8 @@ import (
|
||||||
"forgejo.org/services/context/upload"
|
"forgejo.org/services/context/upload"
|
||||||
"forgejo.org/services/forms"
|
"forgejo.org/services/forms"
|
||||||
files_service "forgejo.org/services/repository/files"
|
files_service "forgejo.org/services/repository/files"
|
||||||
|
|
||||||
|
"github.com/editorconfig/editorconfig-core-go/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -223,13 +225,22 @@ func editFile(ctx *context.Context, isNewFile bool) {
|
||||||
ctx.Data["last_commit"] = ctx.Repo.CommitID
|
ctx.Data["last_commit"] = ctx.Repo.CommitID
|
||||||
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
|
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
|
||||||
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
|
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
|
||||||
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, treePath)
|
ctx.Data["EditorconfigJson"] = GetEditorConfigJSON(ctx, treePath)
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplEditFile)
|
ctx.HTML(http.StatusOK, tplEditFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEditorConfig returns a editorconfig JSON string for given treePath or "null"
|
// GetEditorConfig returns a editorconfig object for given treePath or nil
|
||||||
func GetEditorConfig(ctx *context.Context, treePath string) string {
|
func GetEditorConfig(ctx *context.Context, treePath string) (ec *editorconfig.Editorconfig) {
|
||||||
|
ec, _, err := ctx.Repo.GetEditorconfig()
|
||||||
|
if err == nil {
|
||||||
|
return ec
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetEditorConfigJSON returns a editorconfig JSON string for given treePath or "null"
|
||||||
|
func GetEditorConfigJSON(ctx *context.Context, treePath string) string {
|
||||||
ec, _, err := ctx.Repo.GetEditorconfig()
|
ec, _, err := ctx.Repo.GetEditorconfig()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
def, err := ec.GetDefinitionForFilename(treePath)
|
def, err := ec.GetDefinitionForFilename(treePath)
|
||||||
|
|
@ -274,7 +285,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
|
||||||
ctx.Data["last_commit"] = ctx.Repo.CommitID
|
ctx.Data["last_commit"] = ctx.Repo.CommitID
|
||||||
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
|
ctx.Data["PreviewableExtensions"] = strings.Join(markup.PreviewableExtensions(), ",")
|
||||||
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
|
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
|
||||||
ctx.Data["EditorconfigJson"] = GetEditorConfig(ctx, form.TreePath)
|
ctx.Data["EditorconfigJson"] = GetEditorConfigJSON(ctx, form.TreePath)
|
||||||
|
|
||||||
if ctx.HasError() {
|
if ctx.HasError() {
|
||||||
ctx.HTML(http.StatusOK, tplEditFile)
|
ctx.HTML(http.StatusOK, tplEditFile)
|
||||||
|
|
@ -458,6 +469,7 @@ func DiffPreviewPost(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["File"] = diff.Files[0]
|
ctx.Data["File"] = diff.Files[0]
|
||||||
|
ctx.Data["Editorconfig"] = GetEditorConfig(ctx, treePath)
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplEditDiffPreview)
|
ctx.HTML(http.StatusOK, tplEditDiffPreview)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<div class="diff-file-box">
|
<div class="diff-file-box {{TabSizeClass $.Editorconfig .File.Name}}">
|
||||||
<div class="ui attached table segment">
|
<div class="ui attached table segment">
|
||||||
<div class="file-body file-code code-diff code-diff-unified unicode-escaped">
|
<div class="file-body file-code code-diff code-diff-unified unicode-escaped">
|
||||||
<table>
|
<table>
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ export async function createCodemirror(
|
||||||
codemirrorAutocomplete.autocompletion(),
|
codemirrorAutocomplete.autocompletion(),
|
||||||
codemirrorCommands.history(),
|
codemirrorCommands.history(),
|
||||||
tabSize.of(
|
tabSize.of(
|
||||||
codemirrorState.EditorState.tabSize.of(editorOpts.tabSize || 4),
|
codemirrorState.EditorState.tabSize.of(editorOpts.indentSize || 4),
|
||||||
),
|
),
|
||||||
wordWrap.of(
|
wordWrap.of(
|
||||||
editorOpts.wordWrap ? codemirrorView.EditorView.lineWrapping : [],
|
editorOpts.wordWrap ? codemirrorView.EditorView.lineWrapping : [],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue