diff options
author | Andrey Nering <andrey.nering@gmail.com> | 2016-08-30 20:18:40 -0300 |
---|---|---|
committer | Andrey Nering <andrey.nering@gmail.com> | 2016-08-30 20:30:47 -0300 |
commit | 9ac46fb983f31cfece76e2181a9fe73b71f02e2e (patch) | |
tree | 6499c69d94fcbeb71c144295c6df1ccc1883f8b9 | |
parent | 47a3243ff1b3da1b435d9390e578752f3a74ae8e (diff) | |
download | gitea-9ac46fb983f31cfece76e2181a9fe73b71f02e2e.tar.gz gitea-9ac46fb983f31cfece76e2181a9fe73b71f02e2e.zip |
Support Editorconfig on web editor
-rw-r--r-- | .gopmfile | 2 | ||||
-rw-r--r-- | glide.lock | 2 | ||||
-rw-r--r-- | public/js/gogs.js | 28 | ||||
-rw-r--r-- | routers/api/v1/api.go | 1 | ||||
-rw-r--r-- | routers/api/v1/repo/file.go | 20 | ||||
-rw-r--r-- | routers/repo/editor.go | 1 | ||||
-rw-r--r-- | templates/repo/editor/edit.tmpl | 2 |
7 files changed, 53 insertions, 3 deletions
@@ -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}} <div class="divider"> / </div> {{if eq $i $l}} - <input id="file-name" value="{{$v}}" placeholder="{{$.i18n.Tr "repo.editor.name_your_file"}}" required autofocus> + <input id="file-name" value="{{$v}}" placeholder="{{$.i18n.Tr "repo.editor.name_your_file"}}" data-ec-url-prefix="{{$.EditorconfigURLPrefix}}" required autofocus> <span class="octicon octicon-info poping up" data-content="{{$.i18n.Tr "repo.editor.filename_help"}}" data-position="bottom center" data-variation="tiny inverted"></span> {{else}} <span class="section"><a href="{{EscapePound $.BranchLink}}/{{EscapePound $v}}">{{$v}}</a></span> |