|
31 | 31 | package org.scijava.script.autocompletion; |
32 | 32 |
|
33 | 33 | import java.util.ArrayList; |
34 | | -import java.util.HashMap; |
35 | | -import java.util.Map; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Comparator; |
| 36 | +import java.util.List; |
| 37 | +import javax.script.Bindings; |
| 38 | +import javax.script.ScriptContext; |
36 | 39 | import javax.script.ScriptEngine; |
| 40 | +import org.scijava.script.ScriptLanguage; |
37 | 41 |
|
38 | 42 | /** |
39 | 43 | * |
40 | 44 | * @author Hadrien Mary |
41 | 45 | */ |
42 | 46 | public abstract class AbstractAutoCompleter implements AutoCompleter { |
43 | 47 |
|
44 | | - protected ScriptEngine engine = null; |
| 48 | + protected ScriptLanguage scriptLanguage = null; |
45 | 49 |
|
46 | | - public AbstractAutoCompleter(ScriptEngine engine) { |
47 | | - this.engine = engine; |
| 50 | + public AbstractAutoCompleter(ScriptLanguage scriptLanguage) { |
| 51 | + this.scriptLanguage = scriptLanguage; |
48 | 52 | } |
49 | 53 |
|
50 | 54 | @Override |
51 | | - public Map<String, Object> autocomplete(String code) { |
52 | | - return autocomplete(code, 0); |
| 55 | + public AutoCompletionResult autocomplete(String code, ScriptEngine engine) { |
| 56 | + return autocomplete(code, 0, engine); |
53 | 57 | } |
54 | | - |
| 58 | + |
55 | 59 | @Override |
56 | | - public Map<String, Object> autocomplete(String code, int i) { |
57 | | - Map<String, Object> result = new HashMap<>(); |
58 | | - result.put("matches", new ArrayList<>()); |
59 | | - result.put("startIndex", 0); |
60 | | - return result; |
| 60 | + public AutoCompletionResult autocomplete(String code, int index, ScriptEngine engine) { |
| 61 | + |
| 62 | + List<String> matches = new ArrayList<>(); |
| 63 | + int startIndex = 0; |
| 64 | + |
| 65 | + // Naive autocompletion with variables in the engine scope |
| 66 | + matches.addAll(engineVariablesCompleter(code, index, engine)); |
| 67 | + |
| 68 | + // Sort matches alphabetcially |
| 69 | + Collections.sort(matches, new SortIgnoreCase()); |
| 70 | + |
| 71 | + // Return results. For now we ignore index and startIndex. |
| 72 | + return new AutoCompletionResult(matches, startIndex); |
61 | 73 | } |
62 | 74 |
|
63 | | - @Override |
64 | | - public ScriptEngine getScriptEngine() { |
65 | | - return this.engine; |
| 75 | + private List<String> engineVariablesCompleter(String code, int index, ScriptEngine engine) { |
| 76 | + List<String> matches = new ArrayList<>(); |
| 77 | + |
| 78 | + Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); |
| 79 | + |
| 80 | + for (String key : bindings.keySet()) { |
| 81 | + if (key.toLowerCase().startsWith(code.toLowerCase())) { |
| 82 | + matches.add(key); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return matches; |
| 87 | + } |
| 88 | + |
| 89 | + public class SortIgnoreCase implements Comparator<Object> { |
| 90 | + |
| 91 | + @Override |
| 92 | + public int compare(Object o1, Object o2) { |
| 93 | + String s1 = (String) o1; |
| 94 | + String s2 = (String) o2; |
| 95 | + return s1.toLowerCase().compareTo(s2.toLowerCase()); |
| 96 | + } |
66 | 97 | } |
67 | 98 | } |
0 commit comments