Search in sources :

Example 1 with Completion

use of com.github.anba.es6draft.repl.console.ShellConsole.Completion in project es6draft by anba.

the class ShellCompleter method complete.

@Override
public Optional<Completion> complete(String line, int cursor) {
    ExecutionContext cx = realm.defaultContext();
    ScriptObject object = realm.getGlobalThis();
    String leftContext = line.substring(0, cursor);
    if (leftContext.isEmpty()) {
        ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), "", "");
        return Optional.of(new Completion(line, 0, cursor, candidates));
    }
    Matcher m = hierarchyPattern.matcher(leftContext);
    if (!m.find()) {
        return Optional.empty();
    }
    ArrayList<String> segments = segments(m.group(1));
    StringBuilder prefix = new StringBuilder();
    List<String> properties = segments.subList(0, segments.size() - 1);
    if (!properties.isEmpty() && "this".equals(properties.get(0))) {
        // skip leading `this` segment in property traversal
        properties = properties.subList(1, properties.size());
        prefix.append("this.");
    }
    for (String property : properties) {
        if (!HasProperty(cx, object, property)) {
            return Optional.empty();
        }
        Object value = Get(cx, object, property);
        if (Type.isObject(value)) {
            object = Type.objectValue(value);
        } else if (!Type.isUndefinedOrNull(value)) {
            object = ToObject(cx, value);
        } else {
            return Optional.empty();
        }
        prefix.append(property).append('.');
    }
    String partial = segments.get(segments.size() - 1);
    ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), partial, prefix.toString());
    return Optional.of(new Completion(line, m.start(1), cursor, candidates));
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Completion(com.github.anba.es6draft.repl.console.ShellConsole.Completion) Matcher(java.util.regex.Matcher) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Aggregations

Completion (com.github.anba.es6draft.repl.console.ShellConsole.Completion)1 ToObject (com.github.anba.es6draft.runtime.AbstractOperations.ToObject)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 Matcher (java.util.regex.Matcher)1