Skip to content

Commit 666820b

Browse files
gselzerclaude
andcommitted
Talk to language servers for code intelligence
Make the script editor a thin Swing/RSTA adapter over scijava-lsp, so languages contribute completion, hover docs, diagnostics and signature help without any Swing code: - LanguageServerLanguageSupport: install editor support for any language with a language server, asking it through script sessions. - SciJavaCompletionProvider: translate LSP completions into RSTA completions (BasicCompletion, or FunctionCompletion for callables so parameter assistance engages), requested asynchronously. - SciJavaAutoCompletion, SciJavaCompletion, SciJavaFunctionCompletion and AdditionalEdits: apply additional edits (e.g. auto-imports) on acceptance. - SignaturePopup and SignatureLabel: show signature help as you type, highlighting the active parameter. - HoverToolTipSupplier and DiagnosticsParser: show hover docs and problems. - EditorPane, TextEditor and PromptPane: wire the above into editors and the REPL, and tell servers when a script's editor closes. - ClassUtil: reduced to a thin facade over scijava-common's ClassIndex. - PythonHighlighter: add a built-in Python (Jython) syntax highlighter. - Update copyright headers for 2026. pom.xml uses scijava-common 2.101.0 and a scijava-lsp snapshot until pom-scijava manages them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9762eaa commit 666820b

22 files changed

Lines changed: 3186 additions & 427 deletions

‎pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@
141141

142142
<properties>
143143
<scijava.jvm.version>11</scijava.jvm.version>
144+
<!-- TEMP: Remove once pom-scijava manages scijava-common 2.101.0. -->
145+
<scijava-common.version>2.101.0</scijava-common.version>
146+
<!-- TEMP: in development; remove once pinned by pom-scijava. -->
147+
<scijava-lsp.version>0.1.0-SNAPSHOT</scijava-lsp.version>
144148
<main-class>org.scijava.ui.swing.script.Main</main-class>
145149
<package-name>org.scijava.ui.swing.script</package-name>
146150
<license.licenseName>bsd_2</license.licenseName>
@@ -160,6 +164,11 @@
160164
<groupId>org.scijava</groupId>
161165
<artifactId>scijava-common</artifactId>
162166
</dependency>
167+
<dependency>
168+
<groupId>org.scijava</groupId>
169+
<artifactId>scijava-lsp</artifactId>
170+
<version>${scijava-lsp.version}</version>
171+
</dependency>
163172
<dependency>
164173
<groupId>org.scijava</groupId>
165174
<artifactId>scijava-search</artifactId>

‎src/main/java/org/scijava/ui/swing/script/EditorPane.java‎

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
import org.scijava.script.ScriptLanguage;
9797
import org.scijava.script.ScriptService;
9898
import org.scijava.ui.swing.script.vim.VimHandler;
99+
import org.scijava.lsp.LanguageServerService;
100+
import org.scijava.lsp.ScriptSession;
101+
import org.scijava.ui.swing.script.autocompletion.LanguageServerLanguageSupport;
99102
import org.scijava.util.FileUtils;
100103

