Permalink
Browse files

Managed to fix the indenter, go figure...

  • Loading branch information...
1 parent c7d81fc commit b8c76d1c5b00efa2c51f50c7c43d0a985b52062b @jamesgao committed Oct 21, 2010
@@ -1,5 +1,105 @@
/*
* jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
* (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
+ * Modified 2010 James Gao <james@jamesgao.com>
*/
-(function(){var c={getSelection:function(){var e=this.jquery?this[0]:this;return(('selectionStart'in e&&function(){var l=e.selectionEnd-e.selectionStart;return{start:e.selectionStart,end:e.selectionEnd,length:l,text:e.value.substr(e.selectionStart,l)}})||(document.selection&&function(){e.focus();var r=document.selection.createRange();if(r==null){return{start:0,end:e.value.length,length:0}}var a=e.createTextRange();var b=a.duplicate();a.moveToBookmark(r.getBookmark());b.setEndPoint('EndToStart',a);return{start:b.text.length,end:b.text.length+r.text.length,length:r.text.length,text:r.text}})||function(){return{start:0,end:e.value.length,length:0}})()},replaceSelection:function(){var e=this.jquery?this[0]:this;var a=arguments[0]||'';return(('selectionStart'in e&&function(){e.value=e.value.substr(0,e.selectionStart)+a+e.value.substr(e.selectionEnd,e.value.length);return this})||(document.selection&&function(){e.focus();document.selection.createRange().text=a;return this})||function(){e.value+=a;return this})()}};jQuery.each(c,function(i){jQuery.fn[i]=this})})();
+
+(function() {
+
+ var fieldSelection = {
+
+ getSelection: function() {
+
+ var e = this.jquery ? this[0] : this;
+
+ return (
+
+ /* mozilla / dom 3.0 */
+ ('selectionStart' in e && function() {
+ var l = e.selectionEnd - e.selectionStart;
+ return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
+ }) ||
+
+ /* exploder */
+ (document.selection && function() {
+
+ e.focus();
+
+ var r = document.selection.createRange();
+ if (r == null) {
+ return { start: 0, end: e.value.length, length: 0 }
+ }
+
+ var re = e.createTextRange();
+ var rc = re.duplicate();
+ re.moveToBookmark(r.getBookmark());
+ rc.setEndPoint('EndToStart', re);
+
+ return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
+ }) ||
+
+ /* browser not supported */
+ function() {
+ return { start: 0, end: e.value.length, length: 0 };
+ }
+
+ )();
+
+ },
+
+ setCursor: function () {
+ var e = this.jquery ? this[0] : this;
+ var pos = arguments[0] || 0;
+
+ return (
+ ('setSelectionRange' in e && function () {
+ e.focus();
+ e.setSelectionRange(pos,pos);
+ return this;
+ }) ||
+ ('createTextRange' in e && function () {
+ var range = e.createTextRange();
+ range.collapse(true);
+ range.moveEnd('character', pos);
+ range.moveStart('character', pos);
+ range.select();
+ return this;
+ })
+ )();
+ },
+
+ replaceSelection: function() {
+
+ var e = this.jquery ? this[0] : this;
+ var text = arguments[0] || '';
+
+ return (
+
+ /* mozilla / dom 3.0 */
+ ('selectionStart' in e && function() {
+ e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
+ return this;
+ }) ||
+
+ /* exploder */
+ (document.selection && function() {
+ e.focus();
+ document.selection.createRange().text = text;
+ return this;
+ }) ||
+
+ /* browser not supported */
+ function() {
+ e.value += text;
+ return this;
+ }
+
+ )();
+
+ }
+
+ };
+
+ jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });
+
+})();
@@ -82,7 +82,8 @@ function inspect(obj) {
var dedentKeys = /(raise|return|break|continue|yield|pass)/g
function checkIndent(code, pos) {
code = code.slice(0,pos).split("\n")
- var last = code[code.length-1]
+ //Second to last one, the last one is the newline
+ var last = code[code.length-2].trim()
if (last[last.length-1] == ":")
return 1
else if (dedentKeys.test(last))
@@ -315,7 +315,7 @@ InputArea.prototype.keyfunc = function (e) {
} else {
if (e.which == 13) {
if (e.ctrlKey) {
- this.text.val(this.text.val()+"\n")
+ this.insert("\n")
this.update(1)
} else
this.submit(e.target.value)
@@ -336,12 +336,12 @@ InputArea.prototype.keyfunc = function (e) {
InputArea.prototype.update = function (nlines) {
//Updates line height for multi-line input
var nnlines = this.msg.code.split("\n").length+(nlines?nlines:0)
- if (nnlines != this.nlines)
+ if (nnlines != this.nlines) {
this.text.animate({height: this.lh*nnlines }, 50)
+ this.indent(this.nlines > nnlines?-1:null)
+ }
this.nlines = nnlines
var code = this.text.val().slice(0,this.text.getSelection().end)
- if (code[code.length-1] == "\n")
- this.indent()
}
InputArea.prototype.submit = function (code) {
//Submits to the server, or removes the message if empty
@@ -380,24 +380,24 @@ InputArea.prototype.replace = function (match, pos) {
this.msg.code = words.join(' ')
this.text.val(this.msg.code)
}
-InputArea.prototype.indent = function () {
+InputArea.prototype.indent = function (dedent) {
//An attempt to make an autoindenter
var code = this.text.val()
var pos = this.text.getSelection().end
- this.ilevel += checkIndent(code, pos)
+ this.ilevel += dedent?dedent:checkIndent(code, pos)
this.ilevel = this.ilevel < 0?0:this.ilevel
var tabs = ""
for (var i = 0; i < this.ilevel; i++)
tabs += " "
- this.text.val(code.slice(0,pos)+tabs+code.slice(pos))
+ this.insert(tabs)
}
InputArea.prototype.insert = function (txt) {
var pos = this.text.getSelection().end
- txt = this.text.val().slice(0,pos)+txt+this.text.val().slice(pos)
- this.text.val(txt)
+ var code = this.text.val().slice(0,pos)+txt+this.text.val().slice(pos)
+ this.text.val(code).setCursor(pos+txt.length)
}
@@ -53,7 +53,7 @@ def send(self, msg_type, *args):
return self.req_queue.get()
def complete(self, code, pos):
- chunk = re.split('\s|\(', code[:int(pos)])[-1]
+ chunk = re.split('\s|\(|=|;', code[:int(pos)])[-1]
self.kernel_manager.xreq_channel.complete(chunk, code, pos)
return self.req_queue.get()

0 comments on commit b8c76d1

Please sign in to comment.