From 9ac46fb983f31cfece76e2181a9fe73b71f02e2e Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Tue, 30 Aug 2016 20:18:40 -0300 Subject: [PATCH] Support Editorconfig on web editor --- .gopmfile | 2 +- glide.lock | 2 +- public/js/gogs.js | 28 ++++++++++++++++++++++++++++ routers/api/v1/api.go | 1 + routers/api/v1/repo/file.go | 20 ++++++++++++++++++++ routers/repo/editor.go | 1 + templates/repo/editor/edit.tmpl | 2 +- 7 files changed, 53 insertions(+), 3 deletions(-) diff --git a/.gopmfile b/.gopmfile index ed3138d069..0b1b7cc52e 100644 --- a/.gopmfile +++ b/.gopmfile @@ -48,7 +48,7 @@ golang.org/x/text = commit:2910a50 gopkg.in/alexcesaro/quotedprintable.v3 = commit:2caba25 gopkg.in/asn1-ber.v1 = commit:4e86f43 gopkg.in/bufio.v1 = commit:567b2bf -gopkg.in/editorconfig/editorconfig-core-go.v1 = commit:737b8e4 +gopkg.in/editorconfig/editorconfig-core-go.v1 = commit:a872f05 gopkg.in/gomail.v2 = commit:81ebce5 gopkg.in/ini.v1 = commit:cf53f92 gopkg.in/ldap.v2 = commit:d0a5ced diff --git a/glide.lock b/glide.lock index 6c97edd208..0d280df273 100644 --- a/glide.lock +++ b/glide.lock @@ -138,7 +138,7 @@ imports: - name: gopkg.in/bufio.v1 version: 567b2bfa514e796916c4747494d6ff5132a1dfce - name: gopkg.in/editorconfig/editorconfig-core-go.v1 - version: 737b8e4491939fe2277d99b8e726a086266e549e + version: a872f05c2e34b37b567401384d202aff11ba06d4 - name: gopkg.in/gomail.v2 version: 81ebce5c23dfd25c6c67194b37d3dd3f338c98b1 - name: gopkg.in/ini.v1 diff --git a/public/js/gogs.js b/public/js/gogs.js index c9f621745b..d04b5d64d0 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -850,6 +850,34 @@ function initEditor() { else { codeMirrorEditor.setOption("lineWrapping", false); } + + // get the filename without any folder + var value = $editFilename.val(); + if (value.length === 0) { + return; + } + value = value.split('/'); + value = value[value.length - 1]; + + $.getJSON($editFilename.data('ec-url-prefix')+value, function(editorconfig) { + if (editorconfig.indent_style === 'tab') { + codeMirrorEditor.setOption("indentWithTabs", true); + codeMirrorEditor.setOption('extraKeys', {}); + } else { + codeMirrorEditor.setOption("indentWithTabs", false); + // required because CodeMirror doesn't seems to use spaces correctly for {"indentWithTabs": false}: + // - https://github.com/codemirror/CodeMirror/issues/988 + // - https://codemirror.net/doc/manual.html#keymaps + codeMirrorEditor.setOption('extraKeys', { + Tab: function(cm) { + var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(" "); + cm.replaceSelection(spaces); + } + }); + } + codeMirrorEditor.setOption("indentUnit", editorconfig.indent_size || 4); + codeMirrorEditor.setOption("tabSize", editorconfig.tab_width || 4); + }); }).trigger('keyup'); } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 16322fbffd..0591cddb86 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -290,6 +290,7 @@ func RegisterRoutes(m *macaron.Macaron) { Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone). Delete(reqRepoWriter(), repo.DeleteMilestone) }) + m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig) }, repoAssignment()) }, reqToken()) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 972dbefac0..ee9d15a322 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -45,3 +45,23 @@ func GetArchive(ctx *context.APIContext) { repo.Download(ctx.Context) } + +func GetEditorconfig(ctx *context.APIContext) { + ec, err := ctx.Repo.GetEditorconfig() + if err != nil { + if git.IsErrNotExist(err) { + ctx.Error(404, "GetEditorconfig", err) + } else { + ctx.Error(500, "GetEditorconfig", err) + } + return + } + + fileName := ctx.Params("filename") + def := ec.GetDefinitionForFilename(fileName) + if def == nil { + ctx.Error(404, "GetDefinitionForFilename", err) + return + } + ctx.JSON(200, def) +} diff --git a/routers/repo/editor.go b/routers/repo/editor.go index f471e5ee22..bf2244719c 100644 --- a/routers/repo/editor.go +++ b/routers/repo/editor.go @@ -98,6 +98,7 @@ func editFile(ctx *context.Context, isNewFile bool) { ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",") ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",") ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",") + ctx.Data["EditorconfigURLPrefix"] = fmt.Sprintf("%s/api/v1/repos/%s/editorconfig/", setting.AppSubUrl, ctx.Repo.Repository.FullName()) ctx.HTML(200, EDIT_FILE) } diff --git a/templates/repo/editor/edit.tmpl b/templates/repo/editor/edit.tmpl index 9de55c65a1..b247e1ea35 100644 --- a/templates/repo/editor/edit.tmpl +++ b/templates/repo/editor/edit.tmpl @@ -15,7 +15,7 @@ {{range $i, $v := .TreeNames}}
/
{{if eq $i $l}} - + {{else}} {{$v}}