Skip to content

Commit

Permalink
fixes #2: adjusts formatter to conform to google protobuf style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Jun 28, 2024
1 parent ab2bbea commit 29c66df
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static com.google.inject.name.Names.named;
import static org.eclipse.ui.PlatformUI.isWorkbenchRunning;

import com.google.eclipse.protobuf.formatting.IMaximumLineWidthProvider;
import com.google.eclipse.protobuf.preferences.general.GeneralPreferences;
import com.google.eclipse.protobuf.resource.IResourceVerifier;
import com.google.eclipse.protobuf.scoping.IImportResolver;
Expand All @@ -19,6 +20,8 @@
import com.google.eclipse.protobuf.ui.documentation.ProtobufDocumentationProvider;
import com.google.eclipse.protobuf.ui.editor.FileOutsideWorkspaceIconUpdater;
import com.google.eclipse.protobuf.ui.editor.ProtobufUriEditorOpener;
import com.google.eclipse.protobuf.ui.editor.formatting.CustomPreferenceStoreIndentationInformation;
import com.google.eclipse.protobuf.ui.editor.formatting.EditorMaximumLineWidthProvider;
import com.google.eclipse.protobuf.ui.editor.hyperlinking.ProtobufHyperlinkDetector;
import com.google.eclipse.protobuf.ui.editor.model.ProtobufDocumentProvider;
import com.google.eclipse.protobuf.ui.editor.syntaxcoloring.HighlightingConfiguration;
Expand All @@ -28,6 +31,7 @@
import com.google.eclipse.protobuf.ui.outline.ProtobufOutlinePage;
import com.google.eclipse.protobuf.ui.parser.PreferenceDrivenProtobufParser;
import com.google.eclipse.protobuf.ui.preferences.compiler.CompilerPreferences;
import com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferences;
import com.google.eclipse.protobuf.ui.preferences.editor.ignore.IgnoredExtensionsPreferences;
import com.google.eclipse.protobuf.ui.preferences.editor.numerictag.NumericTagPreferences;
import com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferences;
Expand All @@ -43,6 +47,7 @@
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
import org.eclipse.xtext.formatting.IIndentationInformation;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.parser.IParser;
import org.eclipse.xtext.resource.IResourceServiceProvider;
Expand Down Expand Up @@ -141,6 +146,7 @@ public void configurePreferencesInitializers(Binder binder) {
configurePreferenceInitializer(binder, "numericTagPreferences", NumericTagPreferences.Initializer.class);
configurePreferenceInitializer(binder, "miscellaneousPreferences", MiscellaneousPreferences.Initializer.class);
configurePreferenceInitializer(binder, "pathsPreferences", PathsPreferences.Initializer.class);
configurePreferenceInitializer(binder, "editorPreferences", EditorPreferences.Initializer.class);
configurePreferenceInitializer(binder, "saveActionsPreferences", SaveActionsPreferences.Initializer.class);
}

Expand All @@ -158,4 +164,13 @@ private void configurePreferenceInitializer(Binder binder, String name,
public Class<? extends AbstractAntlrTokenToAttributeIdMapper> bindAbstractAntlrTokenToAttributeIdMapper() {
return ProtobufAntlrTokenToAttributeIdMapper.class;
}

@Override
public Class<? extends IIndentationInformation> bindIIndentationInformation() {
return CustomPreferenceStoreIndentationInformation.class;
}

public Class<? extends IMaximumLineWidthProvider> bindIMaximumLineWidthProvider() {
return EditorMaximumLineWidthProvider.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}
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";
}

}
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class Messages extends NLS {
public static String header;
public static String insertSpacesForTabs;
public static String maximumLineWidth;

static {
// initialize resource bundle
Expand Down
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
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() {}
}
Loading

0 comments on commit 29c66df

Please sign in to comment.