Skip to content

Commit

Permalink
Implement format code on file save option
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke committed Sep 16, 2023
1 parent aaf3f80 commit 121905b
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bundles/org.eclipse.cdt.lsp/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Bundle-Activator: org.eclipse.cdt.lsp.LspPlugin
Bundle-Vendor: %Bundle-Vendor
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.lsp4e;bundle-version="0.17.1",
org.eclipse.lsp4e;bundle-version="0.17.3",
org.eclipse.tm4e.ui,
org.eclipse.tm4e.languageconfiguration,
org.eclipse.ui.genericeditor,
Expand All @@ -37,4 +37,5 @@ Service-Component: OSGI-INF/org.eclipse.cdt.lsp.editor.BuiltinEditorOptionsDefau
OSGI-INF/org.eclipse.cdt.lsp.editor.DefaultConfigurationVisibility.xml,
OSGI-INF/org.eclipse.cdt.lsp.editor.EditorConfigurationAccess.xml,
OSGI-INF/org.eclipse.cdt.lsp.editor.EditorMetadataDefaults.xml,
OSGI-INF/org.eclipse.cdt.lsp.editor.format.FormatOnSave.xml,
OSGI-INF/org.eclipse.cdt.lsp.internal.InitialFileManager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ which is available at https://www.eclipse.org/legal/epl-2.0/\n\
\n\
SPDX-License-Identifier: EPL-2.0\n\
SaveActionsPreferencePage.name=Save Actions
EditorPreferencePage.name=Editor (LSP)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.editor.format.FormatOnSave">
<property name="serverDefinitionId" type="String" value="org.eclipse.cdt.lsp.server"/>
<service>
<provide interface="org.eclipse.lsp4e.format.IFormatRegionsProvider"/>
</service>
<reference cardinality="1..1" field="configuration" interface="org.eclipse.cdt.lsp.editor.Configuration" name="configuration"/>
<implementation class="org.eclipse.cdt.lsp.editor.format.FormatOnSave"/>
</scr:component>
18 changes: 17 additions & 1 deletion bundles/org.eclipse.cdt.lsp/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@
id="org.eclipse.cdt.lsp.editor.preferencePage"
name="%EditorPreferencePage.name">
</page>
<page
name="%SaveActionsPreferencePage.name"
category="org.eclipse.cdt.lsp.editor.preferencePage"
class="org.eclipse.cdt.lsp.editor.SaveActionsConfigurationPage"
id="org.eclipse.cdt.lsp.editor.SaveActionsPreferencePage">
<keywordReference id="org.eclipse.cdt.ui.saveactions"/>
<keywordReference id="org.eclipse.cdt.ui.common"/>
</page>
</extension>
<extension
point="org.eclipse.ui.propertyPages">
Expand All @@ -295,6 +303,14 @@
</adapt>
</enabledWhen>
</page>
</extension>
<page
category="org.eclipse.cdt.lsp.editor.propertyPage"
class="org.eclipse.cdt.lsp.editor.SaveActionsConfigurationPage"
id="org.eclipse.cdt.lsp.editor.SaveActionsPropertyPage"
name="%SaveActionsPreferencePage.name">
<keywordReference id="org.eclipse.cdt.ui.saveactions"/>
<keywordReference id="org.eclipse.cdt.ui.common"/>
</page>
</extension>
</plugin>

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ public boolean preferLspEditor() {
return false;
}

@Override
public boolean formatOnSave() {
return false;
}

@Override
public boolean formatAllLines() {
return true;
}

@Override
public boolean formatEditedLines() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,30 @@ public interface EditorMetadata {
*/
PreferenceMetadata<Boolean> preferLspEditor();

/**
* Returns the metadata for the "Format source code" option, must not return <code>null</code>.
*
* @return the metadata for the "Format source code" option
*
* @see EditorOptions#formatOnSave()
*/
PreferenceMetadata<Boolean> formatOnSave();

/**
* Returns the metadata for the "Format all lines" option, must not return <code>null</code>.
*
* @return the metadata for the "Format all lines" option
*
* @see EditorOptions#formatAllLines()
*/
PreferenceMetadata<Boolean> formatAllLines();

/**
* Returns the metadata for the "Format edited lines" option, must not return <code>null</code>.
*
* @return the metadata for the "Format edited lines" option
*
* @see EditorOptions#formatEditedLines()
*/
PreferenceMetadata<Boolean> formatEditedLines();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ public PreferenceMetadata<Boolean> preferLspEditor() {
LspUiMessages.LspEditorConfigurationPage_preferLspEditor_description);
}

@Override
public PreferenceMetadata<Boolean> formatOnSave() {
return new PreferenceMetadata<>(Boolean.class, //
"format_source", //$NON-NLS-1$
defaults.formatOnSave(), //
LspUiMessages.SaveActionsConfigurationPage_FormatSourceCode,
LspUiMessages.SaveActionsConfigurationPage_FormatSourceCode_description);
}

@Override
public PreferenceMetadata<Boolean> formatAllLines() {
return new PreferenceMetadata<>(Boolean.class, //
"format_all_lines", //$NON-NLS-1$
defaults.formatAllLines(), //
LspUiMessages.SaveActionsConfigurationPage_FormatAllLines,
LspUiMessages.SaveActionsConfigurationPage_FormatAllLines_description);
}

