|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.script; |
| 33 | + |
| 34 | +import java.lang.reflect.Field; |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.Comparator; |
| 40 | +import java.util.List; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +import javax.script.Bindings; |
| 44 | +import javax.script.ScriptContext; |
| 45 | +import javax.script.ScriptEngine; |
| 46 | + |
| 47 | +/** |
| 48 | + * Abstract base class for {@link AutoCompleter} implementations. |
| 49 | + * |
| 50 | + * @author Hadrien Mary |
| 51 | + */ |
| 52 | +public abstract class AbstractAutoCompleter implements AutoCompleter { |
| 53 | + |
| 54 | + @SuppressWarnings("unused") |
| 55 | + private ScriptLanguage scriptLanguage; |
| 56 | + |
| 57 | + public AbstractAutoCompleter(final ScriptLanguage scriptLanguage) { |
| 58 | + this.scriptLanguage = scriptLanguage; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public AutoCompletionResult autocomplete(final String code, final int index, |
| 63 | + final ScriptEngine engine) |
| 64 | + { |
| 65 | + |
| 66 | + final List<String> matches = new ArrayList<>(); |
| 67 | + final int startIndex = 0; |
| 68 | + |
| 69 | + if (code.endsWith(".")) { |
| 70 | + // Autocompletion with all the attributes of the object |
| 71 | + matches.addAll(engineAttributesCompleter(code, index, engine)); |
| 72 | + |
| 73 | + } |
| 74 | + else if (code.contains(".")) { |
| 75 | + final List<String> codeList = Arrays.asList(code.split("\\.")); |
| 76 | + final String objectString = codeList.get(codeList.size() - 2); |
| 77 | + final String prefix = codeList.get(codeList.size() - 1); |
| 78 | + matches.addAll(engineAttributesCompleter(objectString + ".", |
| 79 | + prefix, index, engine)); |
| 80 | + |
| 81 | + } |
| 82 | + else { |
| 83 | + // Autocompletion with variables in the engine scope |
| 84 | + matches.addAll(engineVariablesCompleter(code, index, engine)); |
| 85 | + } |
| 86 | + |
| 87 | + // Remove duplicates |
| 88 | + final List<String> unique = // |
| 89 | + matches.stream().distinct().collect(Collectors.toList()); |
| 90 | + |
| 91 | + // Sort alphabetically, ignoring case |
| 92 | + Collections.sort(unique, new Comparator<Object>() { |
| 93 | + |
| 94 | + @Override |
| 95 | + public int compare(final Object o1, final Object o2) { |
| 96 | + final String s1 = (String) o1; |
| 97 | + final String s2 = (String) o2; |
| 98 | + return s1.toLowerCase().compareTo(s2.toLowerCase()); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + // Return results. For now we ignore index and startIndex. |
| 103 | + return new AutoCompletionResult(unique, startIndex); |
| 104 | + } |
| 105 | + |
| 106 | + private List<String> engineVariablesCompleter(final String code, |
| 107 | + @SuppressWarnings("unused") final int index, final ScriptEngine engine) |
| 108 | + { |
| 109 | + final List<String> matches = new ArrayList<>(); |
| 110 | + |
| 111 | + final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); |
| 112 | + |
| 113 | + for (final String key : bindings.keySet()) { |
| 114 | + if (key.toLowerCase().startsWith(code.toLowerCase())) { |
| 115 | + matches.add(key); |
| 116 | + } |
| 117 | + } |
| 118 | + return matches; |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + private List<String> engineAttributesCompleter(final String objectString, |
| 123 | + final int index, final ScriptEngine engine) |
| 124 | + { |
| 125 | + return engineAttributesCompleter(objectString, "", index, engine); |
| 126 | + } |
| 127 | + |
| 128 | + private List<String> engineAttributesCompleter(final String objectString, |
| 129 | + final String prefix, @SuppressWarnings("unused") final int index, |
| 130 | + final ScriptEngine engine) |
| 131 | + { |
| 132 | + final List<String> matches = new ArrayList<>(); |
| 133 | + final String lPrefix = prefix.toLowerCase(); |
| 134 | + |
| 135 | + final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); |
| 136 | + |
| 137 | + for (final String key : bindings.keySet()) { |
| 138 | + if (objectString.endsWith(key + ".")) { |
| 139 | + final Object obj = bindings.get(key); |
| 140 | + // check for public field completions |
| 141 | + for (final Field field : obj.getClass().getFields()) { |
| 142 | + if (field.getName().toLowerCase().startsWith(lPrefix)) { |
| 143 | + matches.add(objectString + field.getName()); |
| 144 | + } |
| 145 | + } |
| 146 | + // check for public method completions |
| 147 | + for (final Method method : obj.getClass().getMethods()) { |
| 148 | + if (method.getName().toLowerCase().startsWith(lPrefix)) { |
| 149 | + matches.add(objectString + method.getName() + "("); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return matches; |
| 156 | + } |
| 157 | +} |
0 commit comments