contexthint.js
98 lines
| 3.0 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17200 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Matthias BUSSONNIER
|
r7131 | // highly adapted for codemiror jshint | ||
Jonathan Frederic
|
r17200 | define([], function() { | ||
Matthias BUSSONNIER
|
r7133 | "use strict"; | ||
Matthias BUSSONNIER
|
r7194 | |||
Jonathan Frederic
|
r17200 | var forEach = function(arr, f) { | ||
Matthias BUSSONNIER
|
r7133 | for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); | ||
Jonathan Frederic
|
r17200 | }; | ||
Matthias BUSSONNIER
|
r7194 | |||
Jonathan Frederic
|
r17200 | var arrayContains = function(arr, item) { | ||
Matthias BUSSONNIER
|
r7133 | if (!Array.prototype.indexOf) { | ||
var i = arr.length; | ||||
while (i--) { | ||||
Matthias BUSSONNIER
|
r7194 | if (arr[i] === item) { | ||
return true; | ||||
} | ||||
Matthias BUSSONNIER
|
r7133 | } | ||
return false; | ||||
} | ||||
return arr.indexOf(item) != -1; | ||||
Jonathan Frederic
|
r17200 | }; | ||
Matthias BUSSONNIER
|
r7194 | |||
Matthias BUSSONNIER
|
r7195 | CodeMirror.contextHint = function (editor) { | ||
Matthias BUSSONNIER
|
r7133 | // Find the token at the cursor | ||
Matthias BUSSONNIER
|
r7194 | var cur = editor.getCursor(), | ||
token = editor.getTokenAt(cur), | ||||
tprop = token; | ||||
Matthias BUSSONNIER
|
r7133 | // If it's not a 'word-style' token, ignore the token. | ||
// If it is a property, find out what it is a property of. | ||||
Jonathan Frederic
|
r17200 | var list = []; | ||
Matthias BUSSONNIER
|
r7194 | var clist = getCompletions(token, editor); | ||
for (var i = 0; i < clist.length; i++) { | ||||
list.push({ | ||||
str: clist[i], | ||||
type: "context", | ||||
from: { | ||||
line: cur.line, | ||||
ch: token.start | ||||
}, | ||||
to: { | ||||
line: cur.line, | ||||
ch: token.end | ||||
} | ||||
Jonathan Frederic
|
r17200 | }); | ||
Matthias BUSSONNIER
|
r7133 | } | ||
return list; | ||||
Jonathan Frederic
|
r17200 | }; | ||
Matthias BUSSONNIER
|
r7131 | |||
Matthias BUSSONNIER
|
r7133 | // find all 'words' of current cell | ||
Matthias BUSSONNIER
|
r7195 | var getAllTokens = function (editor) { | ||
Jonathan Frederic
|
r17200 | var found = []; | ||
Matthias BUSSONNIER
|
r7194 | |||
Jonathan Frederic
|
r17200 | // add to found if not already in it | ||
Matthias BUSSONNIER
|
r7140 | |||
Jonathan Frederic
|
r17200 | function maybeAdd(str) { | ||
if (!arrayContains(found, str)) found.push(str); | ||||
} | ||||
Matthias BUSSONNIER
|
r7194 | |||
Jonathan Frederic
|
r17200 | // loop through all token on all lines | ||
var lineCount = editor.lineCount(); | ||||
// loop on line | ||||
for (var l = 0; l < lineCount; l++) { | ||||
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); | ||||
Matthias BUSSONNIER
|
r7140 | } | ||
Jonathan Frederic
|
r17200 | // jump to char after end of current token | ||
c = tk.end; | ||||
Matthias BUSSONNIER
|
r7140 | } | ||
Matthias BUSSONNIER
|
r7133 | } | ||
Jonathan Frederic
|
r17200 | return found; | ||
}; | ||||
Matthias BUSSONNIER
|
r7131 | |||
Jonathan Frederic
|
r17200 | var getCompletions = function(token, editor) { | ||
Matthias BUSSONNIER
|
r7133 | var candidates = getAllTokens(editor); | ||
// filter all token that have a common start (but nox exactly) the lenght of the current token | ||||
Matthias BUSSONNIER
|
r7195 | var lambda = function (x) { | ||
Jonathan Frederic
|
r17200 | return (x.indexOf(token.string) === 0 && x != token.string); | ||
Matthias BUSSONNIER
|
r7133 | }; | ||
var filterd = candidates.filter(lambda); | ||||
return filterd; | ||||
Jonathan Frederic
|
r17200 | }; | ||
Jonathan Frederic
|
r17201 | return {'contextHint': CodeMirror.contextHint}; | ||
Jonathan Frederic
|
r17200 | }); | ||