From da9c6079f02224df5bc001dc4c2bb7e392c474a5 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 14 Jun 2015 14:43:09 +0800 Subject: [PATCH] Fix #230 --- conf/wide.go | 1 + conf/wide.json | 3 ++- static/js/editors.js | 33 +++++++++++++++++++++++++-------- views/index.html | 3 ++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/conf/wide.go b/conf/wide.go index bd903ca..4b7b6e1 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -70,6 +70,7 @@ type conf struct { Locale string // default locale Playground string // playground directory AllowRegister bool // allow register or not + Autocomplete bool // default autocomplete } // Logger. diff --git a/conf/wide.json b/conf/wide.json index 9ac29b1..dac8bb1 100644 --- a/conf/wide.json +++ b/conf/wide.json @@ -13,5 +13,6 @@ "WD": "${pwd}", "Locale": "en_US", "Playground": "${home}/playground", - "AllowRegister": true + "AllowRegister": true, + "Autocomplete": true } \ No newline at end of file diff --git a/static/js/editors.js b/static/js/editors.js index 4f9c4f8..1ce4a9d 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -15,6 +15,7 @@ */ var editors = { + autocompleteMutex: false, data: [], tabs: {}, getEditorByPath: function (path) { @@ -354,6 +355,10 @@ var editors = { var autocompleteHints = []; + if (editors.autocompleteMutex && editor.state.completionActive) { + return; + } + $.ajax({ async: false, // 同步执行 type: 'POST', @@ -407,9 +412,10 @@ var editors = { } } - // 清除未保存状态 editor.doc.markClean(); $(".edit-panel .tabs > div.current > span").removeClass("changed"); + + editors.autocompleteMutex = false; } }); @@ -842,13 +848,24 @@ var editors = { $span.removeClass("changed"); } }); - } else { - $(".edit-panel .tabs > div").each(function () { - var $span = $(this).find("span:eq(0)"); - if ($span.attr("title") === cm.options.path) { - $span.addClass("changed"); - } - }); + + return; + } + + // changed + + $(".edit-panel .tabs > div").each(function () { + var $span = $(this).find("span:eq(0)"); + if ($span.attr("title") === cm.options.path) { + $span.addClass("changed"); + } + }); + + if (config.autocomplete) { + var curLine = cm.doc.getLine(cm.getCursor().line).trim().replace(/\W/, ""); + if (0.5 <= Math.random() && "" !== curLine && /^\w+$/.test(curLine)) { + CodeMirror.commands.autocompleteAfterDot(cm); + } } }); diff --git a/views/index.html b/views/index.html index a3af2dd..e42ec37 100644 --- a/views/index.html +++ b/views/index.html @@ -609,7 +609,8 @@ "editorTheme": '{{.user.Editor.Theme}}', "latestSessionContent": {{.latestSessionContent}}, "editorTabSize": '{{.user.Editor.TabSize}}', - "keymap": '{{.user.Keymap}}' + "keymap": '{{.user.Keymap}}', + "autocomplete": {{.conf.Autocomplete}} }; // 发往 Wide 的所有 AJAX 请求需要使用该函数创建请求参数. function newWideRequest() {