|
28 | 28 | */ |
29 | 29 | package org.scijava.plugins.scripting.jython; |
30 | 30 |
|
| 31 | +import java.util.ArrayList; |
31 | 32 | import java.util.Collections; |
| 33 | +import java.util.Comparator; |
| 34 | +import java.util.List; |
32 | 35 | import java.util.LinkedHashMap; |
33 | 36 | import java.util.Map; |
| 37 | +import java.util.stream.Collectors; |
34 | 38 |
|
35 | 39 | import javax.script.Bindings; |
36 | 40 | import javax.script.ScriptContext; |
|
48 | 52 | import org.scijava.code.api.CodeCompleterPlugin; |
49 | 53 | import org.scijava.code.api.CodeCompletionService; |
50 | 54 | import org.scijava.code.api.CompletionRequest; |
| 55 | +import org.scijava.code.api.Completion; |
51 | 56 | import org.scijava.code.api.CompletionResult; |
| 57 | +import org.scijava.code.api.SignatureHelp; |
| 58 | +import org.scijava.code.api.Signatures; |
52 | 59 |
|
53 | 60 | /** |
54 | 61 | * Toolkit-agnostic code completion for the Jython language, contributed by the |
@@ -94,7 +101,73 @@ public CompletionResult complete(final CompletionRequest request) { |
94 | 101 | codeWithoutLastLine, lastLine, alreadyEntered, predefinedVariables( |
95 | 102 | request)); |
96 | 103 | return new CompletionResult(result.completions, replaceStart, |
97 | | - result.parameterChoices, result.typeResolver); |
| 104 | + result.parameterChoices); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public SignatureHelp signatureHelp(final CompletionRequest request) { |
| 109 | + final String text = request.text(); |
| 110 | + final int caret = request.offset(); |
| 111 | + if (request.lineToCaret().trim().startsWith("#")) return SignatureHelp.NONE; |
| 112 | + final int open = Signatures.callStart(text, caret); |
| 113 | + if (open < 0) return SignatureHelp.NONE; |
| 114 | + final List<String> args = Signatures.arguments(text, open, caret); |
| 115 | + if (args == null) return SignatureHelp.NONE; |
| 116 | + |
| 117 | + // The callee, e.g. "Math.max" or "ArrayList". |
| 118 | + final String callee = alreadyEnteredText(text.substring(text.lastIndexOf( |
| 119 | + '\n', open - 1) + 1, open)).trim(); |
| 120 | + if (callee.isEmpty()) return SignatureHelp.NONE; |
| 121 | + |
| 122 | + // Its signatures: what completion offers for it, as if the caret were |
| 123 | + // just before the "(" (methods: "Math.max"), or just after it |
| 124 | + // (constructors: offered by their class's simple name, "ArrayList"). |
| 125 | + final String simpleName = callee.substring(callee.lastIndexOf('.') + 1); |
| 126 | + JythonAutoCompletions.Result result = null; |
| 127 | + List<Completion> callables = Collections.emptyList(); |
| 128 | + for (final int at : new int[] { open, open + 1 }) { |
| 129 | + final String name = at == open ? callee : simpleName; |
| 130 | + result = completionsAt(request, at); |
| 131 | + callables = result.completions.stream().filter(c -> c.isCallable() && |
| 132 | + stripParens(c.insertionText()).equals(name)).collect(Collectors |
| 133 | + .toList()); |
| 134 | + if (!callables.isEmpty()) break; |
| 135 | + } |
| 136 | + if (callables.isEmpty()) return SignatureHelp.NONE; |
| 137 | + |
| 138 | + // The types of the arguments before the caret's (which is being typed). |
| 139 | + final List<String> argTypes = new ArrayList<>(); |
| 140 | + for (int i = 0; i < args.size(); i++) { |
| 141 | + final String arg = args.get(i).trim(); |
| 142 | + argTypes.add(i == args.size() - 1 || arg.isEmpty() ? null : result.types |
| 143 | + .apply(arg)); |
| 144 | + } |
| 145 | + // NB: Reflection lists methods in no particular order: sort them by |
| 146 | + // their parameter types, so that equally good fits come out the same. |
| 147 | + final List<Completion> sorted = new ArrayList<>(callables); |
| 148 | + sorted.sort(Comparator.comparing(c -> c.parameters().stream().map( |
| 149 | + p -> String.valueOf(p.type())).collect(Collectors.joining(",")))); |
| 150 | + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 151 | + return new SignatureHelp(Signatures.rateAll(sorted, argTypes, (a, |
| 152 | + p) -> Signatures.fit(a, p, loader)), args.size() - 1, open); |
| 153 | + } |
| 154 | + |
| 155 | + /** Computes completions as if the caret were at the given offset. */ |
| 156 | + private JythonAutoCompletions.Result completionsAt( |
| 157 | + final CompletionRequest request, final int offset) |
| 158 | + { |
| 159 | + final CompletionRequest there = new CompletionRequest(request.text(), |
| 160 | + offset, request.language(), request.engine(), request.context(), request |
| 161 | + .path()); |
| 162 | + final String textToCaret = there.textToCaret(); |
| 163 | + final String lastLine = there.lineToCaret(); |
| 164 | + return engine.completionsFor(textToCaret.substring(0, textToCaret |
| 165 | + .length() - lastLine.length()), lastLine, alreadyEnteredText(lastLine), |
| 166 | + predefinedVariables(request)); |
| 167 | + } |
| 168 | + |
| 169 | + private static String stripParens(final String name) { |
| 170 | + return name.endsWith("()") ? name.substring(0, name.length() - 2) : name; |
98 | 171 | } |
99 | 172 |
|
100 | 173 | /** |
|
0 commit comments