src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/EditObject.java
author sundar
Fri, 03 Nov 2017 19:53:09 +0530
changeset 47492 560fab171dc7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop Reviewed-by: jlaskey, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     1
/*
45950
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     4
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    10
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    15
 * accompanied this code).
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    16
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    20
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    23
 * questions.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    24
 */
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    25
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    27
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    28
import java.util.Collections;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    29
import java.util.HashSet;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    30
import java.util.Set;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    31
import java.util.function.Consumer;
45950
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
    32
import java.util.ServiceLoader;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    33
import jdk.nashorn.api.scripting.AbstractJSObject;
45950
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
    34
import jdk.internal.editor.spi.BuildInEditorProvider;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    35
import jdk.nashorn.internal.runtime.JSType;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    36
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    37
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    38
/*
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    39
 * "edit" top level script function which shows an external Window
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    40
 * for editing and evaluating scripts from it.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    41
 */
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    42
final class EditObject extends AbstractJSObject {
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    43
    private static final Set<String> props;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    44
    static {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    45
        final HashSet<String> s = new HashSet<>();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    46
        s.add("editor");
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    47
        props = Collections.unmodifiableSet(s);
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    48
    }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    49
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: 32320
diff changeset
    50
    private final Console console;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    51
    private final Consumer<String> errorHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    52
    private final Consumer<String> evaluator;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    53
    private String editor;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    54
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: 32320
diff changeset
    55
    EditObject(final Console console, final Consumer<String> errorHandler,
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: 32320
diff changeset
    56
            final Consumer<String> 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: 32320
diff changeset
    57
        this.console = console;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    58
        this.errorHandler = errorHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    59
        this.evaluator = evaluator;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    60
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    61
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    62
    @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    63
    public Object getDefaultValue(final Class<?> hint) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    64
        if (hint == String.class) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    65
            return toString();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    66
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    67
        return UNDEFINED;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    68
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    69
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    70
    @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    71
    public String toString() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    72
        return "function edit() { [native code] }";
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    73
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    74
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    75
    @Override
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    76
    public Set<String> keySet() {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    77
        return props;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    78
    }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    79
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    80
    @Override
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    81
    public Object getMember(final String name) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    82
        if (name.equals("editor")) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    83
            return editor;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    84
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    85
        return UNDEFINED;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    86
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    87
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    88
    @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    89
    public void setMember(final String name, final Object value) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    90
        if (name.equals("editor")) {
32320
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32314
diff changeset
    91
            this.editor = value != null && value != UNDEFINED? JSType.toString(value) : "";
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    92
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    93
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    94
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    95
    // called whenever user 'saves' script in editor
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    96
    class SaveHandler implements Consumer<String> {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    97
         private String lastStr; // last seen code
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    98
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    99
         SaveHandler(final String str) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   100
             this.lastStr = str;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   101
         }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   102
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   103
         @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   104
         public void accept(final String str) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   105
             // ignore repeated save of the same code!
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   106
             if (! str.equals(lastStr)) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   107
                 this.lastStr = str;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   108
                 // evaluate the new code
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   109
                 evaluator.accept(str);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   110
             }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   111
         }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   112
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   113
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   114
    @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   115
    public Object call(final Object thiz, final Object... args) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   116
        final String initText = args.length > 0? JSType.toString(args[0]) : "";
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   117
        final SaveHandler saveHandler = new SaveHandler(initText);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   118
        if (editor != null && !editor.isEmpty()) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   119
            ExternalEditor.edit(editor, errorHandler, initText, saveHandler, console);
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   120
        } else {
45950
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   121
            try {
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   122
                ServiceLoader<BuildInEditorProvider> sl
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   123
                        = ServiceLoader.load(BuildInEditorProvider.class);
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   124
                //find the highest ranking provider
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   125
                BuildInEditorProvider provider = null;
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   126
                for (BuildInEditorProvider p : sl){
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   127
                    if (provider == null || p.rank() > provider.rank()) {
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   128
                        provider = p;
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   129
                    }
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   130
                }
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   131
                if (provider != null) {
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   132
                    provider.edit(null, initText, saveHandler, errorHandler);
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   133
                } else {
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   134
                    errorHandler.accept(Main.getMessage("jjs.err.no.builtin.editor"));
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   135
                }
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   136
            } catch (RuntimeException ex) {
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   137
                errorHandler.accept(Main.getMessage("jjs.err.cant.launch.editor"));
720bc0f5ba9d 8180727: Use jdk.editpad to replace jdk.nashorn.tools.jjs.EditPad duplicated class
sdama
parents: 32324
diff changeset
   138
            }
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   139
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   140
        return UNDEFINED;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   141
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   142
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   143
    @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   144
    public boolean isFunction() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   145
        return true;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   146
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   147
}