Skip to content

Commit

Permalink
Support Editorconfig on web editor (gogs#3512)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Aug 30, 2016
2 parents 8516dfc + 9ac46fb commit cd9b926
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gopmfile
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions public/js/gogs.js
Expand Up @@ -827,6 +827,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');
}

Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Expand Up @@ -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())

Expand Down
20 changes: 20 additions & 0 deletions routers/api/v1/repo/file.go
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions routers/repo/editor.go
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/editor/edit.tmpl
Expand Up @@ -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>
Expand Down

0 comments on commit cd9b926

Please sign in to comment.