##// END OF EJS Templates
import Utils keycodes into the completer
Matthias BUSSONNIER -
Show More
@@ -1,241 +1,233 b''
1 // function completer.
1 // function completer.
2 //
2 //
3 // completer should be a class that take an editor instance, and a list of
3 // completer should be a class that take an editor instance, and a list of
4 // function to call to get the list of completion.
4 // function to call to get the list of completion.
5 //
5 //
6 // the function that send back the list of completion should received the
6 // the function that send back the list of completion should received the
7 // editor handle as sole argument, and should return a json object with the
7 // editor handle as sole argument, and should return a json object with the
8 // following structure
8 // following structure
9
9
10 // {list: clist, # list of n string containing the completions
10 // {list: clist, # list of n string containing the completions
11 // type : rp, # list of n string containingtype/ origin of the completion
11 // type : rp, # list of n string containingtype/ origin of the completion
12 // # (will be set as the class of the <option> to be able to style
12 // # (will be set as the class of the <option> to be able to style
13 // # them according to the origin of the completion)
13 // # them according to the origin of the completion)
14 // from: {line: cur.line, ch: token.start},
14 // from: {line: cur.line, ch: token.start},
15 // to: {line: cur.line, ch: token.end}
15 // to: {line: cur.line, ch: token.end}
16 // };
16 // };
17 //
17 //
18
18
19 var IPython = (function(IPython ) {
19 var IPython = (function(IPython ) {
20 // that will prevent us froom missspelling
20 // that will prevent us froom missspelling
21 "use strict";
21 "use strict";
22
22
23 // easyier key mapping
23 // easyier key mapping
24 var key = { tab:9,
24 var key = IPython.utils.keycodes;
25 esc:27,
26 backspace:8,
27 space:32,
28 shift:16,
29 enter:13,
30 upArrow:38, // check with keyDown..
31 downArrow :40 // check with keyUp
32 };
33
25
34 // what is the common start of all completions
26 // what is the common start of all completions
35 function sharedStart(B){
27 function sharedStart(B){
36 if(B.length == 1){return B[0]}
28 if(B.length == 1){return B[0]}
37 var A = new Array()
29 var A = new Array()
38 for(var i=0; i< B.length; i++)
30 for(var i=0; i< B.length; i++)
39 {
31 {
40 A.push(B[i].str);
32 A.push(B[i].str);
41 }
33 }
42 if(A.length > 1 ){
34 if(A.length > 1 ){
43 var tem1, tem2, s, A = A.slice(0).sort();
35 var tem1, tem2, s, A = A.slice(0).sort();
44 tem1 = A[0];
36 tem1 = A[0];
45 s = tem1.length;
37 s = tem1.length;
46 tem2 = A.pop();
38 tem2 = A.pop();
47 while(s && tem2.indexOf(tem1) == -1){
39 while(s && tem2.indexOf(tem1) == -1){
48 tem1 = tem1.substring(0, --s);
40 tem1 = tem1.substring(0, --s);
49 }
41 }
50 if (tem1 == "" || tem2.indexOf(tem1) != 0){return null;}
42 if (tem1 == "" || tem2.indexOf(tem1) != 0){return null;}
51 return { str : tem1,
43 return { str : tem1,
52 type : "computed",
44 type : "computed",
53 from : B[0].from,
45 from : B[0].from,
54 to : B[0].to
46 to : B[0].to
55 };
47 };
56 }
48 }
57 return null;
49 return null;
58 }
50 }
59
51
60 // user to nsert the given completion
52 // user to nsert the given completion
61 var Completer = function(editor,getHints) {
53 var Completer = function(editor,getHints) {
62 this.editor = editor;
54 this.editor = editor;
63 this.hintfunc = getHints;
55 this.hintfunc = getHints;
64 // if last caractere before cursor is not in this, we stop completing
56 // if last caractere before cursor is not in this, we stop completing
65 this.reg = /[A-Za-z.]/;
57 this.reg = /[A-Za-z.]/;
66 }
58 }
67
59
68 Completer.prototype.startCompletion = function()
60 Completer.prototype.startCompletion = function()
69 {
61 {
70 // call for a 'first' completion, that will set the editor and do some
62 // call for a 'first' completion, that will set the editor and do some
71 // special behaviour like autopicking if only one completion availlable
63 // special behaviour like autopicking if only one completion availlable
72 //
64 //
73 if (this.editor.somethingSelected()) return;
65 if (this.editor.somethingSelected()) return;
74 this.done = false;
66 this.done = false;
75 // use to get focus back on opera
67 // use to get focus back on opera
76 this.carryOnCompletion(true);
68 this.carryOnCompletion(true);
77 }
69 }
78
70
79 Completer.prototype.carryOnCompletion = function(ff)
71 Completer.prototype.carryOnCompletion = function(ff)
80 {
72 {
81 // pass true as parameter if you want the commpleter to autopick
73 // pass true as parameter if you want the commpleter to autopick
82 // when only one completion
74 // when only one completion
83 // as this function is auto;atically reinvoked at each keystroke with
75 // as this function is auto;atically reinvoked at each keystroke with
84 // ff = false
76 // ff = false
85 var cur = this.editor.getCursor();
77 var cur = this.editor.getCursor();
86 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-1},cur);
78 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-1},cur);
87
79
88 // we nned to check that we are still on a word boundary
80 // we nned to check that we are still on a word boundary
89 // because while typing the completer is still reinvoking itself
81 // because while typing the completer is still reinvoking itself
90 if(!this.reg.test(pre_cursor)){ this.close(); return;}
82 if(!this.reg.test(pre_cursor)){ this.close(); return;}
91
83
92 this.autopick = false;
84 this.autopick = false;
93 if( ff != 'undefined' && ff==true)
85 if( ff != 'undefined' && ff==true)
94 {
86 {
95 this.autopick=true;
87 this.autopick=true;
96 }
88 }
97 // We want a single cursor position.
89 // We want a single cursor position.
98 if (this.editor.somethingSelected()) return;
90 if (this.editor.somethingSelected()) return;
99
91
100 // there we will need to gather the results for all the function (and merge them ?)
92 // there we will need to gather the results for all the function (and merge them ?)
101 // lets assume for now only one source
93 // lets assume for now only one source
102 //
94 //
103 var that = this;
95 var that = this;
104 this.hintfunc(function(result){that._resume_completion(result)});
96 this.hintfunc(function(result){that._resume_completion(result)});
105 }
97 }
106 Completer.prototype._resume_completion = function(results)
98 Completer.prototype._resume_completion = function(results)
107 {
99 {
108 this.raw_result = results;
100 this.raw_result = results;
109
101
110 // if empty result return
102 // if empty result return
111 if (!this.raw_result || !this.raw_result.length) return;
103 if (!this.raw_result || !this.raw_result.length) return;
112
104
113
105
114
106
115 // When there is only one completion, use it directly.
107 // When there is only one completion, use it directly.
116 if (this.autopick == true && this.raw_result.length == 1)
108 if (this.autopick == true && this.raw_result.length == 1)
117 {
109 {
118 this.insert(this.raw_result[0]);
110 this.insert(this.raw_result[0]);
119 return true;
111 return true;
120 }
112 }
121
113
122 if (this.raw_result.length == 1)
114 if (this.raw_result.length == 1)
123 {
115 {
124 // test if first and only completion totally matches
116 // test if first and only completion totally matches
125 // what is typed, in this case dismiss
117 // what is typed, in this case dismiss
126 var str = this.raw_result[0].str
118 var str = this.raw_result[0].str
127 var cur = this.editor.getCursor();
119 var cur = this.editor.getCursor();
128 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-str.length},cur);
120 var pre_cursor = this.editor.getRange({line:cur.line,ch:cur.ch-str.length},cur);
129 if(pre_cursor == str){
121 if(pre_cursor == str){
130 this.close();
122 this.close();
131 return ;
123 return ;
132 }
124 }
133 }
125 }
134
126
135 this.complete = $('<div/>').addClass('completions');
127 this.complete = $('<div/>').addClass('completions');
136 this.complete.attr('id','complete');
128 this.complete.attr('id','complete');
137
129
138 this.sel = $('<select/>')
130 this.sel = $('<select/>')
139 .attr('multiple','true')
131 .attr('multiple','true')
140 .attr('size',Math.min(10,this.raw_result.length));
132 .attr('size',Math.min(10,this.raw_result.length));
141 var pos = this.editor.cursorCoords();
133 var pos = this.editor.cursorCoords();
142
134
143 // TODO: I propose to remove enough horizontal pixel
135 // TODO: I propose to remove enough horizontal pixel
144 // to align the text later
136 // to align the text later
145 this.complete.css('left',pos.x+'px');
137 this.complete.css('left',pos.x+'px');
146 this.complete.css('top',pos.yBot+'px');
138 this.complete.css('top',pos.yBot+'px');
147 this.complete.append(this.sel);
139 this.complete.append(this.sel);
148
140
149 $('body').append(this.complete);
141 $('body').append(this.complete);
150 //build the container
142 //build the container
151 var that = this;
143 var that = this;
152 this.sel.dblclick(function(){that.pick()});
144 this.sel.dblclick(function(){that.pick()});
153 this.sel.blur(this.close);
145 this.sel.blur(this.close);
154 this.sel.keydown(function(event){that.keydown(event)});
146 this.sel.keydown(function(event){that.keydown(event)});
155
147
156 this.build_gui_list(this.raw_result);
148 this.build_gui_list(this.raw_result);
157 var that=this;
149 var that=this;
158 //CodeMirror.connect(that.sel, "dblclick", function(){that.pick()});
150 //CodeMirror.connect(that.sel, "dblclick", function(){that.pick()});
159
151
160 this.sel.focus();
152 this.sel.focus();
161 // Opera sometimes ignores focusing a freshly created node
153 // Opera sometimes ignores focusing a freshly created node
162 if (window.opera) setTimeout(function(){if (!this.done) this.sel.focus();}, 100);
154 if (window.opera) setTimeout(function(){if (!this.done) this.sel.focus();}, 100);
163 // why do we return true ?
155 // why do we return true ?
164 return true;
156 return true;
165 }
157 }
166
158
167 Completer.prototype.insert = function(completion) {
159 Completer.prototype.insert = function(completion) {
168 this.editor.replaceRange(completion.str, completion.from, completion.to);
160 this.editor.replaceRange(completion.str, completion.from, completion.to);
169 }
161 }
170
162
171 Completer.prototype.build_gui_list = function(completions){
163 Completer.prototype.build_gui_list = function(completions){
172 // Need to clear the all list
164 // Need to clear the all list
173 for (var i = 0; i < completions.length; ++i) {
165 for (var i = 0; i < completions.length; ++i) {
174 var opt = $('<option/>')
166 var opt = $('<option/>')
175 .text(completions[i].str)
167 .text(completions[i].str)
176 .addClass(completions[i].type);
168 .addClass(completions[i].type);
177 this.sel.append(opt);
169 this.sel.append(opt);
178 }
170 }
179 this.sel.children().first().attr('selected','true');
171 this.sel.children().first().attr('selected','true');
180
172
181 //sel.size = Math.min(10, completions.length);
173 //sel.size = Math.min(10, completions.length);
182 // Hack to hide the scrollbar.
174 // Hack to hide the scrollbar.
183 //if (completions.length <= 10)
175 //if (completions.length <= 10)
184 //this.complete.style.width = (this.sel.clientWidth - 1) + "px";
176 //this.complete.style.width = (this.sel.clientWidth - 1) + "px";
185 }
177 }
186
178
187 Completer.prototype.close = function() {
179 Completer.prototype.close = function() {
188 if (this.done) return;
180 if (this.done) return;
189 this.done = true;
181 this.done = true;
190 $('.completions').remove();
182 $('.completions').remove();
191 }
183 }
192
184
193 Completer.prototype.pick = function(){
185 Completer.prototype.pick = function(){
194 this.insert(this.raw_result[this.sel[0].selectedIndex]);
186 this.insert(this.raw_result[this.sel[0].selectedIndex]);
195 this.close();
187 this.close();
196 var that = this;
188 var that = this;
197 setTimeout(function(){that.editor.focus();}, 50);
189 setTimeout(function(){that.editor.focus();}, 50);
198 }
190 }
199
191
200
192
201 Completer.prototype.keydown = function(event) {
193 Completer.prototype.keydown = function(event) {
202 var code = event.keyCode;
194 var code = event.keyCode;
203 // Enter
195 // Enter
204 if (code == key.enter) {CodeMirror.e_stop(event); this.pick();}
196 if (code == key.enter) {CodeMirror.e_stop(event); this.pick();}
205 // Escape or backspace
197 // Escape or backspace
206 else if (code == key.esc ) {CodeMirror.e_stop(event); this.close(); this.editor.focus();}
198 else if (code == key.esc ) {CodeMirror.e_stop(event); this.close(); this.editor.focus();}
207 else if (code == key.space || code == key.backspace) {this.close(); this.editor.focus();}
199 else if (code == key.space || code == key.backspace) {this.close(); this.editor.focus();}
208 else if (code == key.tab){
200 else if (code == key.tab){
209 //all the fastforwarding operation,
201 //all the fastforwarding operation,
210 //Check that shared start is not null which can append with prefixed completion
202 //Check that shared start is not null which can append with prefixed completion
211 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
203 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
212 // to erase py
204 // to erase py
213 var sh = sharedStart(this.raw_result);
205 var sh = sharedStart(this.raw_result);
214 if(sh){
206 if(sh){
215 this.insert(sh);
207 this.insert(sh);
216 }
208 }
217 this.close();
209 this.close();
218 CodeMirror.e_stop(event);
210 CodeMirror.e_stop(event);
219 this.editor.focus();
211 this.editor.focus();
220 //reinvoke self
212 //reinvoke self
221 var that = this;
213 var that = this;
222 setTimeout(function(){that.carryOnCompletion();}, 50);
214 setTimeout(function(){that.carryOnCompletion();}, 50);
223 }
215 }
224 else if (code == key.upArrow || code == key.downArrow) {
216 else if (code == key.upArrow || code == key.downArrow) {
225 // need to do that to be able to move the arrow
217 // need to do that to be able to move the arrow
226 // when on the first or last line ofo a code cell
218 // when on the first or last line ofo a code cell
227 event.stopPropagation();
219 event.stopPropagation();
228 }
220 }
229 else if (code != key.upArrow && code != key.downArrow) {
221 else if (code != key.upArrow && code != key.downArrow) {
230 this.close(); this.editor.focus();
222 this.close(); this.editor.focus();
231 //we give focus to the editor immediately and call sell in 50 ms
223 //we give focus to the editor immediately and call sell in 50 ms
232 var that = this;
224 var that = this;
233 setTimeout(function(){that.carryOnCompletion();}, 50);
225 setTimeout(function(){that.carryOnCompletion();}, 50);
234 }
226 }
235 }
227 }
236
228
237
229
238 IPython.Completer = Completer;
230 IPython.Completer = Completer;
239
231
240 return IPython;
232 return IPython;
241 }(IPython));
233 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now