Modules (188)

HintUtils

Description

Dependencies

Functions

annotateKeywords

Annotate a list of tokens as keywords

keyword Array.<Object>
list of keyword tokens
Returns: Array.<Object>
- the input array; to each object in the array a new keyword {boolean} property has been added to indicate that the hint is a keyword.
    function annotateKeywords(keywords) {
        return keywords.map(function (t) {
            t.keyword = true;
            t.origin = "ecmascript";
            return t;
        });
    }

    function isSupportedLanguage(languageId) {
        return SUPPORTED_LANGUAGES.indexOf(languageId) !== -1;
    }

    var KEYWORD_NAMES   = [
        "break", "case", "catch", "class", "const", "continue", "debugger",
        "default", "delete", "do", "else", "export", "extends", "finally",
        "for", "function", "if", "import", "in", "instanceof", "let", "new",
        "return", "super", "switch", "this", "throw", "try", "typeof", "var",
        "void", "while", "with", "yield"
    ],
        KEYWORD_TOKENS  = KEYWORD_NAMES.map(function (t) {
            return makeToken(t, []);
        }),
        KEYWORDS        = annotateKeywords(KEYWORD_TOKENS);

    var LITERAL_NAMES   = [
        "true", "false", "null"
    ],
        LITERAL_TOKENS  = LITERAL_NAMES.map(function (t) {
            return makeToken(t, []);
        }),
        LITERALS        = annotateLiterals(LITERAL_TOKENS);

    exports.makeToken                   = makeToken;
    exports.hintable                    = hintable;
    exports.hintableKey                 = hintableKey;
    exports.maybeIdentifier             = maybeIdentifier;
    exports.eventName                   = eventName;
    exports.annotateLiterals            = annotateLiterals;
    exports.isSupportedLanguage         = isSupportedLanguage;
    exports.KEYWORDS                    = KEYWORDS;
    exports.LITERALS                    = LITERALS;
    exports.LANGUAGE_ID                 = LANGUAGE_ID;
    exports.SINGLE_QUOTE                = SINGLE_QUOTE;
    exports.DOUBLE_QUOTE                = DOUBLE_QUOTE;
    exports.SUPPORTED_LANGUAGES         = SUPPORTED_LANGUAGES;
});
Public API

annotateLiterals

Annotate a list of tokens as literals of a particular kind; if string literals, annotate with an appropriate delimiter.

literals Array.<Object>
list of hint tokens
kind string
the kind of literals in the list (e.g., "string")
Returns: Array.<Object>
- the input array; to each object in the array a new literal {boolean} property has been added to indicate that it is a literal hint, and also a new kind {string} property to indicate the literal kind. For string literals, a delimiter property is also added to indicate what the default delimiter should be (viz. a single or double quotation mark).
    function annotateLiterals(literals, kind) {
        return literals.map(function (t) {
            t.literal = true;
            t.kind = kind;
            t.origin = "ecmascript";
            if (kind === "string") {
                if (/[^\\]"/.test(t.value)) {
                    t.delimiter = SINGLE_QUOTE;
                } else {
                    t.delimiter = DOUBLE_QUOTE;
                }
            }
            return t;
        });
    }
Public API

eventName

Get a JS-hints-specific event name. Used to prevent event namespace pollution.

name string
the unqualified event name
Returns: string
- the qualified event name
    function eventName(name) {
        var EVENT_TAG = "brackets-js-hints";
        return name + "." + EVENT_TAG;
    }
Public API

hintable

Is the token's class hintable? (A very conservative test.)

token Object
the token to test for hintability
Returns: boolean
- could the token be hintable?
    function hintable(token) {

        function _isInsideRegExp(token) {
            return token.state && (token.state.lastType === "regexp" ||
                   (token.state.localState && token.state.localState.lastType === "regexp"));
        }

        switch (token.type) {
        case "comment":
        case "number":
        case "regexp":
        case "string":
        case "def":     // exclude variable & param decls
            return false;
        case "string-2":
            // exclude strings inside a regexp
            return !_isInsideRegExp(token);
        default:
            return true;
        }
    }
Public API

hintableKey

Determine if hints should be displayed for the given key.

key string
key entered by the user
showOnDot boolean
show hints on dot (".").
Returns: boolean
true if the hints should be shown for the key, false otherwise.
    function hintableKey(key, showOnDot) {
        return (key === null || (showOnDot && key === ".") || maybeIdentifier(key));
    }
Public API

makeToken

Create a hint token with name value that occurs at the given list of positions.

value string
name of the new hint token
positions nullable optional ?Array.<number>
optional list of positions at which the token occurs
Returns: Object
- a new hint token
    function makeToken(value, positions) {
        positions = positions || [];

        return {
            value: value,
            positions: positions
        };
    }
Public API

maybeIdentifier

Is the string key perhaps a valid JavaScript identifier?

key string
string to test.
Returns: boolean
- could key be a valid identifier?
    function maybeIdentifier(key) {
        var result = false,
            i;

        for (i = 0; i < key.length; i++) {
            result = Acorn.isIdentifierChar(key.charCodeAt(i));
            if (!result) {
                break;
            }
        }

        return result;
    }