|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Script Editor and Interpreter for SciJava script languages. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2022 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | +package org.scijava.plugins.scripteditor.jython; |
| 30 | + |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
| 34 | + |
| 35 | +import javax.swing.text.BadLocationException; |
| 36 | + |
| 37 | +import org.fife.ui.autocomplete.AutoCompletion; |
| 38 | +import org.fife.ui.autocomplete.Completion; |
| 39 | +import org.fife.ui.autocomplete.CompletionProvider; |
| 40 | +import org.scijava.ui.swing.script.EditorPane; |
| 41 | +import org.scijava.ui.swing.script.autocompletion.ImportCompletion; |
| 42 | + |
| 43 | +public class JythonAutoCompletion extends AutoCompletion { |
| 44 | + |
| 45 | + public JythonAutoCompletion(final CompletionProvider provider) { |
| 46 | + super(provider); |
| 47 | + this.setShowDescWindow(true); |
| 48 | + } |
| 49 | + |
| 50 | + static private final Pattern importPattern = Pattern.compile("^(from[ \\t]+([a-zA-Z_][a-zA-Z0-9._]*)[ \\t]+|)import[ \\t]+([a-zA-Z_][a-zA-Z0-9_]*[ \\ta-zA-Z0-9_,]*)[ \\t]*([\\\\]*|)[ \\t]*(#.*|)$"), |
| 51 | + tripleQuotePattern = Pattern.compile("\"\"\""), |
| 52 | + variableDeclarationPattern = Pattern.compile("([a-zA-Z_][a-zA-Z0-9._]*)[ \\t]*=[ \\t]*([A-Z_][a-zA-Z0-9._]*)(?:\\()"); // E.g., in 'imp=ImagePlus()' group1: imp; group2: ImagePlus |
| 53 | + |
| 54 | + static public class Import { |
| 55 | + final public String className, |
| 56 | + alias; // same as simple class name, or the alias from "as <alias>" |
| 57 | + final public int lineNumber; |
| 58 | + |
| 59 | + public Import(final String className, final String alias, final int lineNumber) { |
| 60 | + this.className = className; |
| 61 | + this.alias = null != alias ? alias : className.substring(className.lastIndexOf('.') + 1); |
| 62 | + this.lineNumber = lineNumber; |
| 63 | + } |
| 64 | + |
| 65 | + // E.g. handle "ImageJFunctions as IL" -> ["ImageJFunctions", "as", "IL"] |
| 66 | + public Import(final String packageName, final String[] parts, final int lineNumber) { |
| 67 | + this(packageName + "." + parts[0], 3 == parts.length ? parts[2] : null, lineNumber); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + static public final String findClassAliasOfVariable(final String variable, String inputText) { |
| 72 | + final String[] lines = inputText.split("\n"); |
| 73 | + for (int i = 0; i < lines.length; ++i) { |
| 74 | + final String line = lines[i]; |
| 75 | + final Matcher matcher = variableDeclarationPattern.matcher(line); |
| 76 | + if (matcher.find()) { |
| 77 | + // a line containing a variable declaration |
| 78 | +// System.out.println("Queried variable: " + variable); |
| 79 | +// System.out.println("Hit: line #" + i + ": " + line); |
| 80 | +// System.out.println("Matcher g1: " + matcher.group(1)); |
| 81 | +// System.out.println("Matcher g2: " + matcher.group(2)); |
| 82 | + if (variable.equals(matcher.group(1))) { |
| 83 | + return matcher.group(2); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + static public final HashMap<String, Import> findImportedClasses(final String text) { |
| 91 | + final HashMap<String, Import> importedClasses = new HashMap<>(); |
| 92 | + String packageName = ""; |
| 93 | + boolean endingBackslash = false; |
| 94 | + boolean insideTripleQuotes = false; |
| 95 | + |
| 96 | + // Scan the whole file for imports |
| 97 | + final String[] lines = text.split("\n"); |
| 98 | + for (int i=0; i<lines.length; ++i) { |
| 99 | + String line = lines[i]; |
| 100 | + final String trimmed = line.trim(); |
| 101 | + if (0 == trimmed.length() || '#' == trimmed.charAt(0)) continue; |
| 102 | + final Matcher mq = tripleQuotePattern.matcher(line); |
| 103 | + int n_triple_quotes = 0; |
| 104 | + while (mq.find()) ++n_triple_quotes; |
| 105 | + if (insideTripleQuotes) { |
| 106 | + if (0 != n_triple_quotes % 2) { // odd number |
| 107 | + insideTripleQuotes = false; |
| 108 | + } |
| 109 | + continue; |
| 110 | + } else { |
| 111 | + // If odd, enter |
| 112 | + if (0 != n_triple_quotes % 2) { |
| 113 | + insideTripleQuotes = true; |
| 114 | + continue; |
| 115 | + } |
| 116 | + } |
| 117 | + // Handle classes imported in a truncated import statement |
| 118 | + if (endingBackslash) { |
| 119 | + String importLine = line; |
| 120 | + final int backslash = line.lastIndexOf('\\'); |
| 121 | + if (backslash > -1) importLine = importLine.substring(0, backslash); |
| 122 | + else { |
| 123 | + final int sharp = importLine.lastIndexOf('#'); |
| 124 | + if (sharp > -1) importLine = importLine.substring(0, sharp); |
| 125 | + } |
| 126 | + for (final String simpleClassName : importLine.split(",")) { |
| 127 | + final Import im = new Import(packageName, simpleClassName.trim().split("\\s"), i); |
| 128 | + importedClasses.put(im.alias, im); |
| 129 | + } |
| 130 | + endingBackslash = -1 != backslash; // otherwise there is another line with classes of the same package |
| 131 | + continue; |
| 132 | + } |
| 133 | + final Matcher m = importPattern.matcher(line); |
| 134 | + if (m.find()) { |
| 135 | + packageName = null == m.group(2) ? "" : m.group(2); |
| 136 | + for (final String simpleClassName : m.group(3).split(",")) { |
| 137 | + final Import im = new Import(packageName, simpleClassName.trim().split("\\s"), i); |
| 138 | + importedClasses.put(im.alias, im); |
| 139 | + } |
| 140 | + endingBackslash = null != m.group(4) && m.group(4).length() > 0 && '\\' == m.group(4).charAt(0); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return importedClasses; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void insertCompletion(final Completion c, final boolean typedParamListStartChar) { |
| 149 | + if (c instanceof ImportCompletion) { |
| 150 | + final EditorPane editor = (EditorPane) super.getTextComponent(); |
| 151 | + editor.beginAtomicEdit(); |
| 152 | + try { |
| 153 | + super.insertCompletion(c, typedParamListStartChar); |
| 154 | + final ImportCompletion cc = (ImportCompletion)c; |
| 155 | + final HashMap<String, Import> importedClasses = findImportedClasses(editor.getText()); |
| 156 | + // Insert import statement after the last import, if not there already |
| 157 | + for (final Import im : importedClasses.values()) { |
| 158 | + if (im.className.contentEquals(cc.getClassName())) |
| 159 | + return; // don't insert |
| 160 | + } |
| 161 | + try { |
| 162 | + final int insertAtLine = 0 == importedClasses.size() ? 0 |
| 163 | + : importedClasses.values().stream().map(im -> im.lineNumber).reduce(Math::max).get(); |
| 164 | + editor.insert(cc.getImportStatement() + "\n", editor.getLineStartOffset(0 == insertAtLine ? 0 : insertAtLine + 1)); |
| 165 | + } catch (BadLocationException e) { |
| 166 | + e.printStackTrace(); |
| 167 | + } |
| 168 | + } finally { |
| 169 | + editor.endAtomicEdit(); |
| 170 | + } |
| 171 | + } else { |
| 172 | + super.insertCompletion(c, typedParamListStartChar); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments