Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions org.eclipse.corrosion/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ Export-Package: org.eclipse.corrosion;x-friends:="org.eclipse.corrosion.tests",
org.eclipse.corrosion.wizards.export;x-friends:="org.eclipse.corrosion.tests",
org.eclipse.corrosion.wizards.newproject;x-friends:="org.eclipse.corrosion.tests"
Import-Package: org.eclipse.jdt.annotation
Service-Component: OSGI-INF/org.eclipse.corrosion.edit.FormatOnSave.xml
1 change: 1 addition & 0 deletions org.eclipse.corrosion/OSGI-INF/l10n/bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ content-type.name.0 = Cargo manifest file
toml.content-type.name = Toml file
page.name = Rust
page.name.0 = Text Editor
page.name.1 = Save Actions
category.name = Rust
wizard.name = Cargo Project
wizard.name.0 = Rust Crate Packager
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.corrosion.edit.FormatOnSave">
<property name="serverDefinitionId" type="String" value="org.eclipse.corrosion.rls"/>
<service>
<provide interface="org.eclipse.lsp4e.format.IFormatRegionsProvider"/>
</service>
<implementation class="org.eclipse.corrosion.edit.FormatOnSave"/>
</scr:component>
6 changes: 6 additions & 0 deletions org.eclipse.corrosion/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
id="org.eclipse.corrosion.debug"
name="%debugPreferencePage">
</page>
<page
category="org.eclipse.corrosion.texteditor"
class="org.eclipse.corrosion.edit.SaveActionsPreferencePage"
id="org.eclipse.corrosion.saveactions"
name="%page.name.1">
</page>
</extension>
<extension
point="org.eclipse.core.expressions.definitions">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class CorrosionPreferenceInitializer extends AbstractPreferenceInitialize
*/
public static final String DEFAULT_GDB_PREFERENCE = "corrosion.defaultGdb"; //$NON-NLS-1$

// Editor format on save preferences
public static final String EDIT_FORMAT_ON_SAVE_PREFERENCE = "corrosion.edit.formatOnSave"; //$NON-NLS-1$
public static final String EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE = "corrosion.edit.formatEditedOnSave"; //$NON-NLS-1$
public static final String EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE = "corrosion.edit.formatAllOnSave"; //$NON-NLS-1$

@Override
public void initializeDefaultPreferences() {
STORE.setDefault(RUSTUP_PATHS_PREFERENCE, getRustupPathBestGuess());
Expand All @@ -54,6 +59,10 @@ public void initializeDefaultPreferences() {

STORE.setDefault(WORKING_DIRECTORY_PREFERENCE, getWorkingDirectoryBestGuess());
STORE.setDefault(DEFAULT_GDB_PREFERENCE, DEFAULT_DEBUGGER);

STORE.setDefault(EDIT_FORMAT_ON_SAVE_PREFERENCE, false);
STORE.setDefault(EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE, false);
STORE.setDefault(EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE, true);
}

private static String getRustupPathBestGuess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,10 @@ public class Messages extends NLS {
public static String LaunchHandler_launchingFromCargoRegistryUnsupported;
public static String LaunchHandler_unsupportedProjectLocation;
public static String LaunchHandler_unableToLaunchCommand;

public static String SaveActionsConfigurationPage_FormatSourceCode;
public static String SaveActionsConfigurationPage_FormatSourceCode_description;
public static String SaveActionsConfigurationPage_FormatAllLines;
public static String SaveActionsConfigurationPage_FormatAllLines_description;
public static String SaveActionsConfigurationPage_FormatEditedLines;
public static String SaveActionsConfigurationPage_FormatEditedLines_description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/

package org.eclipse.corrosion.edit;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.corrosion.CorrosionPlugin;
import org.eclipse.corrosion.CorrosionPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.format.IFormatRegionsProvider;
import org.osgi.service.component.annotations.Component;

@Component(property = { "serverDefinitionId:String=org.eclipse.corrosion.rls" })
public class FormatOnSave implements IFormatRegionsProvider {

@Override
public IRegion[] getFormattingRegions(IDocument document) {
IPreferenceStore store = CorrosionPlugin.getDefault().getPreferenceStore();
var file = LSPEclipseUtils.getFile(document);
if (file != null) {
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE)) {
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE)) {
return IFormatRegionsProvider.allLines(document);
}
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE)) {
return IFormatRegionsProvider.calculateEditedLineRegions(document, new NullProgressMonitor());
}
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.eclipse.corrosion.edit;

import org.eclipse.corrosion.CorrosionPlugin;
import org.eclipse.corrosion.CorrosionPreferenceInitializer;
import org.eclipse.corrosion.Messages;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class SaveActionsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
private IPreferenceStore store;

private Button format;
private Button formatAll;
private Button formatEdited;

@Override
public void init(IWorkbench workbench) {
store = CorrosionPlugin.getDefault().getPreferenceStore();
}

@Override
protected Control createContents(Composite parent) {
parent.setLayoutData(new GridData(SWT.NONE, SWT.TOP, true, false));

Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(GridLayoutFactory.fillDefaults().create());

format = createButton(Messages.SaveActionsConfigurationPage_FormatSourceCode,
Messages.SaveActionsConfigurationPage_FormatSourceCode_description, composite, SWT.CHECK, 0);
formatAll = createButton(Messages.SaveActionsConfigurationPage_FormatAllLines,
Messages.SaveActionsConfigurationPage_FormatAllLines_description, composite, SWT.RADIO, 15);
formatEdited = createButton(Messages.SaveActionsConfigurationPage_FormatEditedLines,
Messages.SaveActionsConfigurationPage_FormatEditedLines_description, composite, SWT.RADIO, 15);

format.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE));
formatAll.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE));
formatEdited
.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE));

// listener to update enable state for the radio buttons
format.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateEnabledState();
}
});

updateEnabledState();
return parent;

}

@Override
protected void performDefaults() {
format.setSelection(store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE));
formatAll.setSelection(
store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE));
formatEdited.setSelection(
store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE));
updateEnabledState();
super.performDefaults();
}

@Override
public boolean performOk() {
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE, format.getSelection());
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE, formatAll.getSelection());
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE,
formatEdited.getSelection());
return true;
}

private static Button createButton(String name, String description, Composite composite, int style,
int horizontalIndent) {
Button button = new Button(composite, style);
button.setLayoutData(GridDataFactory.fillDefaults().indent(horizontalIndent, 0).create());
button.setText(name);
button.setToolTipText(description);
return button;
}

private void updateEnabledState() {
boolean formatOnSaveIsEnabled = format.getSelection();
formatAll.setEnabled(formatOnSaveIsEnabled);
formatEdited.setEnabled(formatOnSaveIsEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ LaunchHandler_unableToLaunch=Unable to Launch
LaunchHandler_launchingFromCargoRegistryUnsupported=Launching from the central cargo registry is not supported.
LaunchHandler_unsupportedProjectLocation=Unsupported project location. The project must be imported as an Eclipse project.
LaunchHandler_unableToLaunchCommand=Unable to launch command: {0}.

SaveActionsConfigurationPage_FormatSourceCode=Format source code
SaveActionsConfigurationPage_FormatSourceCode_description=Formats source code when file is saved
SaveActionsConfigurationPage_FormatAllLines=Format all lines
SaveActionsConfigurationPage_FormatAllLines_description=Formats all source code lines
SaveActionsConfigurationPage_FormatEditedLines=Format edited lines (currently unsupported by rust-analyzer)
SaveActionsConfigurationPage_FormatEditedLines_description=Formats edited source code lines only