소스 검색

Support Editorconfig on web editor

Andrey Nering 8 년 전
부모
커밋
9ac46fb983
7개의 변경된 파일53개의 추가작업 그리고 3개의 파일을 삭제
  1. 1 1
      .gopmfile
  2. 1 1
      glide.lock
  3. 28 0
      public/js/gogs.js
  4. 1 0
      routers/api/v1/api.go
  5. 20 0
      routers/api/v1/repo/file.go
  6. 1 0
      routers/repo/editor.go
  7. 1 1
      templates/repo/editor/edit.tmpl

+ 1 - 1
.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

+ 1 - 1
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

+ 28 - 0
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');
 }
 

+ 1 - 0
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())
 

+ 20 - 0
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)
+}

+ 1 - 0
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)
 }

+ 1 - 1
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>