2011-10-29 04:20:49 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported getCompletions, getCommonPrefix, getDeclaredConstants */
|
2011-10-29 04:20:49 -04:00
|
|
|
|
|
|
|
// Returns a list of potential completions for text. Completions either
|
|
|
|
// follow a dot (e.g. foo.ba -> bar) or they are picked from globalCompletionList (e.g. fo -> foo)
|
|
|
|
// commandHeader is prefixed on any expression before it is eval'ed. It will most likely
|
|
|
|
// consist of global constants that might not carry over from the calling environment.
|
|
|
|
//
|
|
|
|
// This function is likely the one you want to call from external modules
|
|
|
|
function getCompletions(text, commandHeader, globalCompletionList) {
|
|
|
|
let methods = [];
|
2019-01-31 09:08:00 -05:00
|
|
|
let expr_, base;
|
2011-10-29 04:20:49 -04:00
|
|
|
let attrHead = '';
|
2019-08-19 20:51:42 -04:00
|
|
|
if (globalCompletionList == null)
|
2011-10-29 04:20:49 -04:00
|
|
|
globalCompletionList = [];
|
|
|
|
|
|
|
|
let offset = getExpressionOffset(text, text.length - 1);
|
|
|
|
if (offset >= 0) {
|
|
|
|
text = text.slice(offset);
|
|
|
|
|
|
|
|
// Look for expressions like "Main.panel.foo" and match Main.panel and foo
|
|
|
|
let matches = text.match(/(.*)\.(.*)/);
|
|
|
|
if (matches) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[expr_, base, attrHead] = matches;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
methods = getPropertyNamesFromExpression(base, commandHeader).filter(
|
|
|
|
attr => attr.slice(0, attrHead.length) == attrHead
|
|
|
|
);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the empty expression or partially entered words
|
|
|
|
// not proceeded by a dot and match them against global constants
|
|
|
|
matches = text.match(/^(\w*)$/);
|
|
|
|
if (text == '' || matches) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[expr_, attrHead] = matches;
|
2017-10-30 20:38:18 -04:00
|
|
|
methods = globalCompletionList.filter(
|
|
|
|
attr => attr.slice(0, attrHead.length) == attrHead
|
|
|
|
);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [methods, attrHead];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// A few functions for parsing strings of javascript code.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Identify characters that delimit an expression. That is,
|
|
|
|
// if we encounter anything that isn't a letter, '.', ')', or ']',
|
|
|
|
// we should stop parsing.
|
|
|
|
function isStopChar(c) {
|
2019-01-29 17:44:38 -05:00
|
|
|
return !c.match(/[\w.)\]]/);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given the ending position of a quoted string, find where it starts
|
|
|
|
function findMatchingQuote(expr, offset) {
|
|
|
|
let quoteChar = expr.charAt(offset);
|
|
|
|
for (let i = offset - 1; i >= 0; --i) {
|
2019-08-19 20:51:42 -04:00
|
|
|
if (expr.charAt(i) == quoteChar && expr.charAt(i - 1) != '\\')
|
2011-10-29 04:20:49 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given the ending position of a regex, find where it starts
|
|
|
|
function findMatchingSlash(expr, offset) {
|
|
|
|
for (let i = offset - 1; i >= 0; --i) {
|
2019-08-19 20:51:42 -04:00
|
|
|
if (expr.charAt(i) == '/' && expr.charAt(i - 1) != '\\')
|
2011-10-29 04:20:49 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If expr.charAt(offset) is ')' or ']',
|
|
|
|
// return the position of the corresponding '(' or '[' bracket.
|
|
|
|
// This function does not check for syntactic correctness. e.g.,
|
|
|
|
// findMatchingBrace("[(])", 3) returns 1.
|
|
|
|
function findMatchingBrace(expr, offset) {
|
|
|
|
let closeBrace = expr.charAt(offset);
|
2019-08-19 15:38:51 -04:00
|
|
|
let openBrace = { ')': '(', ']': '[' }[closeBrace];
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, offset - 1, openBrace, closeBrace);
|
|
|
|
}
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
function findTheBrace(expr, offset, ...braces) {
|
|
|
|
let [openBrace, closeBrace] = braces;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
if (offset < 0)
|
|
|
|
return -1;
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
if (expr.charAt(offset) == openBrace)
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
if (expr.charAt(offset).match(/['"]/))
|
|
|
|
return findTheBrace(expr, findMatchingQuote(expr, offset) - 1, ...braces);
|
|
|
|
|
|
|
|
if (expr.charAt(offset) == '/')
|
|
|
|
return findTheBrace(expr, findMatchingSlash(expr, offset) - 1, ...braces);
|
|
|
|
|
|
|
|
if (expr.charAt(offset) == closeBrace)
|
|
|
|
return findTheBrace(expr, findTheBrace(expr, offset - 1, ...braces) - 1, ...braces);
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
return findTheBrace(expr, offset - 1, ...braces);
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Walk expr backwards from offset looking for the beginning of an
|
|
|
|
// expression suitable for passing to eval.
|
|
|
|
// There is no guarantee of correct javascript syntax between the return
|
|
|
|
// value and offset. This function is meant to take a string like
|
|
|
|
// "foo(Obj.We.Are.Completing" and allow you to extract "Obj.We.Are.Completing"
|
|
|
|
function getExpressionOffset(expr, offset) {
|
|
|
|
while (offset >= 0) {
|
|
|
|
let currChar = expr.charAt(offset);
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (isStopChar(currChar))
|
2011-10-29 04:20:49 -04:00
|
|
|
return offset + 1;
|
|
|
|
|
2019-08-19 20:51:42 -04:00
|
|
|
if (currChar.match(/[)\]]/))
|
2011-10-29 04:20:49 -04:00
|
|
|
offset = findMatchingBrace(expr, offset);
|
|
|
|
|
|
|
|
--offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things with non-word characters or that start with a number
|
|
|
|
// are not accessible via .foo notation and so aren't returned
|
|
|
|
function isValidPropertyName(w) {
|
|
|
|
return !(w.match(/\W/) || w.match(/^\d/));
|
|
|
|
}
|
|
|
|
|
|
|
|
// To get all properties (enumerable and not), we need to walk
|
|
|
|
// the prototype chain ourselves
|
|
|
|
function getAllProps(obj) {
|
2019-08-19 20:51:42 -04:00
|
|
|
if (obj === null || obj === undefined)
|
2011-10-29 04:20:49 -04:00
|
|
|
return [];
|
2019-08-19 20:51:42 -04:00
|
|
|
|
2019-08-19 13:55:49 -04:00
|
|
|
return Object.getOwnPropertyNames(obj).concat(getAllProps(Object.getPrototypeOf(obj)));
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a string _expr_, returns all methods
|
|
|
|
// that can be accessed via '.' notation.
|
|
|
|
// e.g., expr="({ foo: null, bar: null, 4: null })" will
|
|
|
|
// return ["foo", "bar", ...] but the list will not include "4",
|
|
|
|
// since methods accessed with '.' notation must star with a letter or _.
|
2019-07-11 19:31:38 -04:00
|
|
|
function getPropertyNamesFromExpression(expr, commandHeader = '') {
|
2011-10-29 04:20:49 -04:00
|
|
|
let obj = {};
|
|
|
|
if (!isUnsafeExpression(expr)) {
|
|
|
|
try {
|
2019-01-29 14:36:54 -05:00
|
|
|
obj = eval(commandHeader + expr);
|
2011-10-29 04:20:49 -04:00
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let propsUnique = {};
|
2019-01-28 20:27:05 -05:00
|
|
|
if (typeof obj === 'object') {
|
2011-10-29 04:20:49 -04:00
|
|
|
let allProps = getAllProps(obj);
|
|
|
|
// Get only things we are allowed to complete following a '.'
|
2019-08-19 13:55:49 -04:00
|
|
|
allProps = allProps.filter(isValidPropertyName);
|
2011-10-29 04:20:49 -04:00
|
|
|
|
|
|
|
// Make sure propsUnique contains one key for every
|
|
|
|
// property so we end up with a unique list of properties
|
2019-08-19 16:20:35 -04:00
|
|
|
allProps.map(p => (propsUnique[p] = null));
|
2011-10-29 04:20:49 -04:00
|
|
|
}
|
|
|
|
return Object.keys(propsUnique).sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a list of words, returns the longest prefix they all have in common
|
|
|
|
function getCommonPrefix(words) {
|
|
|
|
let word = words[0];
|
|
|
|
for (let i = 0; i < word.length; i++) {
|
|
|
|
for (let w = 1; w < words.length; w++) {
|
|
|
|
if (words[w].charAt(i) != word.charAt(i))
|
|
|
|
return word.slice(0, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return word;
|
|
|
|
}
|
|
|
|
|
2019-08-19 20:24:28 -04:00
|
|
|
// Remove any blocks that are quoted or are in a regex
|
|
|
|
function removeLiterals(str) {
|
|
|
|
if (str.length == 0)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
let currChar = str.charAt(str.length - 1);
|
|
|
|
if (currChar == '"' || currChar == '\'') {
|
|
|
|
return removeLiterals(
|
|
|
|
str.slice(0, findMatchingQuote(str, str.length - 1)));
|
|
|
|
} else if (currChar == '/') {
|
|
|
|
return removeLiterals(
|
|
|
|
str.slice(0, findMatchingSlash(str, str.length - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return removeLiterals(str.slice(0, str.length - 1)) + currChar;
|
|
|
|
}
|
|
|
|
|
2011-10-29 04:20:49 -04:00
|
|
|
// Returns true if there is reason to think that eval(str)
|
|
|
|
// will modify the global scope
|
|
|
|
function isUnsafeExpression(str) {
|
|
|
|
|
|
|
|
// Check for any sort of assignment
|
|
|
|
// The strategy used is dumb: remove any quotes
|
|
|
|
// or regexs and comparison operators and see if there is an '=' character.
|
|
|
|
// If there is, it might be an unsafe assignment.
|
|
|
|
|
|
|
|
let prunedStr = removeLiterals(str);
|
2019-08-19 13:55:49 -04:00
|
|
|
prunedStr = prunedStr.replace(/[=!]==/g, ''); // replace === and !== with nothing
|
|
|
|
prunedStr = prunedStr.replace(/[=<>!]=/g, ''); // replace ==, <=, >=, != with nothing
|
2011-10-29 04:20:49 -04:00
|
|
|
|
2019-08-20 17:20:31 -04:00
|
|
|
if (prunedStr.match(/[=]/)) {
|
2011-10-29 04:20:49 -04:00
|
|
|
return true;
|
|
|
|
} else if (prunedStr.match(/;/)) {
|
|
|
|
// If we contain a semicolon not inside of a quote/regex, assume we're unsafe as well
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of global keywords derived from str
|
|
|
|
function getDeclaredConstants(str) {
|
|
|
|
let ret = [];
|
2017-10-30 20:38:18 -04:00
|
|
|
str.split(';').forEach(s => {
|
2019-01-31 09:08:00 -05:00
|
|
|
let base_, keyword;
|
2011-10-29 04:20:49 -04:00
|
|
|
let match = s.match(/const\s+(\w+)\s*=/);
|
|
|
|
if (match) {
|
2019-01-31 09:08:00 -05:00
|
|
|
[base_, keyword] = match;
|
2011-10-29 04:20:49 -04:00
|
|
|
ret.push(keyword);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|