9696import org .scijava .script .ScriptLanguage ;
9797import org .scijava .script .ScriptService ;
9898import 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 ;
99102import 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 ());
0 commit comments