forked from eclipse-cdt/cdt-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement format code on file save option
fixes eclipse-cdt#178
- Loading branch information
1 parent
aaf3f80
commit 121905b
Showing
15 changed files
with
282 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
bundles/org.eclipse.cdt.lsp/OSGI-INF/org.eclipse.cdt.lsp.editor.format.FormatOnSave.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/editor/SaveActionsConfigurationArea.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/editor/SaveActionsConfigurationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/editor/format/FormatOnSave.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters