Skip to content

Commit ede01ad

Browse files
add preference page to format on save
note that currently rnage-formatting is an unstable feature only enabled in nightly builds, so lsp4e will automatically map it to a full file format when using the stable toolchain. Copied and modified from eclipse CDT LSP.
1 parent 0172c8b commit ede01ad

9 files changed

Lines changed: 182 additions & 1 deletion

File tree

org.eclipse.corrosion/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ Export-Package: org.eclipse.corrosion;x-friends:="org.eclipse.corrosion.tests",
5353
org.eclipse.corrosion.wizards.export;x-friends:="org.eclipse.corrosion.tests",
5454
org.eclipse.corrosion.wizards.newproject;x-friends:="org.eclipse.corrosion.tests"
5555
Import-Package: org.eclipse.jdt.annotation
56+
Service-Component: OSGI-INF/org.eclipse.corrosion.edit.FormatOnSave.xml

org.eclipse.corrosion/OSGI-INF/l10n/bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ content-type.name.0 = Cargo manifest file
66
toml.content-type.name = Toml file
77
page.name = Rust
88
page.name.0 = Text Editor
9+
page.name.1 = Save Actions
910
category.name = Rust
1011
wizard.name = Cargo Project
1112
wizard.name.0 = Rust Crate Packager
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.corrosion.edit.FormatOnSave">
3+
<property name="serverDefinitionId" type="String" value="org.eclipse.corrosion.rls"/>
4+
<service>
5+
<provide interface="org.eclipse.lsp4e.format.IFormatRegionsProvider"/>
6+
</service>
7+
<implementation class="org.eclipse.corrosion.edit.FormatOnSave"/>
8+
</scr:component>

org.eclipse.corrosion/plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
id="org.eclipse.corrosion.debug"
8282
name="%debugPreferencePage">
8383
</page>
84+
<page
85+
category="org.eclipse.corrosion.texteditor"
86+
class="org.eclipse.corrosion.edit.SaveActionsPreferencePage"
87+
id="org.eclipse.corrosion.saveactions"
88+
name="%page.name.1">
89+
</page>
8490
</extension>
8591
<extension
8692
point="org.eclipse.core.expressions.definitions">

org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferenceInitializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public class CorrosionPreferenceInitializer extends AbstractPreferenceInitialize
4343
*/
4444
public static final String DEFAULT_GDB_PREFERENCE = "corrosion.defaultGdb"; //$NON-NLS-1$
4545