@Override
public PreferenceMetadata<Boolean> formatEditedLines() {
return new PreferenceMetadata<>(Boolean.class, //
"format_edited_lines", //$NON-NLS-1$
defaults.formatEditedLines(), //
LspUiMessages.SaveActionsConfigurationPage_FormatEditedLines,
LspUiMessages.SaveActionsConfigurationPage_FormatEditedLines_description);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ public interface EditorOptions {
*/
boolean preferLspEditor();

/**
* Format source code on file save action
*
* @return if source code should be formatted on file save action
*/
boolean formatOnSave();

/**
* Format all lines in source file
*
* @return if all lines should be formatted
*/
boolean formatAllLines();

/**
* Format edited lines only
*
* @return if only edited lines should be formatted
*/
boolean formatEditedLines();

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ private void initializeDefaults(Configuration configuration) {
EditorMetadata metadata = (EditorMetadata) configuration.metadata();
String qualifier = configuration.qualifier();
initializeBoolean(metadata.preferLspEditor(), qualifier);
initializeBoolean(metadata.formatOnSave(), qualifier);
initializeBoolean(metadata.formatAllLines(), qualifier);
initializeBoolean(metadata.formatEditedLines(), qualifier);
}

private void initializeBoolean(PreferenceMetadata<Boolean> preference, String qualifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public boolean preferLspEditor() {
return booleanValue(metadata.preferLspEditor());
}

@Override
public boolean formatOnSave() {
return booleanValue(metadata.formatOnSave());
}

@Override
public boolean formatAllLines() {
return booleanValue(metadata.formatAllLines());
}

@Override
public boolean formatEditedLines() {
return booleanValue(metadata.formatEditedLines());
}

@Override
public boolean isEnabledFor(IProject project) {
if (enable != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.eclipse.cdt.lsp.editor;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.OsgiPreferenceMetadataStore;
import org.eclipse.jface.layout.GridLayoutFactory;
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;

public class SaveActionsConfigurationArea extends ConfigurationArea {

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

public SaveActionsConfigurationArea(Composite parent, EditorMetadata metadata, boolean isProjectScope) {
super(1);
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(columns).create());

this.format = createButton(metadata.formatOnSave(), composite, SWT.CHECK, 0);
this.formatAll = createButton(metadata.formatAllLines(), composite, SWT.RADIO, 15);
this.formatEdited = createButton(metadata.formatEditedLines(), composite, SWT.RADIO, 15);

final SelectionAdapter formatListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
var selection = format.getSelection();
formatAll.setEnabled(selection);
formatEdited.setEnabled(selection);
}
};
this.format.addSelectionListener(formatListener);
}

@Override
public void load(Object options, boolean enable) {
if (options instanceof EditorOptions editorOptions) {
format.setSelection(editorOptions.formatOnSave());
formatAll.setSelection(editorOptions.formatAllLines());
formatEdited.setSelection(editorOptions.formatEditedLines());
format.setEnabled(enable);
formatAll.setEnabled(enable && format.getSelection());
formatEdited.setEnabled(enable && format.getSelection());
}
}

@Override
public void store(IEclipsePreferences prefs) {
OsgiPreferenceMetadataStore store = new OsgiPreferenceMetadataStore(prefs);
buttons.entrySet().forEach(e -> store.save(e.getValue().getSelection(), e.getKey()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.eclipse.cdt.lsp.editor;

import org.eclipse.swt.widgets.Composite;

public class SaveActionsConfigurationPage extends EditorConfigurationPage {
private final String id = "org.eclipse.cdt.lsp.editor.SaveActionsPreferencePage"; //$NON-NLS-1$

@Override
protected ConfigurationArea getConfigurationArea(Composite composite, boolean isProjectScope) {
return new SaveActionsConfigurationArea(composite, (EditorMetadata) configuration.metadata(), isProjectScope);
}

@Override
protected String getPreferenceId() {
return id;
}

@Override
protected boolean hasProjectSpecificOptions() {
return projectScope()//
.map(p -> p.getNode(configuration.qualifier()))//
.map(n -> n.get(((EditorMetadata) configuration.metadata()).formatAllLines().identifer(), null))//
.isPresent();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2023 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.cdt.lsp.editor.format;

import org.eclipse.cdt.lsp.editor.Configuration;
import org.eclipse.cdt.lsp.editor.EditorOptions;
import org.eclipse.core.runtime.NullProgressMonitor;
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;
import org.osgi.service.component.annotations.Reference;

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

@Reference
private Configuration configuration;

@Override
public IRegion[] getFormattingRegions(IDocument document) {
var file = LSPEclipseUtils.getFile(document);
if (file != null) {
var editorOptions = (EditorOptions) configuration.options(file);
if (editorOptions != null && editorOptions.formatOnSave()) {
if (editorOptions.formatAllLines()) {
return IFormatRegionsProvider.allLines(document);
}
if (editorOptions.formatEditedLines()) {
return IFormatRegionsProvider.calculateEditedLineRegions(document, new NullProgressMonitor());
}
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public class LspUiMessages extends NLS {
public static String LspEditorConfigurationPage_preferLspEditor;
public static String LspEditorConfigurationPage_preferLspEditor_description;

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
Expand Up @@ -18,3 +18,10 @@ LspEditorConfigurationPage_configure_ws_specific=Configure Workspace Settings...
LspEditorConfigurationPage_preferLspEditor=Prefer C/C++ Editor (LSP)
LspEditorConfigurationPage_preferLspEditor_description=Prefer to use language server based C/C++ Editor instead of classic C/C++ Editor

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
SaveActionsConfigurationPage_FormatEditedLines_description=Formats edited source code lines only

0 comments on commit 121905b

Please sign in to comment.