■ ■ ■ ■ ■ ■
jadx-gui/src/main/java/jadx/gui/plugins/script/JadxScriptCompleteProvider.java
| 1 | + | package jadx.gui.plugins.script; |
| 2 | + | |
| 3 | + | import java.awt.Point; |
| 4 | + | import java.util.ArrayList; |
| 5 | + | import java.util.Collections; |
| 6 | + | import java.util.HashMap; |
| 7 | + | import java.util.List; |
| 8 | + | import java.util.Map; |
| 9 | + | import java.util.Objects; |
| 10 | + | import java.util.stream.Collectors; |
| 11 | + | |
| 12 | + | import javax.swing.Icon; |
| 13 | + | import javax.swing.text.BadLocationException; |
| 14 | + | import javax.swing.text.JTextComponent; |
| 15 | + | |
| 16 | + | import org.fife.ui.autocomplete.Completion; |
| 17 | + | import org.fife.ui.autocomplete.CompletionProviderBase; |
| 18 | + | import org.fife.ui.autocomplete.ParameterizedCompletion; |
| 19 | + | import org.slf4j.Logger; |
| 20 | + | import org.slf4j.LoggerFactory; |
| 21 | + | |
| 22 | + | import kotlin.script.experimental.api.ScriptDiagnostic; |
| 23 | + | import kotlin.script.experimental.api.SourceCode; |
| 24 | + | import kotlin.script.experimental.api.SourceCodeCompletionVariant; |
| 25 | + | |
| 26 | + | import jadx.core.utils.ListUtils; |
| 27 | + | import jadx.core.utils.exceptions.JadxRuntimeException; |
| 28 | + | import jadx.gui.ui.codearea.AbstractCodeArea; |
| 29 | + | import jadx.gui.utils.Icons; |
| 30 | + | import jadx.plugins.script.ide.JadxScriptAutoComplete; |
| 31 | + | import jadx.plugins.script.ide.ScriptCompletionResult; |
| 32 | + | |
| 33 | + | import static jadx.plugins.script.ide.CompleteKt.AUTO_COMPLETE_INSERT_STR; |
| 34 | + | |
| 35 | + | public class JadxScriptCompleteProvider extends CompletionProviderBase { |
| 36 | + | private static final Logger LOG = LoggerFactory.getLogger(JadxScriptCompleteProvider.class); |
| 37 | + | |
| 38 | + | private static final Map<String, Icon> ICONS_MAP = buildIconsMap(); |
| 39 | + | |
| 40 | + | private static Map<String, Icon> buildIconsMap() { |
| 41 | + | Map<String, Icon> map = new HashMap<>(); |
| 42 | + | map.put("class", Icons.CLASS); |
| 43 | + | map.put("method", Icons.METHOD); |
| 44 | + | map.put("field", Icons.FIELD); |
| 45 | + | map.put("property", Icons.PROPERTY); |
| 46 | + | map.put("parameter", Icons.PARAMETER); |
| 47 | + | map.put("package", Icons.PACKAGE); |
| 48 | + | return map; |
| 49 | + | } |
| 50 | + | |
| 51 | + | private final AbstractCodeArea codeArea; |
| 52 | + | |
| 53 | + | public JadxScriptCompleteProvider(AbstractCodeArea codeArea) { |
| 54 | + | this.codeArea = codeArea; |
| 55 | + | } |
| 56 | + | |
| 57 | + | private List<Completion> getCompletions() { |
| 58 | + | try { |
| 59 | + | String code = codeArea.getText(); |
| 60 | + | int caretPos = codeArea.getCaretPosition(); |
| 61 | + | JadxScriptAutoComplete scriptComplete = new JadxScriptAutoComplete(codeArea.getNode().getName()); |
| 62 | + | ScriptCompletionResult result = scriptComplete.complete(code, caretPos); |
| 63 | + | int replacePos = getReplacePos(caretPos, result); |
| 64 | + | if (!result.getReports().isEmpty()) { |
| 65 | + | LOG.debug("Script completion reports: {}", result.getReports()); |
| 66 | + | } |
| 67 | + | return convertCompletions(result.getCompletions(), code, replacePos); |
| 68 | + | } catch (Exception e) { |
| 69 | + | LOG.error("Code completion failed", e); |
| 70 | + | return Collections.emptyList(); |
| 71 | + | } |
| 72 | + | } |
| 73 | + | |
| 74 | + | private List<Completion> convertCompletions(List<SourceCodeCompletionVariant> completions, String code, int replacePos) { |
| 75 | + | if (completions.isEmpty()) { |
| 76 | + | return Collections.emptyList(); |
| 77 | + | } |
| 78 | + | if (LOG.isDebugEnabled()) { |
| 79 | + | String cmplStr = completions.stream().map(SourceCodeCompletionVariant::toString).collect(Collectors.joining("\n")); |
| 80 | + | LOG.debug("Completions:\n{}", cmplStr); |
| 81 | + | } |
| 82 | + | int count = completions.size(); |
| 83 | + | List<Completion> list = new ArrayList<>(count); |
| 84 | + | for (int i = 0; i < count; i++) { |
| 85 | + | SourceCodeCompletionVariant c = completions.get(i); |
| 86 | + | if (Objects.equals(c.getIcon(), "keyword")) { |
| 87 | + | // too many, not very useful |
| 88 | + | continue; |
| 89 | + | } |
| 90 | + | |
| 91 | + | JadxScriptCompletion cmpl = new JadxScriptCompletion(this, count - i); |
| 92 | + | cmpl.setData(c.getText(), code, replacePos); |
| 93 | + | if (Objects.equals(c.getIcon(), "method") && !Objects.equals(c.getText(), c.getDisplayText())) { |
| 94 | + | // add method args details for methods |
| 95 | + | cmpl.setSummary(c.getDisplayText() + " " + c.getTail()); |
| 96 | + | } else { |
| 97 | + | cmpl.setSummary(c.getTail()); |
| 98 | + | } |
| 99 | + | cmpl.setToolTip(c.getDisplayText()); |
| 100 | + | Icon icon = ICONS_MAP.get(c.getIcon()); |
| 101 | + | cmpl.setIcon(icon != null ? icon : Icons.FILE); |
| 102 | + | list.add(cmpl); |
| 103 | + | } |
| 104 | + | return list; |
| 105 | + | } |
| 106 | + | |
| 107 | + | private int getReplacePos(int caretPos, ScriptCompletionResult result) throws BadLocationException { |
| 108 | + | int lineRaw = codeArea.getLineOfOffset(caretPos); |
| 109 | + | int lineStart = codeArea.getLineStartOffset(lineRaw); |
| 110 | + | int line = lineRaw + 1; |
| 111 | + | int col = caretPos - lineStart + 1; |
| 112 | + | |
| 113 | + | List<ScriptDiagnostic> reports = result.getReports(); |
| 114 | + | ScriptDiagnostic cmplReport = ListUtils.filterOnlyOne(reports, r -> { |
| 115 | + | if (r.getSeverity() == ScriptDiagnostic.Severity.ERROR && r.getLocation() != null) { |
| 116 | + | SourceCode.Position start = r.getLocation().getStart(); |
| 117 | + | return start.getLine() == line && r.getMessage().endsWith(AUTO_COMPLETE_INSERT_STR); |
| 118 | + | } |
| 119 | + | return false; |
| 120 | + | }); |
| 121 | + | if (cmplReport == null) { |
| 122 | + | LOG.warn("Failed to find completion report in: {}", reports); |
| 123 | + | return caretPos; |
| 124 | + | } |
| 125 | + | reports.remove(cmplReport); |
| 126 | + | int reportCol = Objects.requireNonNull(cmplReport.getLocation()).getStart().getCol(); |
| 127 | + | return caretPos - (col - reportCol); |
| 128 | + | } |
| 129 | + | |
| 130 | + | @Override |
| 131 | + | public String getAlreadyEnteredText(JTextComponent comp) { |
| 132 | + | try { |
| 133 | + | int pos = codeArea.getCaretPosition(); |
| 134 | + | return codeArea.getText(0, pos); |
| 135 | + | } catch (Exception e) { |
| 136 | + | throw new JadxRuntimeException("Failed to get text before caret", e); |
| 137 | + | } |
| 138 | + | } |
| 139 | + | |
| 140 | + | @Override |
| 141 | + | public List<Completion> getCompletionsAt(JTextComponent comp, Point p) { |
| 142 | + | return getCompletions(); |
| 143 | + | } |
| 144 | + | |
| 145 | + | @Override |
| 146 | + | protected List<Completion> getCompletionsImpl(JTextComponent comp) { |
| 147 | + | return getCompletions(); |
| 148 | + | } |
| 149 | + | |
| 150 | + | @Override |
| 151 | + | public List<ParameterizedCompletion> getParameterizedCompletions(JTextComponent tc) { |
| 152 | + | return null; |
| 153 | + | } |
| 154 | + | } |
| 155 | + | |