46+
// Editor format on save preferences
47+
public static final String EDIT_FORMAT_ON_SAVE_PREFERENCE = "corrosion.edit.formatOnSave"; //$NON-NLS-1$
48+
public static final String EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE = "corrosion.edit.formatEditedOnSave"; //$NON-NLS-1$
49+
public static final String EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE = "corrosion.edit.formatAllOnSave"; //$NON-NLS-1$
50+
4651
@Override
4752
public void initializeDefaultPreferences() {
4853
STORE.setDefault(RUSTUP_PATHS_PREFERENCE, getRustupPathBestGuess());
@@ -54,6 +59,10 @@ public void initializeDefaultPreferences() {
5459

5560
STORE.setDefault(WORKING_DIRECTORY_PREFERENCE, getWorkingDirectoryBestGuess());
5661
STORE.setDefault(DEFAULT_GDB_PREFERENCE, DEFAULT_DEBUGGER);
62+
63+
STORE.setDefault(EDIT_FORMAT_ON_SAVE_PREFERENCE, false);
64+
STORE.setDefault(EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE, false);
65+
STORE.setDefault(EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE, true);
5766
}
5867

5968
private static String getRustupPathBestGuess() {

org.eclipse.corrosion/src/org/eclipse/corrosion/Messages.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,10 @@ public class Messages extends NLS {
169169
public static String LaunchHandler_launchingFromCargoRegistryUnsupported;
170170
public static String LaunchHandler_unsupportedProjectLocation;
171171
public static String LaunchHandler_unableToLaunchCommand;
172-
172+
public static String SaveActionsConfigurationPage_FormatSourceCode;
173+
public static String SaveActionsConfigurationPage_FormatSourceCode_description;
174+
public static String SaveActionsConfigurationPage_FormatAllLines;
175+
public static String SaveActionsConfigurationPage_FormatAllLines_description;
176+
public static String SaveActionsConfigurationPage_FormatEditedLines;
177+
public static String SaveActionsConfigurationPage_FormatEditedLines_description;
173178
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* See git history
11+
*******************************************************************************/
12+
13+
package org.eclipse.corrosion.edit;
14+
15+
import org.eclipse.core.runtime.NullProgressMonitor;
16+
import org.eclipse.corrosion.CorrosionPlugin;
17+
import org.eclipse.corrosion.CorrosionPreferenceInitializer;
18+
import org.eclipse.jface.preference.IPreferenceStore;
19+
import org.eclipse.jface.text.IDocument;
20+
import org.eclipse.jface.text.IRegion;
21+
import org.eclipse.lsp4e.LSPEclipseUtils;
22+
import org.eclipse.lsp4e.format.IFormatRegionsProvider;
23+
import org.osgi.service.component.annotations.Component;
24+
25+
@Component(property = { "serverDefinitionId:String=org.eclipse.corrosion.rls" })
26+
public class FormatOnSave implements IFormatRegionsProvider {
27+
28+
@Override
29+
public IRegion[] getFormattingRegions(IDocument document) {
30+
IPreferenceStore store = CorrosionPlugin.getDefault().getPreferenceStore();
31+
var file = LSPEclipseUtils.getFile(document);
32+
if (file != null) {
33+
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE)) {
34+
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE)) {
35+
return IFormatRegionsProvider.allLines(document);
36+
}
37+
if (store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE)) {
38+
return IFormatRegionsProvider.calculateEditedLineRegions(document, new NullProgressMonitor());
39+
}
40+
}
41+
}
42+
return null;
43+
}
44+
45+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.eclipse.corrosion.edit;
2+
3+
import org.eclipse.corrosion.CorrosionPlugin;
4+
import org.eclipse.corrosion.CorrosionPreferenceInitializer;
5+
import org.eclipse.corrosion.Messages;
6+
import org.eclipse.jface.layout.GridDataFactory;
7+
import org.eclipse.jface.layout.GridLayoutFactory;
8+
import org.eclipse.jface.preference.IPreferenceStore;
9+
import org.eclipse.jface.preference.PreferencePage;
10+
import org.eclipse.swt.SWT;
11+
import org.eclipse.swt.events.SelectionAdapter;
12+
import org.eclipse.swt.events.SelectionEvent;
13+
import org.eclipse.swt.layout.GridData;
14+
import org.eclipse.swt.widgets.Button;
15+
import org.eclipse.swt.widgets.Composite;
16+
import org.eclipse.swt.widgets.Control;
17+
import org.eclipse.ui.IWorkbench;
18+
import org.eclipse.ui.IWorkbenchPreferencePage;
19+
20+
public class SaveActionsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
21+
private IPreferenceStore store;
22+
23+
private Button format;
24+
private Button formatAll;
25+
private Button formatEdited;
26+
27+
@Override
28+
public void init(IWorkbench workbench) {
29+
store = CorrosionPlugin.getDefault().getPreferenceStore();
30+
}
31+
32+
@Override
33+
protected Control createContents(Composite parent) {
34+
parent.setLayoutData(new GridData(SWT.NONE, SWT.TOP, true, false));
35+
36+
Composite composite = new Composite(parent, SWT.NONE);
37+
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
38+
composite.setLayout(GridLayoutFactory.fillDefaults().create());
39+
40+
format = createButton(Messages.SaveActionsConfigurationPage_FormatSourceCode,
41+
Messages.SaveActionsConfigurationPage_FormatSourceCode_description, composite, SWT.CHECK, 0);
42+
formatAll = createButton(Messages.SaveActionsConfigurationPage_FormatAllLines,
43+
Messages.SaveActionsConfigurationPage_FormatAllLines_description, composite, SWT.RADIO, 15);
44+
formatEdited = createButton(Messages.SaveActionsConfigurationPage_FormatEditedLines,
45+
Messages.SaveActionsConfigurationPage_FormatEditedLines_description, composite, SWT.RADIO, 15);
46+
47+
format.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE));
48+
formatAll.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE));
49+
formatEdited
50+
.setSelection(store.getBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE));
51+
52+
// listener to update enable state for the radio buttons
53+
format.addSelectionListener(new SelectionAdapter() {
54+
@Override
55+
public void widgetSelected(SelectionEvent e) {
56+
updateEnabledState();
57+
}
58+
});
59+
60+
updateEnabledState();
61+
return parent;
62+
63+
}
64+
65+
@Override
66+
protected void performDefaults() {
67+
format.setSelection(store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE));
68+
formatAll.setSelection(
69+
store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE));
70+
formatEdited.setSelection(
71+
store.getDefaultBoolean(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE));
72+
updateEnabledState();
73+
super.performDefaults();
74+
}
75+
76+
@Override
77+
public boolean performOk() {
78+
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_ON_SAVE_PREFERENCE, format.getSelection());
79+
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_ALL_ON_SAVE_PREFERENCE, formatAll.getSelection());
80+
store.setValue(CorrosionPreferenceInitializer.EDIT_FORMAT_EDITED_ON_SAVE_PREFERENCE,
81+
formatEdited.getSelection());
82+
return true;
83+
}
84+
85+
private static Button createButton(String name, String description, Composite composite, int style,
86+
int horizontalIndent) {
87+
Button button = new Button(composite, style);
88+
button.setLayoutData(GridDataFactory.fillDefaults().indent(horizontalIndent, 0).create());
89+
button.setText(name);
90+
button.setToolTipText(description);
91+
return button;
92+
}
93+
94+
private void updateEnabledState() {
95+
boolean formatOnSaveIsEnabled = format.getSelection();
96+
formatAll.setEnabled(formatOnSaveIsEnabled);
97+
formatEdited.setEnabled(formatOnSaveIsEnabled);
98+
}
99+
}

org.eclipse.corrosion/src/org/eclipse/corrosion/messages.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,10 @@ LaunchHandler_unableToLaunch=Unable to Launch
158158
LaunchHandler_launchingFromCargoRegistryUnsupported=Launching from the central cargo registry is not supported.
159159
LaunchHandler_unsupportedProjectLocation=Unsupported project location. The project must be imported as an Eclipse project.
160160
LaunchHandler_unableToLaunchCommand=Unable to launch command: {0}.
161+
162+
SaveActionsConfigurationPage_FormatSourceCode=Format source code
163+
SaveActionsConfigurationPage_FormatSourceCode_description=Formats source code when file is saved
164+
SaveActionsConfigurationPage_FormatAllLines=Format all lines
165+
SaveActionsConfigurationPage_FormatAllLines_description=Formats all source code lines
166+
SaveActionsConfigurationPage_FormatEditedLines=Format edited lines (currently unsupported by rust-analyzer)
167+
SaveActionsConfigurationPage_FormatEditedLines_description=Formats edited source code lines only

0 commit comments

Comments
 (0)