fitnesse.resources.codemirror.addon.hint.fitnesse_anyword-hint.js Maven / Gradle / Ivy
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// Modified to adjust to FitNesse.org basic needs
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
var WORD = /([@\!\$\w][\w]*)([^|]*\|)?/, RANGE = 500;
CodeMirror.registerHelper("hint", "fitnesse_anyword", function(editor, options) {
var word = options && options.word || WORD;
var range = options && options.range || RANGE;
var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
var end = cur.ch, start = end;
while (start && word.test(curLine.charAt(start - 1))) --start;
var curWord = start != end && curLine.slice(start, end).toLocaleLowerCase();
var list = options && options.list || [], seen = {};
function addIfMatch(newWord){
if ((!curWord || newWord.toLocaleLowerCase().lastIndexOf(curWord, 0) == 0) && !Object.prototype.hasOwnProperty.call(seen, newWord)) {
seen[newWord] = true;
list.push(newWord);
}
}
addIfMatch("!contents -R2 -g -p -f -h");
addIfMatch("!include");
addIfMatch("!include -setup");
addIfMatch("!include -seamless");
addIfMatch("!include -teardown");
addIfMatch("!define");
addIfMatch("!today");
addIfMatch("!help");
addIfMatch("!path");
addIfMatch("!fixture");
addIfMatch("!lastmodified");
addIfMatch("!img -l");
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;
addIfMatch(m[0]);
}
}
}
return {list: list, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
});
});