-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #2: adjusts formatter to conform to google protobuf style guide
- Loading branch information
Showing
11 changed files
with
401 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,67 @@ | |
*/ | ||
package com.google.eclipse.protobuf.ui.editor; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jface.text.DefaultLineTracker; | ||
import org.eclipse.jface.text.ITextViewerExtension7; | ||
import org.eclipse.jface.text.TabsToSpacesConverter; | ||
import org.eclipse.jface.text.source.ISourceViewer; | ||
import org.eclipse.jface.text.source.SourceViewerConfiguration; | ||
import org.eclipse.jface.util.PropertyChangeEvent; | ||
import org.eclipse.swt.widgets.Composite; | ||
import org.eclipse.ui.IEditorInput; | ||
import org.eclipse.ui.IEditorSite; | ||
import org.eclipse.ui.PartInitException; | ||
import org.eclipse.xtext.ui.editor.XtextEditor; | ||
import org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration; | ||
|
||
import com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferences; | ||
import com.google.inject.Inject; | ||
|
||
/** | ||
* @author [email protected] (Alex Ruiz) | ||
*/ | ||
public class ProtobufEditor extends XtextEditor { | ||
|
||
@Inject EditorPreferences preferences; | ||
|
||
@Override | ||
protected void handlePreferenceStoreChanged(PropertyChangeEvent event) { | ||
super.handlePreferenceStoreChanged(event); | ||
|
||
fixTabsToSpaces(); | ||
} | ||
|
||
@Override | ||
public void createPartControl(Composite parent) { | ||
super.createPartControl(parent); | ||
|
||
// Needed because super.createPartControl will call initializeSourceViewer | ||
// which sets this setting and which we cannot override | ||
fixTabsToSpaces(); | ||
} | ||
|
||
|
||
protected void fixTabsToSpaces() { | ||
// We have our own tabs-to-spaces setting in order to comply with protolint style guide | ||
if(preferences.shouldInsertSpacesForTabs()) { | ||
installTabsToSpacesConverter(); | ||
} else { | ||
uninstallTabsToSpacesConverter(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void installTabsToSpacesConverter() { | ||
// Code mostly duplicated from superclass; overridden to fix indent to 2 spaces | ||
if (getSourceViewer() instanceof ITextViewerExtension7) { | ||
int tabWidth= 2; // The protobuf indent is fixed to 2 by style guide | ||
TabsToSpacesConverter tabToSpacesConverter= new TabsToSpacesConverter(); | ||
tabToSpacesConverter.setLineTracker(new DefaultLineTracker()); | ||
tabToSpacesConverter.setNumberOfSpacesPerTab(tabWidth); | ||
((ITextViewerExtension7) getSourceViewer()).setTabsToSpacesConverter(tabToSpacesConverter); | ||
updateIndentPrefixes(); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...le/eclipse/protobuf/ui/editor/formatting/CustomPreferenceStoreIndentationInformation.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,38 @@ | ||
package com.google.eclipse.protobuf.ui.editor.formatting; | ||
|
||
import org.eclipse.jface.util.IPropertyChangeListener; | ||
import org.eclipse.jface.util.PropertyChangeEvent; | ||
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; | ||
import org.eclipse.xtext.formatting.IIndentationInformation; | ||
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess; | ||
|
||
import com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferences; | ||
import com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferences; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.google.inject.Singleton; | ||
|
||
@Singleton | ||
public class CustomPreferenceStoreIndentationInformation implements IIndentationInformation { | ||
|
||
@Inject private Provider<EditorPreferences> preferencesProvider; | ||
|
||
public int getTabWidth() { | ||
return 2; // storeAccess.getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); | ||
} | ||
|
||
public boolean isSpacesForTab() { | ||
return preferencesProvider.get().shouldInsertSpacesForTabs(); | ||
} | ||
|
||
// note: the maximum length allowed in the eclipse preferences dialog is 16 | ||
private final String WS = " "; | ||
|
||
private String indentString = null; | ||
|
||
@Override | ||
public synchronized String getIndentString() { | ||
return isSpacesForTab() ? " " : "\t"; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
.../src/com/google/eclipse/protobuf/ui/editor/formatting/EditorMaximumLineWidthProvider.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,19 @@ | ||
package com.google.eclipse.protobuf.ui.editor.formatting; | ||
|
||
import com.google.eclipse.protobuf.formatting.IMaximumLineWidthProvider; | ||
import com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferences; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.google.inject.Singleton; | ||
|
||
@Singleton | ||
public class EditorMaximumLineWidthProvider implements IMaximumLineWidthProvider { | ||
|
||
@Inject private Provider<EditorPreferences> preferencesProvider; | ||
|
||
@Override | ||
public int maximumLineWidth() { | ||
return preferencesProvider.get().maximumLineWidth(); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -12,32 +12,53 @@ | |
import static org.eclipse.ui.dialogs.PreferencesUtil.createPreferenceDialogOn; | ||
|
||
import static com.google.eclipse.protobuf.ui.preferences.editor.general.Messages.header; | ||
import static com.google.eclipse.protobuf.ui.preferences.editor.general.Messages.insertSpacesForTabs; | ||
import static com.google.eclipse.protobuf.ui.preferences.editor.general.PreferenceNames.INSERT_SPACES_FOR_TABS; | ||
import static com.google.eclipse.protobuf.ui.preferences.pages.binding.BindingToButtonSelection.bindSelectionOf; | ||
|
||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.jface.preference.IntegerFieldEditor; | ||
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.layout.GridLayout; | ||
import org.eclipse.swt.widgets.Button; | ||
import org.eclipse.swt.widgets.Composite; | ||
import org.eclipse.swt.widgets.Control; | ||
import org.eclipse.swt.widgets.Event; | ||
import org.eclipse.swt.widgets.Link; | ||
import org.eclipse.swt.widgets.Listener; | ||
import org.eclipse.ui.IWorkbench; | ||
import org.eclipse.ui.IWorkbenchPreferencePage; | ||
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess; | ||
|
||
import com.google.eclipse.protobuf.ui.preferences.pages.binding.PreferenceBinder; | ||
import com.google.eclipse.protobuf.ui.preferences.pages.binding.PreferenceFactory; | ||
import com.google.inject.Inject; | ||
|
||
/** | ||
* General editor preferences. | ||
* | ||
* @author [email protected] (Alex Ruiz) | ||
*/ | ||
public class EditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { | ||
@Inject private IPreferenceStoreAccess preferenceStoreAccess; | ||
|
||
private final PreferenceBinder preferenceBinder = new PreferenceBinder(); | ||
|
||
private Button btnInsertSpacesForTabs; | ||
private IntegerFieldEditor fMaximumLineWidth; | ||
|
||
@Override protected Control createContents(Composite parent) { | ||
Composite contents = new Composite(parent, NONE); | ||
contents.setLayout(new GridLayout(1, false)); | ||
|
||
Link link = new Link(contents, SWT.NONE); | ||
GridData gridData = new GridData(SWT.FILL, SWT.BEGINNING, true, false); | ||
gridData.widthHint = 150; // only expand further if anyone else requires it | ||
gridData.horizontalSpan = 2; | ||
link.setLayoutData(gridData); | ||
link.setText(header); | ||
link.addListener(SWT.Selection, new Listener() { | ||
|
@@ -46,8 +67,63 @@ public class EditorPreferencePage extends PreferencePage implements IWorkbenchPr | |
createPreferenceDialogOn(getShell(), text, null, null); | ||
} | ||
}); | ||
|
||
btnInsertSpacesForTabs = new Button(contents, SWT.CHECK); | ||
btnInsertSpacesForTabs.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 2, 1)); | ||
btnInsertSpacesForTabs.setText(insertSpacesForTabs); | ||
|
||
fMaximumLineWidth = new IntegerFieldEditor(PreferenceNames.MAXIMUM_LINE_WIDTH, Messages.maximumLineWidth, contents); | ||
fMaximumLineWidth.setValidRange(80, 1000); | ||
fMaximumLineWidth.setPage(this); | ||
fMaximumLineWidth.setPreferenceStore(getPreferenceStore()); | ||
fMaximumLineWidth.load(); | ||
|
||
setUpBinding(); | ||
preferenceBinder.applyValues(); | ||
updateContents(); | ||
addEventListeners(); | ||
|
||
return contents; | ||
} | ||
|
||
private void setUpBinding() { | ||
PreferenceFactory factory = new PreferenceFactory(getPreferenceStore()); | ||
preferenceBinder.addAll( | ||
bindSelectionOf(btnInsertSpacesForTabs).to(factory.newBooleanPreference(INSERT_SPACES_FOR_TABS)) | ||
); | ||
} | ||
|
||
private void addEventListeners() { | ||
btnInsertSpacesForTabs.addSelectionListener(new SelectionAdapter() { | ||
@Override public void widgetSelected(SelectionEvent e) { | ||
updateContents(); | ||
} | ||
}); | ||
} | ||
|
||
private void updateContents() { | ||
//boolean enabled = btnInsertSpacesForTabs.getSelection(); | ||
//btnInAllLines.setEnabled(enabled); | ||
//btnInEditedLines.setEnabled(enabled); | ||
} | ||
|
||
@Override protected IPreferenceStore doGetPreferenceStore() { | ||
return preferenceStoreAccess.getWritablePreferenceStore(); | ||
} | ||
|
||
@Override public boolean performOk() { | ||
fMaximumLineWidth.store(); | ||
preferenceBinder.saveValues(); | ||
|
||
return true; | ||
} | ||
|
||
@Override protected void performDefaults() { | ||
preferenceBinder.applyDefaults(); | ||
fMaximumLineWidth.loadDefault(); | ||
super.performDefaults(); | ||
updateContents(); | ||
} | ||
|
||
@Override public void init(IWorkbench workbench) {} | ||
} |
45 changes: 45 additions & 0 deletions
45
...f.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/general/EditorPreferences.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,45 @@ | ||
/* | ||
* Copyright (c) 2011 Google Inc. | ||
* | ||
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package com.google.eclipse.protobuf.ui.preferences.editor.general; | ||
|
||
import static com.google.eclipse.protobuf.ui.preferences.editor.general.PreferenceNames.INSERT_SPACES_FOR_TABS; | ||
import static com.google.eclipse.protobuf.ui.preferences.editor.general.PreferenceNames.MAXIMUM_LINE_WIDTH; | ||
|
||
import com.google.eclipse.protobuf.preferences.DefaultPreservingInitializer; | ||
import com.google.inject.Inject; | ||
|
||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess; | ||
|
||
/** | ||
* @author Rene Schneider | ||
*/ | ||
public class EditorPreferences { | ||
private final IPreferenceStore store; | ||
|
||
@Inject public EditorPreferences(IPreferenceStoreAccess storeAccess) { | ||
this.store = storeAccess.getWritablePreferenceStore(); | ||
} | ||
|
||
public boolean shouldInsertSpacesForTabs() { | ||
return store.getBoolean(INSERT_SPACES_FOR_TABS); | ||
} | ||
|
||
public int maximumLineWidth() { | ||
return store.getInt(MAXIMUM_LINE_WIDTH); | ||
} | ||
|
||
public static class Initializer extends DefaultPreservingInitializer { | ||
@Override | ||
public void setDefaults() { | ||
setDefault(INSERT_SPACES_FOR_TABS, true); | ||
setDefault(MAXIMUM_LINE_WIDTH, 120); | ||
} | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...obuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/general/Messages.properties
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
header=Protocol Buffer Editor Preferences. General preferences may be set via <a href="org.eclipse.ui.preferencePages.GeneralTextEditor">Text Editors</a>. | ||
insertSpacesForTabs=Insert spaces for tabs | ||
maximumLineWidth=Maximum line width |
19 changes: 19 additions & 0 deletions
19
...buf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/general/PreferenceNames.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,19 @@ | ||
/* | ||
* Copyright (c) 2012 Google Inc. | ||
* | ||
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse | ||
* Public License v1.0 which accompanies this distribution, and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package com.google.eclipse.protobuf.ui.preferences.editor.general; | ||
|
||
/** | ||
* @author Rene Schneider | ||
*/ | ||
final class PreferenceNames { | ||
static final String INSERT_SPACES_FOR_TABS = "editor.insertSpacesForTabs"; | ||
static final String MAXIMUM_LINE_WIDTH = "editor.maximumLineWidth"; | ||
|
||
private PreferenceNames() {} | ||
} |
Oops, something went wrong.