101104
/**
@@ -121,9 +124,13 @@ public class EditorPane extends RSyntaxTextArea implements DocumentListener {
121124
private boolean undoInProgress;
122125
private boolean redoInProgress;
123126
private boolean autoCompletionEnabled;
127+
private boolean hoverEnabled = true;
128+
private boolean diagnosticsEnabled = true;
124129
private boolean autoCompletionJavaFallback;
125130
private boolean autoCompletionWithoutKey;
126131
private String supportStatus;
132+
/** The completion support currently installed on this pane, if any. */
133+
private LanguageSupport installedSupport;
127134
private final ErrorParser errorHighlighter;
128135
private final EditorPaneActions actions;
129136
private final VimHandler vim = new VimHandler(this);
@@ -134,6 +141,8 @@ public class EditorPane extends RSyntaxTextArea implements DocumentListener {
134141
@Parameter
135142
private LanguageSupportService languageSupportService;
136143
@Parameter
144+
private LanguageServerService languageServerService;
145+
@Parameter
137146
private ScriptService scriptService;
138147
@Parameter
139148
private ScriptHeaderService scriptHeaderService;
@@ -764,6 +773,17 @@ protected void setLanguage(final ScriptLanguage language) {
764773
setLanguage(language, false);
765774
}
766775

776+
/**
777+
* Uninstalls the language support (e.g. code completion), as the editor
778+
* is closed.
779+
*/
780+
void releaseLanguageSupport() {
781+
if (installedSupport != null) {
782+
installedSupport.uninstall(this);
783+
installedSupport = null;
784+
}
785+
}
786+
767787
/**
768788
* Set the language of this {@link EditorPane}, optionally adding a header.
769789
* TODO: What is this header?
@@ -783,11 +803,8 @@ private void setLanguage(final ScriptLanguage language,
783803
final boolean addHeader, final boolean renameFile)
784804
{
785805
// uninstall existing language support.
786-
LanguageSupport support =
787-
languageSupportService.getLanguageSupport(currentLanguage);
788-
if (support != null) {
789-
support.uninstall(this);
790-
}
806+
releaseLanguageSupport();
807+
LanguageSupport support;
791808

792809
String languageName;
793810
String defaultExtension;
@@ -842,15 +859,26 @@ private void setLanguage(final ScriptLanguage language,
842859
return; // no need to update console any further
843860
}
844861
String supportLevel = "SciJava supported";
845-
// try to get language support for current language, may be null.
862+
// Tier 1: a heavy RSTA LanguageSupportPlugin (e.g. the Java parser).
846863
support = languageSupportService.getLanguageSupport(currentLanguage);
847864

848-
// that did not work. See if there is internal support for it.
865+
// Tier 2: language servers for this language.
866+
if (support == null && languageServerService.supports(currentLanguage)) {
867+
final ScriptLanguage served = currentLanguage;
868+
final LanguageServerLanguageSupport serverSupport =
869+
new LanguageServerLanguageSupport(() -> new ScriptSession(
870+
languageServerService, served), log, this::getFile);
871+
serverSupport.setHoverEnabled(hoverEnabled);
872+
serverSupport.setDiagnosticsEnabled(diagnosticsEnabled);
873+
support = serverSupport;
874+
}
875+
876+
// Tier 3: RSTA's own built-in support for the syntax style.
849877
if (support == null) {
850878
support = LanguageSupportFactory.get().getSupportFor(styleName);
851879
supportLevel = "Legacy supported";
852880
}
853-
// that did not work, Fallback to Java
881+
// Tier 4: fall back to Java completion, if requested.
854882
if (support == null && autoCompletionJavaFallback) {
855883
support = languageSupportService.getLanguageSupport(scriptService.getLanguageByName("Java"));
856884
supportLevel = "N/A. Using Java as fallback";
@@ -859,6 +887,7 @@ private void setLanguage(final ScriptLanguage language,
859887
support.setAutoCompleteEnabled(autoCompletionEnabled);
860888
support.setAutoActivationEnabled(autoCompletionWithoutKey);
861889
support.install(this);
890+
installedSupport = support;
862891
if (!autoCompletionEnabled)
863892
supportLevel += " but currently disabled\n";
864893
else {
@@ -891,8 +920,8 @@ public void setAutoCompletion(final boolean enabled) {
891920
* @param value Whether Java should be enabled as fallback language for
892921
* auto-completion
893922
*/
894-
void setFallbackAutoCompletion(final boolean value) {
895-
autoCompletionJavaFallback = value;
923+
void setFallbackAutoCompletion(final boolean enabled) {
924+
autoCompletionJavaFallback = enabled;
896925
if (autoCompletionEnabled && currentLanguage != null)
897926
setLanguage(currentLanguage);
898927
}
@@ -913,6 +942,32 @@ public boolean isAutoCompletionEnabled() {
913942
return autoCompletionEnabled;
914943
}
915944

945+
/**
946+
* Toggles whether what is known about the code under the mouse (e.g. a
947+
* function's documentation) is shown, for languages whose completer knows.
948+
*/
949+
public void setHoverEnabled(final boolean enabled) {
950+
hoverEnabled = enabled;
951+
if (currentLanguage != null) setLanguage(currentLanguage);
952+
}
953+
954+
public boolean isHoverEnabled() {
955+
return hoverEnabled;
956+
}
957+
958+
/**
959+
* Toggles whether problems in the code (e.g. syntax errors) are shown as
960+
* the code is edited, for languages whose completer finds them.
961+
*/
962+
public void setDiagnosticsEnabled(final boolean enabled) {
963+
diagnosticsEnabled = enabled;
964+
if (currentLanguage != null) setLanguage(currentLanguage);
965+
}
966+
967+
public boolean isDiagnosticsEnabled() {
968+
return diagnosticsEnabled;
969+
}
970+
916971
public boolean isAutoCompletionKeyless() {
917972
return autoCompletionWithoutKey;
918973
}
@@ -1120,6 +1175,8 @@ public void convertSpacesToTabs() {
11201175
public static final String AUTOCOMPLETE_PREFS = "script.editor.AC";
11211176
public static final String AUTOCOMPLETE_KEYLESS_PREFS = "script.editor.ACNoKey";
11221177
public static final String AUTOCOMPLETE_FALLBACK_PREFS = "script.editor.ACFallback";
1178+
public static final String HOVER_PREFS = "script.editor.Hover";
1179+
public static final String DIAGNOSTICS_PREFS = "script.editor.Diagnostics";
11231180
public static final String MARK_OCCURRENCES_PREFS = "script.editor.Occurrences";
11241181
public static final String VIM_PREFS = "script.editor.Vim";
11251182
public static final String FOLDERS_PREFS = "script.editor.folders";
@@ -1151,6 +1208,8 @@ public void loadPreferences() {
11511208
setKeylessAutoCompletion(prefService.getBoolean(getClass(), AUTOCOMPLETE_KEYLESS_PREFS, true)); // true for backwards compatibility with ImageJ macro
11521209
setFallbackAutoCompletion(prefService.getBoolean(getClass(), AUTOCOMPLETE_FALLBACK_PREFS, false));
11531210
setMarkOccurrences(prefService.getBoolean(getClass(), MARK_OCCURRENCES_PREFS, true));
1211+
setHoverEnabled(prefService.getBoolean(getClass(), HOVER_PREFS, true));
1212+
setDiagnosticsEnabled(prefService.getBoolean(getClass(), DIAGNOSTICS_PREFS, true));
11541213
setMarginLineEnabled(prefService.getBoolean(getClass(), MARGIN_VISIBLE_PREFS, false));
11551214
setVimEnabled(prefService.getBoolean(getClass(), VIM_PREFS, false));
11561215
applyTheme(themeName());
@@ -1205,6 +1264,8 @@ public void savePreferences(final String top_folders, final String theme) {
12051264
prefService.put(getClass(), AUTOCOMPLETE_PREFS, isAutoCompletionEnabled());
12061265
prefService.put(getClass(), AUTOCOMPLETE_KEYLESS_PREFS, isAutoCompletionKeyless());
12071266
prefService.put(getClass(), AUTOCOMPLETE_FALLBACK_PREFS, isAutoCompletionFallbackEnabled());
1267+
prefService.put(getClass(), HOVER_PREFS, isHoverEnabled());
1268+
prefService.put(getClass(), DIAGNOSTICS_PREFS, isDiagnosticsEnabled());
12081269
prefService.put(getClass(), MARGIN_VISIBLE_PREFS, isMarginLineEnabled());
12091270
prefService.put(getClass(), MARK_OCCURRENCES_PREFS, getMarkOccurrences());
12101271
prefService.put(getClass(), VIM_PREFS, isVimEnabled());

‎src/main/java/org/scijava/ui/swing/script/InterpreterPane.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public void print(final String string) {
172172

173173
public void dispose() {
174174
output.close();
175+
prompt.dispose();
175176
}
176177

177178
// -- UIComponent methods --

0 commit comments

Comments
 (0)