|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSR-223-compliant Jython scripting language plugin. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2020 - 2025 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.scripting.jython; |
| 30 | + |
| 31 | +import java.lang.reflect.Constructor; |
| 32 | +import java.lang.reflect.Field; |
| 33 | +import java.lang.reflect.Method; |
| 34 | +import java.lang.reflect.Parameter; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +import org.scijava.script.complete.ClassIndex; |
| 40 | + |
| 41 | +/** |
| 42 | + * A mutable suggestion used internally by the Jython completion engine. It holds |
| 43 | + * the replacement text plus optional documentation and (for callables) parameter |
| 44 | + * and return-type metadata. The {@link JythonCodeCompleter} converts these into |
| 45 | + * toolkit-agnostic {@link org.scijava.script.complete.Completion}s at the |
| 46 | + * boundary. |
| 47 | + * |
| 48 | + * @author Albert Cardona |
| 49 | + */ |
| 50 | +public class CompletionText { |
| 51 | + |
| 52 | + private String replacementText; |
| 53 | + private String description; |
| 54 | + private String summary; |
| 55 | + private List<Parameter> method_args = Collections.emptyList(); |
| 56 | + private String method_returnType = null; |
| 57 | + |
| 58 | + public CompletionText(final String replacementText) { |
| 59 | + this(replacementText, (String) null, (String) null); |
| 60 | + } |
| 61 | + |
| 62 | + public CompletionText(final String replacementText, final String summary, |
| 63 | + final String description) |
| 64 | + { |
| 65 | + this.replacementText = replacementText; |
| 66 | + this.summary = summary; |
| 67 | + this.description = description; |
| 68 | + } |
| 69 | + |
| 70 | + public CompletionText(final String replacementText, final Class<?> c, |
| 71 | + final Field f) |
| 72 | + { |
| 73 | + this(replacementText, ClassIndex.getSummaryCompletion(f, c), null); |
| 74 | + } |
| 75 | + |
| 76 | + public CompletionText(final String replacementText, final Class<?> c, |
| 77 | + final Method m) |
| 78 | + { |
| 79 | + this(replacementText, ClassIndex.getSummaryCompletion(m, c), null); |
| 80 | + this.method_args = Arrays.asList(m.getParameters()); |
| 81 | + this.method_returnType = m.getReturnType().getCanonicalName(); |
| 82 | + } |
| 83 | + |
| 84 | + public CompletionText(final String replacementText, final Class<?> c, |
| 85 | + final Constructor<?> constructor) |
| 86 | + { |
| 87 | + this(replacementText, ClassIndex.getSummaryCompletion(constructor, c), |
| 88 | + null); |
| 89 | + this.method_args = Arrays.asList(constructor.getParameters()); |
| 90 | + this.method_returnType = c.getCanonicalName(); |
| 91 | + } |
| 92 | + |
| 93 | + public String getReplacementText() { |
| 94 | + return replacementText; |
| 95 | + } |
| 96 | + |
| 97 | + public String getDescription() { |
| 98 | + return description; |
| 99 | + } |
| 100 | + |
| 101 | + public String getSummary() { |
| 102 | + return summary; |
| 103 | + } |
| 104 | + |
| 105 | + public List<Parameter> getMethodArgs() { |
| 106 | + return method_args; |
| 107 | + } |
| 108 | + |
| 109 | + public String getReturnType() { |
| 110 | + return method_returnType; |
| 111 | + } |
| 112 | + |
| 113 | + public void setReplacementText(final String replacementText) { |
| 114 | + this.replacementText = replacementText; |
| 115 | + } |
| 116 | + |
| 117 | + public void setDescription(final String description) { |
| 118 | + this.description = description; |
| 119 | + } |
| 120 | + |
| 121 | + public void setSummary(final String summary) { |
| 122 | + this.summary = summary; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return replacementText + " | " + description + " | " + summary; |
| 128 | + } |
| 129 | +} |
0 commit comments