autocomplete

This commit is contained in:
Liang Ding 2014-08-22 09:44:41 +08:00
parent 67a1e39bd1
commit a27b9b5d3a
2 changed files with 126 additions and 9 deletions

View File

@ -0,0 +1,55 @@
package main
import (
"flag"
"fmt"
"time"
)
type X struct {
s []*int
}
func (x X) F1(n int) int {
if n <= 2 {
return n
}
return x.F1(n-1) + x.F1(n-2)
}
func (x *X) F2(n int) int {
if n <= 2 {
return n
}
return x.F2(n-1) + x.F2(n-2)
}
func F3(n int) int {
if n <= 2 {
return n
}
return F3(n-1) + F3(n-2)
}
var n = flag.Int("n", 40, "")
func main() {
flag.Parse()
N := *n
x := &X{}
start := time.Now()
x.F1(N)
end := time.Now()
fmt.Printf("F1: %v\n", end.Sub(start))
start = time.Now()
x.F2(N)
end = time.Now()
fmt.Printf("F2: %v\n", end.Sub(start))
start = time.Now()
F3(N)
end = time.Now()
fmt.Printf("F3: %v\n", end.Sub(start))
}

View File

@ -6,7 +6,16 @@ var editors = {
}, },
_initAutocomplete: function() { _initAutocomplete: function() {
CodeMirror.registerHelper("hint", "go", function(editor, options) { CodeMirror.registerHelper("hint", "go", function(editor, options) {
var cur = editor.getCursor(); var word = /[\w$]+/
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);
console.log(curWord)
var request = { var request = {
code: editor.getValue(), code: editor.getValue(),
@ -27,19 +36,71 @@ var editors = {
dataType: "json", dataType: "json",
success: function(data) { success: function(data) {
var autocompleteArray = data[1]; var autocompleteArray = data[1];
for (var i = 0; i < autocompleteArray.length; i++) { if (autocompleteArray) {
autocompleteHints[i] = autocompleteArray[i].name; for (var i = 0; i < autocompleteArray.length; i++) {
} autocompleteHints[i] = autocompleteArray[i].name;
}
}
} }
}); });
return {list: autocompleteHints, from: cur, to: cur}; 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.autocomplete = function(cm) { CodeMirror.commands.anyword = function(cm) {
cm.showHint({hint: CodeMirror.hint.go}); cm.showHint({hint: CodeMirror.hint.anyword, completeSingle: false});
}; };
CodeMirror.commands.autocompleteAfterDot = function(cm) {
var cur = cm.getCursor();
console.log(cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur));
var tok = cm.getTokenAt(cm.getCursor());
console.log(tok)
setTimeout(function() {
if (!cm.state.completionActive) {
cm.showHint({hint: CodeMirror.hint.go, completeSingle: false});
}
}, 100)
return CodeMirror.Pass;
};
}, },
_initTabs: function() { _initTabs: function() {
var $tabsPanel = $(".edit-panel .tabs-panel"), var $tabsPanel = $(".edit-panel .tabs-panel"),
@ -111,7 +172,8 @@ var editors = {
theme: 'lesser-dark', theme: 'lesser-dark',
indentUnit: 4, indentUnit: 4,
extraKeys: { extraKeys: {
"Ctrl-\\": "autocomplete" "Ctrl-\\": "anyword",
".": "autocompleteAfterDot"
} }
}); });
editor.setSize('100%', 450); editor.setSize('100%', 450);