This commit is contained in:
Liang Ding 2014-08-22 12:29:54 +08:00
parent a5b2ecb5a5
commit 356a54c762
2 changed files with 7 additions and 43 deletions

View File

@ -15,8 +15,6 @@ var editors = {
while (start && word.test(curLine.charAt(start - 1))) --start; while (start && word.test(curLine.charAt(start - 1))) --start;
var curWord = start != end && curLine.slice(start, end); var curWord = start != end && curLine.slice(start, end);
console.log(curWord)
var request = { var request = {
code: editor.getValue(), code: editor.getValue(),
cursorLine: editor.getCursor().line, cursorLine: editor.getCursor().line,
@ -48,50 +46,11 @@ var editors = {
return {list: autocompleteHints, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)}; return {list: autocompleteHints, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
}); });
CodeMirror.registerHelper("hint", "anyword", function(editor, options) {
var WORD = /[\w$]+/, RANGE = 500;
var word = options && options.word || WORD;
var range = options && options.range || RANGE;
var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
var start = cur.ch, end = start;
while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
while (start && word.test(curLine.charAt(start - 1))) --start;
var curWord = start != end && curLine.slice(start, end);
var list = [], seen = {};
var re = new RegExp(word.source, "g");
for (var dir = -1; dir <= 1; dir += 2) {
var line = cur.line, endLine = Math.min(Math.max(line + dir * range, editor.firstLine()), editor.lastLine()) + dir;
for (; line != endLine; line += dir) {
var text = editor.getLine(line), m;
while (m = re.exec(text)) {
if (line == cur.line && m[0] === curWord) continue;
if ((!curWord || m[0].lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, m[0])) {
seen[m[0]] = true;
list.push(m[0]);
}
}
}
}
console.log(list);
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
});
CodeMirror.commands.anyword = function(cm) {
cm.showHint({hint: CodeMirror.hint.anyword, completeSingle: false});
};
CodeMirror.commands.autocompleteAfterDot = function(cm) { CodeMirror.commands.autocompleteAfterDot = function(cm) {
var cur = cm.getCursor(); var cur = cm.getCursor();
console.log(cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur)); console.log(cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur));
var tok = cm.getTokenAt(cm.getCursor()); // var token = cm.getTokenAt(cm.getCursor());
console.log(tok)
setTimeout(function() { setTimeout(function() {
if (!cm.state.completionActive) { if (!cm.state.completionActive) {
@ -101,6 +60,10 @@ var editors = {
return CodeMirror.Pass; return CodeMirror.Pass;
}; };
CodeMirror.commands.autocompleteAnyWord = function(cm) {
cm.showHint({hint: CodeMirror.hint.auto});
}
}, },
_initTabs: function() { _initTabs: function() {
var $tabsPanel = $(".edit-panel .tabs-panel"), var $tabsPanel = $(".edit-panel .tabs-panel"),
@ -172,7 +135,7 @@ var editors = {
theme: 'lesser-dark', theme: 'lesser-dark',
indentUnit: 4, indentUnit: 4,
extraKeys: { extraKeys: {
"Ctrl-\\": "anyword", "Ctrl-\\": "autocompleteAnyWord",
".": "autocompleteAfterDot" ".": "autocompleteAfterDot"
} }
}); });

View File

@ -69,6 +69,7 @@
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/codemirror.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/codemirror.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/addon/hint/show-hint.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/addon/hint/show-hint.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/addon/hint/anyword-hint.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/go/go.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/go/go.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/xml/xml.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/xml/xml.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/htmlmixed/htmlmixed.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/mode/htmlmixed/htmlmixed.js"></script>