Skip to content

Commit

Permalink
Issue #183 - Support initializationOptions at RLS startup
Browse files Browse the repository at this point in the history
* Support initialization options
* Added default initialization options
* Preferences for custom options

Signed-off-by: Nicola Orru <[email protected]>
  • Loading branch information
norru authored and mickaelistria committed Dec 3, 2018
1 parent 6607e9e commit 251108b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Contributors:
* Lucas Bullen (Red Hat Inc.) - Initial implementation
* Holger Voormann - Set correct default locations on Windows (https://github.com/eclipse/corrosion/issues/86)
* Nicola Orru - Added support for external RLS startup configuration
*******************************************************************************/
package org.eclipse.corrosion;

Expand All @@ -22,6 +23,7 @@
public class CorrosionPreferenceInitializer extends AbstractPreferenceInitializer {

private static final IPreferenceStore STORE = CorrosionPlugin.getDefault().getPreferenceStore();
private static final String CARGO_DEFAULT_ROOT = System.getProperty("user.home") + "/.cargo/"; //$NON-NLS-1$ //$NON-NLS-2$
private static final String CARGO_DEFAULT_HOME = System.getProperty("user.home") + "/.cargo/bin/"; //$NON-NLS-1$ //$NON-NLS-2$
private static final boolean IS_WINDOWS = Platform.getOS().equals(Platform.OS_WIN32);

Expand All @@ -34,6 +36,7 @@ public class CorrosionPreferenceInitializer extends AbstractPreferenceInitialize
public static final String TOOLCHAIN_TYPE_PREFERENCE = "corrosion.rustup_toolchain_type"; //$NON-NLS-1$

public static final String RLS_PATH_PREFERENCE = "corrosion.rslPath"; //$NON-NLS-1$
public static final String RLS_CONFIGURATION_PATH_PREFERENCE = "corrosion.rls_configurationPath"; //$NON-NLS-1$
public static final String SYSROOT_PATH_PREFERENCE = "corrosion.sysrootPath"; //$NON-NLS-1$

public static final String WORKING_DIRECTORY_PREFERENCE = "corrosion.workingDirectory"; //$NON-NLS-1$
Expand All @@ -48,6 +51,8 @@ public void initializeDefaultPreferences() {
setToolchainBestGuesses();

STORE.setDefault(RLS_PATH_PREFERENCE, getRLSPathBestGuess());
STORE.setDefault(RLS_CONFIGURATION_PATH_PREFERENCE, getRLSConfigurationPathBestGuess());

STORE.setDefault(SYSROOT_PATH_PREFERENCE, getSysrootPathBestGuess());

STORE.setDefault(WORKING_DIRECTORY_PREFERENCE, getWorkingDirectoryBestGuess());
Expand Down Expand Up @@ -117,6 +122,10 @@ private static String getRLSPathBestGuess() {
return command;
}

private static String getRLSConfigurationPathBestGuess() {
return CARGO_DEFAULT_ROOT + "rls.conf"; //$NON-NLS-1$
}

private static String getSysrootPathBestGuess() {
File rustc = new File(findCommandPath("rustc")); //$NON-NLS-1$
if (!(rustc.exists() && rustc.isFile() && rustc.canExecute())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - Initial implementation
* Nicola Orru - Added support for external RLS startup configuration
*******************************************************************************/
package org.eclipse.corrosion;

Expand Down Expand Up @@ -126,6 +127,8 @@ private void initializeContent() {
rlsInput.setValue(store.getString(CorrosionPreferenceInitializer.RLS_PATH_PREFERENCE));
sysrootInput.setValue(store.getString(CorrosionPreferenceInitializer.SYSROOT_PATH_PREFERENCE));
workingDirectoryInput.setValue(store.getString(CorrosionPreferenceInitializer.WORKING_DIRECTORY_PREFERENCE));
rlsConfigurationPathInput
.setValue(store.getString(CorrosionPreferenceInitializer.RLS_CONFIGURATION_PATH_PREFERENCE));
}

@Override
Expand Down Expand Up @@ -316,6 +319,9 @@ public boolean performOk() {
int source = getRadioSelection();
store.setValue(CorrosionPreferenceInitializer.RUST_SOURCE_PREFERENCE, RUST_SOURCE_OPTIONS.get(source));
store.setValue(CorrosionPreferenceInitializer.WORKING_DIRECTORY_PREFERENCE, workingDirectoryInput.getValue());
store.setValue(CorrosionPreferenceInitializer.RLS_CONFIGURATION_PATH_PREFERENCE,
rlsConfigurationPathInput.getValue());

if (source == 0) {
store.setValue(CorrosionPreferenceInitializer.TOOLCHAIN_TYPE_PREFERENCE, rustupToolchainCombo.getText());
store.setValue(CorrosionPreferenceInitializer.DEFAULT_PATHS_PREFERENCE,
Expand Down Expand Up @@ -397,6 +403,13 @@ private void createCommandPathsPart(Composite container) {
workingDirectoryInput.setTextGridData(wdTextData);
workingDirectoryInput.createVariableSelection();
workingDirectoryInput.createFolderSelection();

rlsConfigurationPathInput = new InputComponent(parent, Messages.CorrosionPreferencePage_rlsConfigurationPath,
e -> setValid(isPageValid()));
rlsConfigurationPathInput.createComponent();
rlsConfigurationPathInput.createVariableSelection();
rlsConfigurationPathInput.createFileSelection();
rlsConfigurationPathInput.setTextGridData(wdTextData);
}

private boolean installInProgress = false;
Expand Down Expand Up @@ -523,6 +536,7 @@ private void setRustupEnabled(boolean enabled) {
}

private InputComponent rlsInput;
private InputComponent rlsConfigurationPathInput;
private InputComponent sysrootInput;

private void createOtherPart(Composite container) {
Expand Down
5 changes: 5 additions & 0 deletions org.eclipse.corrosion/src/org/eclipse/corrosion/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Mickael Istria (Red Hat Inc.) - Initial implementation
* Nicola Orru - Added support for external RLS startup configuration
*******************************************************************************/
package org.eclipse.corrosion;

Expand Down Expand Up @@ -57,6 +58,9 @@ public class Messages extends NLS {
public static String RLSStreamConnectionProvider_OpenPreferences;
public static String RLSStreamConnectionProvider_requirementsNotFound;
public static String RLSStreamConnectionProvider_rlsNotFound;
public static String RLSStreamConnectionProvider_rlsConfigurationNotFound;
public static String RLSStreamConnectionProvider_rlsConfigurationNotSet;
public static String RLSStreamConnectionProvider_rlsConfigurationError;
public static String RLSStreamConnectionProvider_rustSupportNotFound;
public static String RLSStreamConnectionProvider_unableToSet;
public static String RustDebugDelegate_unableToLaunch_title;
Expand Down Expand Up @@ -124,6 +128,7 @@ public class Messages extends NLS {
public static String CorrosionPreferencePage_rlsLocation;
public static String CorrosionPreferencePage_rlsNonExecutable;
public static String CorrosionPreferencePage_rlsPath;
public static String CorrosionPreferencePage_rlsConfigurationPath;
public static String CorrosionPreferencePage_Rustup;
public static String CorrosionPreferencePage_rustupNonExecutable;
public static String CorrosionPreferencePage_rustupMissingRLS;
Expand Down
14 changes: 14 additions & 0 deletions org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - Initial implementation
* Nicola Orru - Added support for external RLS startup configuration
*******************************************************************************/
package org.eclipse.corrosion;

Expand Down Expand Up @@ -258,6 +259,19 @@ public static boolean setSystemProperties() {
return false;
}

public static String getRlsConfigurationPath() {
CorrosionPlugin plugin = CorrosionPlugin.getDefault();
IPreferenceStore preferenceStore = plugin.getPreferenceStore();
String preferencePath = preferenceStore
.getString(CorrosionPreferenceInitializer.RLS_CONFIGURATION_PATH_PREFERENCE);
if (preferencePath.isEmpty()) {
CorrosionPlugin.getDefault().getLog()
.log(new Status(IStatus.WARNING, CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
Messages.RLSStreamConnectionProvider_rlsConfigurationNotSet));
}
return preferencePath;
}

public static String getRLS() {
CorrosionPlugin plugin = CorrosionPlugin.getDefault();
IPreferenceStore preferenceStore = plugin.getPreferenceStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
*******************************************************************************/
package org.eclipse.corrosion.edit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.corrosion.CorrosionPlugin;
import org.eclipse.corrosion.CorrosionPreferencePage;
import org.eclipse.corrosion.Messages;
Expand All @@ -30,6 +40,9 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class RLSStreamConnectionProvider implements StreamConnectionProvider {

private static boolean hasCancelledSetup = false;
Expand Down Expand Up @@ -71,6 +84,34 @@ private static synchronized void setHasCancelledSetup(Boolean newValue) {
hasCancelledSetup = newValue;
}

private static Map<String, Object> getDefaultInitializationOptions() {
final Map<String, Object> initializationSettings = new HashMap<>();
initializationSettings.put("clippy_preference", "on"); //$NON-NLS-1$//$NON-NLS-2$
// undocumented, transitional. Will be superseded by "racer_completion" which
// defaults to "true"
initializationSettings.put("goto_def_racer_fallback", true); //$NON-NLS-1$
return Collections.singletonMap("settings", Collections.singletonMap("rust", initializationSettings)); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
public Object getInitializationOptions(URI rootUri) {
final String settingsPath = RustManager.getRlsConfigurationPath();
final File settingsFile = new File(settingsPath);
final Gson gson = new Gson();
try (JsonReader reader = new JsonReader(new FileReader(settingsFile))) {
return gson.fromJson(reader, HashMap.class);
} catch (FileNotFoundException e) {
CorrosionPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,
CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
MessageFormat.format(Messages.RLSStreamConnectionProvider_rlsConfigurationNotFound, settingsPath)));
} catch (Throwable e) {
CorrosionPlugin.getDefault().getLog().log(new Status(IStatus.ERROR,
CorrosionPlugin.getDefault().getBundle().getSymbolicName(),
MessageFormat.format(Messages.RLSStreamConnectionProvider_rlsConfigurationError, settingsPath, e)));
}
return getDefaultInitializationOptions();
}

@Override
public InputStream getInputStream() {
return process == null ? null : process.getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ OptionSelector_optionDescription=Option Description:
RLSStreamConnectionProvider_OpenPreferences=Open Preferences
RLSStreamConnectionProvider_requirementsNotFound=Requirments for Rust edition were not found. Install the required components or input their paths in the Rust Preferences.
RLSStreamConnectionProvider_rlsNotFound=Rust Language Server not found. Update in Rust preferences.
RLSStreamConnectionProvider_rlsConfigurationNotSet=RLS settings for Corrosion path not set.
RLSStreamConnectionProvider_rlsConfigurationNotFound=RLS settings for Corrosion path not found at {0}.
RLSStreamConnectionProvider_rlsConfigurationError=RLS settings at {0} could not be parsed: {1}.
RLSStreamConnectionProvider_rustSupportNotFound=Rust Support Not Found
RLSStreamConnectionProvider_unableToSet=Was unable to set the `SYS_ROOT` and `LD_LIBRARY_PATH` environment variables. Please do so manually.
RustDebugDelegate_unableToLaunch_title=Unable to Launch
Expand Down Expand Up @@ -114,6 +117,7 @@ CorrosionPreferencePage_otherInstallation=Other installation
CorrosionPreferencePage_rlsLocation=Rust Language Server Location:
CorrosionPreferencePage_rlsNonExecutable=Inputted `rls` command is not executable
CorrosionPreferencePage_rlsPath=Path to the Rust Language Server (rls):
CorrosionPreferencePage_rlsConfigurationPath=Rls config:
CorrosionPreferencePage_Rustup=Rustup:
CorrosionPreferencePage_rustupNonExecutable=Inputted `rustup` command is not executable
CorrosionPreferencePage_rustupMissingRLS=Install missing rls component in Rustup
Expand Down

0 comments on commit 251108b

Please sign in to comment.