From 8afe49bf7005b314f17006c098b392001990a7c8 Mon Sep 17 00:00:00 2001 From: Phil Middleton Date: Thu, 4 Jun 2015 22:44:40 -0700 Subject: [PATCH] Add format on save option --- .../epic/perleditor/editors/PerlEditor.java | 21 +++++++++++++++++++ .../SourceFormatterPreferencePage.java | 6 +++++- .../SourceFormatterPreferences.java | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/org.epic.perleditor/src/org/epic/perleditor/editors/PerlEditor.java b/org.epic.perleditor/src/org/epic/perleditor/editors/PerlEditor.java index 1039df0c..50e05036 100644 --- a/org.epic.perleditor/src/org/epic/perleditor/editors/PerlEditor.java +++ b/org.epic.perleditor/src/org/epic/perleditor/editors/PerlEditor.java @@ -29,6 +29,7 @@ import org.epic.perleditor.actions.*; import org.epic.perleditor.preferences.MarkOccurrencesPreferences; import org.epic.perleditor.preferences.PreferenceConstants; +import org.epic.perleditor.preferences.SourceFormatterPreferences; import org.epic.perleditor.templates.perl.ModuleCompletionHelper; import org.epic.perleditor.views.PerlOutlinePage; @@ -154,6 +155,7 @@ public void doRevertToSaved() */ public void doSave(IProgressMonitor monitor) { + formatIfEnabled(); super.doSave(monitor); revalidateSyntax(); } @@ -164,10 +166,29 @@ public void doSave(IProgressMonitor monitor) */ public void doSaveAs() { + formatIfEnabled(); super.doSaveAs(); revalidateSyntax(); } + /** + * Run a format source action if enabled. + */ + private void formatIfEnabled() + { + IPreferenceStore store = new ChainedPreferenceStore(new IPreferenceStore[] { + EditorsUI.getPreferenceStore(), + PerlEditorPlugin.getDefault().getPreferenceStore() }); + + boolean formatOnSave = store.getBoolean(SourceFormatterPreferences.FORMAT_ON_SAVE); + + if (formatOnSave) + { + FormatSourceAction fsa = new FormatSourceAction(this); + fsa.run(); + } + } + /** * The PerlEditor implementation of this AbstractTextEditor method performs * sets the input of the outline page after AbstractTextEditor has set diff --git a/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferencePage.java b/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferencePage.java index fc32f5f1..d44bff7b 100644 --- a/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferencePage.java +++ b/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferencePage.java @@ -82,7 +82,11 @@ public void createFieldEditors() { SourceFormatterPreferences.SWALLOW_OPTIONAL_BLANK_LINES, "Swallow optional blank lines", composite)); - + addField( + new BooleanFieldEditor( + SourceFormatterPreferences.FORMAT_ON_SAVE, + "Format on save", + composite)); // addField(new SpacerFieldEditor(composite)); // addField(new LabelFieldEditor("Container tightness:", composite)); diff --git a/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferences.java b/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferences.java index 932660ed..cb9c430e 100644 --- a/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferences.java +++ b/org.epic.perleditor/src/org/epic/perleditor/preferences/SourceFormatterPreferences.java @@ -20,6 +20,7 @@ public class SourceFormatterPreferences { public static final String SWALLOW_OPTIONAL_BLANK_LINES ="Formatter.swallowOptionalBlankLines"; public static final String PERLTIDY_OPTIONS = "Formatter.perltidyOptions"; public static final String HTML_EXPORT_OPTIONS = "Formatter.htmlExportOptions"; + public static final String FORMAT_ON_SAVE = "Formatter.formatOnSave"; /** @@ -44,6 +45,7 @@ public static void initializeDefaultValues(IPreferenceStore store) { // store.setDefault(CONTAINER_TIGHTNESS_SQUARE_BRACKETS, 1); store.setDefault(PERLTIDY_OPTIONS, ""); store.setDefault(HTML_EXPORT_OPTIONS, "-toc"); + store.setDefault(FORMAT_ON_SAVE, false); }