function superHint()

in ui-modules/utils/yaml-editor/addon/hint/super-hint-yaml-schema.js [64:128]


function superHint(schemaMatcher, baseSchema, cm, options) {
    return new Promise((resolve, reject) => {
        let cursor = cm.getCursor();
        let currentText = cm.getLine(cursor.line);

        let separatorMatch = currentText.match(/:\s+/);
        let isKey = separatorMatch === null || cursor.ch < separatorMatch['index'] + separatorMatch[0].length;

        let start = cursor.ch;
        while (start && !/\s/.test(currentText.charAt(start - 1))) {
            --start;
        }
        let keyword = currentText.slice(start, cursor.ch);

        let hints = {
            list: [],
            from: CodeMirror.Pos(cursor.line, start),
            to: cursor
        };

        if (!isKey) {
            resolve(hints);
        } else {
            try {
                let levels = new Set();
                let cursor = cm.getCursor();
                let line = cursor.line;
                let prevLine = cursor.line > 0 ? cursor.line - 1 : 0;

                while (!levels.has(0) && line > 0) {
                    levels.add(cm.getLine(line).match(/^([\s\-]*)/)[1].length);
                    line--;
                }

                levels = Array.from(levels).sort((a, b) => (a - b));

                let level = cm.getRange(CodeMirror.Pos(cursor.line, 0), cursor).match(/^([\s\-]*)/)[1].length;
                if (levels.indexOf(level) > -1) {
                    level = levels.indexOf(level);
                }
                let json = jsyaml.safeLoad(cm.getRange(CodeMirror.Pos(0, 0), CodeMirror.Pos(prevLine, cm.getLine(prevLine).length)));

                schemaMatcher.findProperties(json, JSON.parse(baseSchema), level).then(properties => {
                    resolve(Object.assign(hints, {
                        list: properties
                            .filter(property => keyword.length < 1 || property.$key.indexOf(keyword) > -1)
                            .map(property => {
                                let helper = ['pattern', 'minItems', 'maxItems', 'enum'].reduce((helper, prop) => {
                                    if (property.hasOwnProperty(prop)) {
                                        helper.push(`${prop}: ${property[prop]}`);
                                    }
                                    return helper;
                                }, []).join(';');
                                return cm.superHint(`${property.$key}: `, property.$key, `(${property.type}) ${helper}`, property.description);
                            })
                    }));
                }).catch(ex => {
                    reject(new Error(`Cannot retrieve suggestions: ${ex.message}`));
                });
            } catch (ex) {
                resolve(hints);
            }
        }
    });
}