From bb12838d1c54cd6963236bb831b0507b39214fac Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 15 Jun 2015 21:50:51 +0800 Subject: [PATCH] Enhance autocompletion IDE & Playground --- static/js/editors.js | 11 ++++++++--- static/js/playground.js | 24 ++++++++++++++++++++++-- views/playground/index.html | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/static/js/editors.js b/static/js/editors.js index 1ce4a9d..71d3017 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -359,6 +359,8 @@ var editors = { return; } + editors.autocompleteMutex = true; + $.ajax({ async: false, // 同步执行 type: 'POST', @@ -414,11 +416,13 @@ var editors = { editor.doc.markClean(); $(".edit-panel .tabs > div.current > span").removeClass("changed"); - - editors.autocompleteMutex = false; } }); + setTimeout(function () { + editors.autocompleteMutex = false; + }, 20); + return {list: autocompleteHints, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)}; }); @@ -863,7 +867,8 @@ var editors = { if (config.autocomplete) { var curLine = cm.doc.getLine(cm.getCursor().line).trim().replace(/\W/, ""); - if (0.5 <= Math.random() && "" !== curLine && /^\w+$/.test(curLine)) { + + if (1 === curLine.length || 0.5 <= Math.random() && "" !== curLine && /^\w+$/.test(curLine)) { CodeMirror.commands.autocompleteAfterDot(cm); } } diff --git a/static/js/playground.js b/static/js/playground.js index 6dc754f..d2982ad 100644 --- a/static/js/playground.js +++ b/static/js/playground.js @@ -15,6 +15,7 @@ */ var playground = { + autocompleteMutex: false, editor: undefined, pid: undefined, _resize: function () { @@ -82,6 +83,13 @@ var playground = { var autocompleteHints = []; + if (playground.autocompleteMutex && editor.state.completionActive) { + console.log(1); + return; + } + + playground.autocompleteMutex = true; + $.ajax({ async: false, // 同步执行 type: 'POST', @@ -137,6 +145,10 @@ var playground = { } }); + setTimeout(function () { + playground.autocompleteMutex = false; + }, 20); + return {list: autocompleteHints, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)}; }); @@ -184,7 +196,7 @@ var playground = { ".": "autocompleteAfterDot" } }); - + playground.editor.setOption("gutters", ["CodeMirror-lint-markers", "CodeMirror-foldgutter"]); $(window).resize(function () { @@ -196,7 +208,7 @@ var playground = { } else { playground.editor.setSize("auto", $("#editor").height() + "px"); } - + var hovered = false; $(".menu .ico-share").hover(function () { $(".menu .share-panel").show(); @@ -224,6 +236,14 @@ var playground = { playground.editor.on('changes', function (cm) { $("#url").html(""); + + if (config.autocomplete) { + var curLine = cm.doc.getLine(cm.getCursor().line).trim().replace(/\W/, ""); + + if (1 === curLine.length || 0.5 <= Math.random() && "" !== curLine && /^\w+$/.test(curLine)) { + CodeMirror.commands.autocompleteAfterDot(cm); + } + } }); this._initWS(); diff --git a/views/playground/index.html b/views/playground/index.html index 17fec08..f0301e1 100644 --- a/views/playground/index.html +++ b/views/playground/index.html @@ -102,6 +102,7 @@ "channel": "{{.conf.Channel}}", "wideSessionId": "{{.session.ID}}", "label": {{.i18n}}, + "autocomplete": {{.conf.Autocomplete}}, "embed": {{.embed}}, "disqus": {{.disqus}} };