src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/HistoryObject.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 52938 5ff7480c9e28
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     1
/*
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     4
 *
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    10
 *
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    15
 * accompanied this code).
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    16
 *
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    20
 *
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    23
 * questions.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    24
 */
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    25
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    27
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    28
import java.io.BufferedReader;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    29
import java.io.File;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    30
import java.io.FileReader;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    31
import java.io.IOException;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    32
import java.io.PrintWriter;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    33
import java.io.UncheckedIOException;
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    34
import java.util.Collections;
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    35
import java.util.HashSet;
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    36
import java.util.Set;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    37
import java.util.function.Consumer;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    38
import java.util.function.Function;
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
    39
import java.util.function.Supplier;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    40
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    41
import jdk.internal.org.jline.reader.History;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    42
import jdk.nashorn.api.scripting.AbstractJSObject;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    43
import jdk.nashorn.api.scripting.JSObject;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    44
import jdk.nashorn.internal.runtime.JSType;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    45
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    46
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    47
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    48
/*
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    49
 * A script friendly object that exposes history of commands to scripts.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    50
 */
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    51
final class HistoryObject extends AbstractJSObject {
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    52
    private static final Set<String> props;
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    53
    static {
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    54
        final HashSet<String> s = new HashSet<>();
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    55
        s.add("clear");
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    56
        s.add("forEach");
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    57
        s.add("load");
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    58
        s.add("print");
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    59
        s.add("save");
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    60
        s.add("size");
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
    61
        s.add("toString");
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    62
        props = Collections.unmodifiableSet(s);
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    63
    }
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
    64
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32324
diff changeset
    65
    private final History hist;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    66
    private final PrintWriter err;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    67
    private final Consumer<String> evaluator;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    68
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 32324
diff changeset
    69
    HistoryObject(final History hist, final PrintWriter err,
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    70
            final Consumer<String> evaluator) {
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
    71
        this.hist = hist;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    72
        this.err = err;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    73
        this.evaluator = evaluator;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    74
    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    75
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    76
    @Override
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    77
    public boolean isFunction() {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    78
        return true;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    79
    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    80
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    81
    @Override
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    82
    public Object call(final Object thiz, final Object... args) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    83
        if (args.length > 0) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    84
            int index = JSType.toInteger(args[0]);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    85
            if (index < 0) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    86
                index += (hist.size() - 1);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    87
            } else {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    88
                index--;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    89
            }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    90
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    91
            if (index >= 0 && index < (hist.size() - 1)) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    92
                final CharSequence src = hist.get(index);
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    93
                var it = hist.iterator();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    94
                while (it.hasNext()) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    95
                    it.next();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    96
                }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    97
                it.remove();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    98
                hist.add(src.toString());
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
    99
                err.println(src);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   100
                evaluator.accept(src.toString());
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   101
            } else {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   102
                var it = hist.iterator();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   103
                while (it.hasNext()) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   104
                    it.next();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   105
                }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   106
                it.remove();
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   107
                err.println("no history entry @ " + (index + 1));
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   108
            }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   109
        }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   110
        return UNDEFINED;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   111
    }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   112
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   113
    @Override
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   114
    public Object getMember(final String name) {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   115
        switch (name) {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   116
            case "clear":
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   117
                return (Runnable) () -> {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   118
                    try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   119
                    hist.purge();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   120
                    } catch (IOException ex) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   121
                        throw new UncheckedIOException(ex);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   122
                    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   123
                };
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   124
            case "forEach":
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   125
                return (Function<JSObject, Object>)this::iterate;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   126
            case "load":
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   127
                return (Consumer<Object>)this::load;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   128
            case "print":
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   129
                return (Runnable)this::print;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   130
            case "save":
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   131
                return (Consumer<Object>)this::save;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   132
            case "size":
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   133
                return hist.size();
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   134
            case "toString":
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   135
                return (Supplier<String>)this::toString;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   136
        }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   137
        return UNDEFINED;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   138
    }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   139
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   140
    @Override
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   141
    public Object getDefaultValue(final Class<?> hint) {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   142
        if (hint == String.class) {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   143
            return toString();
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   144
        }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   145
        return UNDEFINED;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   146
    }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   147
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   148
    @Override
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   149
    public String toString() {
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   150
        final StringBuilder buf = new StringBuilder();
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   151
        for (History.Entry e : hist) {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   152
            buf.append(e.line()).append('\n');
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   153
        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32245
diff changeset
   154
        return buf.toString();
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   155
    }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   156
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   157
    @Override
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   158
    public Set<String> keySet() {
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   159
        return props;
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   160
    }
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   161
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   162
    private void save(final Object obj) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   163
        final File file = getFile(obj);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   164
        try (final PrintWriter pw = new PrintWriter(file)) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   165
            for (History.Entry e : hist) {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   166
                pw.println(e.line());
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   167
            }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   168
        } catch (final IOException exp) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   169
            throw new RuntimeException(exp);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   170
        }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   171
    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   172
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   173
    private void load(final Object obj) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   174
        final File file = getFile(obj);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   175
        String item = null;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   176
        try (final BufferedReader r = new BufferedReader(new FileReader(file))) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   177
            while ((item = r.readLine()) != null) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   178
                hist.add(item);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   179
            }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   180
        } catch (final IOException exp) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   181
            throw new RuntimeException(exp);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   182
        }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   183
    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   184
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   185
    private void print() {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   186
        for (History.Entry e : hist) {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   187
            System.out.printf("%3d %s\n", e.index() + 1, e.line());
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   188
        }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   189
    }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   190
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   191
    private Object iterate(final JSObject func) {
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   192
        for (History.Entry e : hist) {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   193
            if (JSType.toBoolean(func.call(this, e.line().toString()))) {
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   194
                break; // return true from callback to skip iteration
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   195
            }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   196
        }
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   197
        return UNDEFINED;
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   198
    }
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   199
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   200
    private static File getFile(final Object obj) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   201
        File file = null;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   202
        if (obj instanceof String) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   203
            file = new File((String)obj);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   204
        } else if (obj instanceof File) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   205
            file = (File)obj;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   206
        } else {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   207
            throw typeError("not.a.file", JSType.toString(obj));
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   208
        }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   209
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   210
        return file;
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32317
diff changeset
   211
    }
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents:
diff changeset
   212
}