10000 ClassUtil and CompletionText support for class constructors. · scijava/script-editor@7c55b2d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c55b2d

Browse files
committed
ClassUtil and CompletionText support for class constructors.
1 parent e9d236f commit 7c55b2d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/java/org/scijava/ui/swing/script/autocompletion/ClassUtil.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.io.File;
3232
import java.io.IOException;
33+
import java.lang.reflect.Constructor;
3334
import java.lang.reflect.Field;
3435
import java.lang.reflect.Method;
3536
import java.lang.reflect.Parameter;
@@ -428,6 +429,39 @@ protected static String getSummaryCompletion(final Method method, final Class<?>
428429
summary.append("</DL>");
429430
return summary.toString();
430431
}
432+
433+
/**
434+
* Assembles an HTML-formatted auto-completion summary with functional
435+
* hyperlinks
436+
*
437+
* @param constructor the constructor being documented
438+
* @param c the class being documented. Expected to be documented at the
439+
* Scijava API documentation portal.
440+
* @return the completion summary
441+
*/
442+
protected static String getSummaryCompletion(final Constructor<?> constructor, final Class<?> c) {
443+
final StringBuffer summary = new StringBuffer();
444+
final StringBuffer replacementHeader = new StringBuffer(c.getSimpleName());
445+
final int bIndex = replacementHeader.length(); // remember '(' position
446+
replacementHeader.append("(");
447+
final Parameter[] params = constructor.getParameters();
448+
if (params.length > 0) {
449+
for (final Parameter parameter : params) {
450+
replacementHeader.append(parameter.getType().getSimpleName()).append(" ").append(parameter.getName()).append(", ");
451+
}
452+
replacementHeader.setLength(replacementHeader.length() - 2); // remove trailing ', ';
453+
}
454+
replacementHeader.append(")");
455+
replacementHeader.replace(bIndex, bIndex + 1, "</b>("); // In header, highlight only method name for extra contrast
456+
summary.append("<b>").append(replacementHeader);
457+
summary.append("<DL>");
458+
summary.append("<DT><b>Intantiates:</b>");
459+
summary.append("<DD>").append(c.getSimpleName());
460+
summary.append("<DT><b>Defined in:</b>");
461+
summary.append("<DD>").append(getJavaDocLink(c));
462+
summary.append("</DL>");
463+
return summary.toString();
464+
}
431465

432466
static List<Completion> classUnavailableCompletions(final CompletionProvider provider, final String pre) {
433467
// placeholder completions to warn users class was not available (repeated to force pop-up display)

src/main/java/org/scijava/ui/swing/script/autocompletion/CompletionText.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929
package org.scijava.ui.swing.script.autocompletion;
3030

31+
import java.lang.reflect.Constructor;
3132
import java.lang.reflect.Field;
3233
import java.lang.reflect.Method;
3334
import java.lang.reflect.Parameter;
@@ -66,6 +67,12 @@ public CompletionText(final String replacementText, final Class<?> c, final Meth
6667
this.method_args = Arrays.asList(m.getParameters());
6768
this.method_returnType = m.getReturnType().getCanonicalName();
6869
}
70+
71+
public CompletionText(final String replacementText, final Class<?> c, final Constructor<?> constructor) {
72+
this(replacementText, ClassUtil.getSummaryCompletion(constructor, c), null);
73+
this.method_args = Arrays.asList(constructor.getParameters());
74+
this.method_returnType = c.getCanonicalName();
75+
}
6976

7077
public String getReplacementText() {
7178
return replacementText;

0 commit comments

Comments
 (0)
0