mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-06 11:35:24 +08:00
be smarter for context completion
Completion source based on context is smarter and use codemirror token mecanisme to propose completions, instead of just plitting text at whitespace and before dots.
This commit is contained in:
parent
23ef964f06
commit
e4784b984b
@ -36,29 +36,40 @@
|
||||
}
|
||||
|
||||
// find all 'words' of current cell
|
||||
function getAllTokens(editor)
|
||||
var getAllTokens = function(editor)
|
||||
{
|
||||
var found = [];
|
||||
// get all text remove and split it before dot and at space
|
||||
// keep the dot for completing token that also start with dot
|
||||
var candidates = editor.getValue()
|
||||
.replace(/[.]/g," .")
|
||||
.replace(/[ ]/g,"\n")
|
||||
.split('\n');
|
||||
// append to arry if not already (the function)
|
||||
|
||||
// add to found if not already in it
|
||||
function maybeAdd(str) {
|
||||
if (!arrayContains(found, str)) found.push(str);
|
||||
}
|
||||
// append to arry if not already
|
||||
// (here we do it )
|
||||
for( var c in candidates )
|
||||
|
||||
// loop through all token on all lines
|
||||
var lineCount = editor.lineCount();
|
||||
// loop on line
|
||||
for( var l=0; l< lineCount ; l++)
|
||||
{
|
||||
if(candidates[c].length >= 1){
|
||||
maybeAdd(candidates[c]);}
|
||||
var line = editor.getLine(l);
|
||||
//loop on char
|
||||
for( var c = 1 ; c < line.length ; c++)
|
||||
{
|
||||
var tk = editor.getTokenAt({line:l,ch:c});
|
||||
// if token has a class, it has geat chances of beeing
|
||||
// of interest. Add it to the list of possible completions.
|
||||
// we could skip token of ClassName 'comment'
|
||||
// or 'number' and 'operator'
|
||||
if(tk.className != null){
|
||||
maybeAdd(tk.string);
|
||||
}
|
||||
// jump to char after end of current token
|
||||
c = tk.end;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
function getCompletions(token,editor)
|
||||
{
|
||||
var candidates = getAllTokens(editor);
|
||||
|
Loading…
Reference in New Issue
Block a user