Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripteditor codeassist and code completion #54

Closed
wants to merge 10 commits into from
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<url>http://maven.imagej.net/content/groups/public</url>
</repository>
</repositories>

<properties>
<fifesoft.version>2.5.7</fifesoft.version>
</properties>

<dependencies>
<!-- ImageJ dependencies -->
Expand Down Expand Up @@ -84,7 +88,17 @@
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>rsyntaxtextarea</artifactId>
<version>2.5.7</version>
<version>${fifesoft.version}</version>
</dependency>
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>autocomplete</artifactId>
<version>${fifesoft.version}</version>
</dependency>
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>languagesupport</artifactId>
<version>${fifesoft.version}</version>
</dependency>
<dependency>
<groupId>com.miglayout</groupId>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/net/imagej/ui/swing/script/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;

import org.fife.rsta.ac.LanguageSupport;
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Style;
Expand All @@ -65,6 +66,7 @@
import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.fife.ui.rtextarea.RecordableTextAction;
import org.scijava.Context;
import org.scijava.plugin.Parameter;
import org.scijava.prefs.PrefService;
import org.scijava.script.ScriptHeaderService;
Expand Down Expand Up @@ -93,6 +95,10 @@ public class EditorPane extends RSyntaxTextArea implements DocumentListener {
private boolean undoInProgress;
private boolean redoInProgress;

@Parameter
Context context;
@Parameter
private LanguageSupportService languageSupportService;
@Parameter
private ScriptService scriptService;
@Parameter
Expand Down Expand Up @@ -441,8 +447,16 @@ protected void setLanguage(final ScriptLanguage language) {
protected void setLanguage(final ScriptLanguage language,
final boolean addHeader)
{
// uninstall existing language support.
LanguageSupport support =
languageSupportService.getLanguageSupport(currentLanguage);
if (support != null) {
support.uninstall(this);
}

String languageName;
String defaultExtension;

if (language == null) {
languageName = "None";
defaultExtension = ".txt";
Expand Down Expand Up @@ -479,6 +493,13 @@ protected void setLanguage(final ScriptLanguage language,
if (header != null) {
setText(header += getText());
}

// try to get language support for current language, may be null.
support = languageSupportService.getLanguageSupport(currentLanguage);

if (support != null) {
support.install(this);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

package net.imagej.ui.swing.script;

import org.fife.rsta.ac.LanguageSupport;
import org.fife.ui.autocomplete.AutoCompletion;
import org.scijava.plugin.SingletonPlugin;

/**
* Interface for {@link AutoCompletion} plugins.
*
* @author Jonathan Hale
*/
public interface LanguageSupportPlugin extends SingletonPlugin,
LanguageSupport
{

/**
* @return the name of the language this plugin adds support for.
*/
public String getLanguageName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

package net.imagej.ui.swing.script;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.fife.rsta.ac.LanguageSupport;
import org.scijava.plugin.AbstractSingletonService;
import org.scijava.plugin.Plugin;
import org.scijava.script.ScriptLanguage;
import org.scijava.service.Service;

/**
* Service which manages {@link LanguageSupportPlugin}s.
* {@link LanguageSupportPlugin}s provide features like code completion for
* example.
*
* @author Jonathan Hale
*/
@Plugin(type = Service.class)
public class LanguageSupportService extends
AbstractSingletonService<LanguageSupportPlugin>
{

Map<String, LanguageSupport> languageSupports = null;

// -- LanguageSupportService methods --

/**
* Get a {@link LanguageSupport} for the given language.
*
* @param language Language to get support for.
* @return a {@link LanguageSupport} matching the given language or the
* <code>null</code> if there was none or language was
* <code>null</code>.
*/
public LanguageSupport getLanguageSupport(ScriptLanguage language) {
if (language == null) {
return null;
}
final String name = language.getLanguageName().toLowerCase();
return languageSupports().get(name);
}

// -- SingletonService methods --

@Override
public Class<LanguageSupportPlugin> getPluginType() {
return LanguageSupportPlugin.class;
}

// -- Helper methods - lazy initialization --

/** Gets {@link #languageSupports}, initializing if necessary. */
private Map<String, LanguageSupport> languageSupports() {
if (languageSupports == null) initLanguageSupportPlugins();
return languageSupports;
}

/** Initializes {@link #languageSupports}. */
private synchronized void initLanguageSupportPlugins() {
if (languageSupports != null) return;
final HashMap<String, LanguageSupport> map =
new HashMap<String, LanguageSupport>();

for (LanguageSupportPlugin instance : getInstances()) {
map.put(instance.getLanguageName(), instance);
}

languageSupports = Collections.unmodifiableMap(map);
}

}
10 changes: 4 additions & 6 deletions src/main/java/net/imagej/ui/swing/script/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public class TextEditor extends JFrame implements ActionListener,
public TextEditor(final Context context) {
super("Script Editor");
context.inject(this);
initializeTokenMakers(pluginService, log);
initializeTokenMakers();
loadPreferences();

// Initialize menu
Expand Down Expand Up @@ -647,20 +647,18 @@ public void componentResized(final ComponentEvent e) {
editorPane.requestFocus();
}

private synchronized static void initializeTokenMakers(
final PluginService pluginService, final LogService log)
{
private synchronized void initializeTokenMakers() {
if (tokenMakerFactory != null) return;
tokenMakerFactory =
(AbstractTokenMakerFactory) TokenMakerFactory.getDefaultInstance();
for (final PluginInfo<SyntaxHighlighter> info : pluginService
.getPluginsOfType(SyntaxHighlighter.class))
try {
tokenMakerFactory.putMapping("text/" + info.getLabel(), info
tokenMakerFactory.putMapping("text/" + info.getName(), info
.getClassName());
}
catch (final Throwable t) {
log.warn("Could not register " + info.getLabel(), t);
log.warn("Could not register " + info.getName(), t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author Johannes Schindelin
* @author Jonathan Hale
*/
@Plugin(type = SyntaxHighlighter.class, label = "beanshell")
@Plugin(type = SyntaxHighlighter.class, name = "beanshell")
public class BeanshellHighlighter extends JavaTokenMaker implements
SyntaxHighlighter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author Johannes Schindelin
* @author Jonathan Hale
*/
@Plugin(type = SyntaxHighlighter.class, label = "ecmascript")
@Plugin(type = SyntaxHighlighter.class, name = "ecmascript")
public class ECMAScriptHighlighter extends JavaScriptTokenMaker implements
SyntaxHighlighter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author Johannes Schindelin
* @author Jonathan Hale
*/
@Plugin(type = SyntaxHighlighter.class, label = "ij1-macro")
@Plugin(type = SyntaxHighlighter.class, name = "ij1-macro")
public class IJ1MacroHighlighter extends ImageJMacroTokenMaker implements
SyntaxHighlighter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author Johannes Schindelin
* @author Jonathan Hale
*/
@Plugin(type = SyntaxHighlighter.class, label = "matlab")
@Plugin(type = SyntaxHighlighter.class, name = "matlab")
public class MatlabHighlighter extends MatlabTokenMaker implements
SyntaxHighlighter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

package net.imagej.ui.swing.script.languagesupport;

import java.io.IOException;

import org.fife.rsta.ac.java.JavaLanguageSupport;
import org.scijava.plugin.Plugin;

import net.imagej.ui.swing.script.LanguageSupportPlugin;
import net.imagej.ui.swing.script.LanguageSupportService;

/**
* {@link LanguageSupportPlugin} for the java language.
*
* @author Jonathan Hale
* @see JavaLanguageSupport
* @see LanguageSupportService
*/
@Plugin(type = LanguageSupportPlugin.class)
public class JavaLanguageSupportPlugin extends JavaLanguageSupport implements
LanguageSupportPlugin
{

public JavaLanguageSupportPlugin() throws IOException {
super();

getJarManager().addCurrentJreClassFileSource();
}

@Override
public String getLanguageName() {
return "java";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

package net.imagej.ui.swing.script.languagesupport;

import org.fife.rsta.ac.js.JavaScriptLanguageSupport;
import org.scijava.plugin.Plugin;

import net.imagej.ui.swing.script.LanguageSupportPlugin;
import net.imagej.ui.swing.script.LanguageSupportService;

/**
* {@link LanguageSupportPlugin} for the javascript language.
*
* @author Jonathan Hale
* @see JavaScriptLanguageSupport
* @see LanguageSupportService
*/
@Plugin(type = LanguageSupportPlugin.class)
public class JavaScriptLanguageSupportPlugin extends JavaScriptLanguageSupport
implements LanguageSupportPlugin
{

public JavaScriptLanguageSupportPlugin() {
super();
}

@Override
public String getLanguageName() {
return "javascript";
}
}