From 9eb9629ea05c140a3b22d7ffba7b0aa6e7150146 Mon Sep 17 00:00:00 2001 From: danthe1st Date: Tue, 31 Dec 2024 16:52:53 +0100 Subject: [PATCH] per-project folding preferences --- org.eclipse.jdt.ui/plugin.xml | 11 +++ .../FoldingConfigurationBlock.java | 36 ++++++++- .../ui/preferences/FoldingPreferencePage.java | 76 +++++++++++++------ .../ui/preferences/PreferencesMessages.java | 1 + .../PreferencesMessages.properties | 1 + .../DefaultJavaFoldingPreferenceBlock.java | 19 ++++- ...avaFoldingStructureProviderDescriptor.java | 7 ++ .../JavaFoldingStructureProviderRegistry.java | 27 +++++-- .../DefaultJavaFoldingStructureProvider.java | 23 +++++- .../IScopedJavaFoldingPreferenceBlock.java | 17 +++++ 10 files changed, 183 insertions(+), 35 deletions(-) create mode 100644 org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/IScopedJavaFoldingPreferenceBlock.java diff --git a/org.eclipse.jdt.ui/plugin.xml b/org.eclipse.jdt.ui/plugin.xml index 5c21a366586..07ebbca5751 100644 --- a/org.eclipse.jdt.ui/plugin.xml +++ b/org.eclipse.jdt.ui/plugin.xml @@ -1055,6 +1055,17 @@ + + + + + + + fProviderPreferences; private final Map fProviderControls; + private IScopeContext fContext; - public FoldingConfigurationBlock(OverlayPreferenceStore store) { + private boolean fIsProjectPreferencePage; + + + public FoldingConfigurationBlock(OverlayPreferenceStore store, IScopeContext context, boolean isProjectPreferencePage) { Assert.isNotNull(store); fStore= store; fStore.addKeys(createOverlayStoreKeys()); fProviderDescriptors= createListModel(); fProviderPreferences= new HashMap<>(); fProviderControls= new HashMap<>(); + this.fContext= context; + this.fIsProjectPreferencePage= isProjectPreferencePage; } private Map createListModel() { @@ -289,6 +298,14 @@ void updateListDependencies() { if (prefs == null) { try { prefs= desc.createPreferences(); + if (fIsProjectPreferencePage) { + if (prefs instanceof IScopedJavaFoldingPreferenceBlock scopedPrefs) { + scopedPrefs.setScopeContext(fContext); + } else { + String message= Messages.format(PreferencesMessages.FoldingConfigurationBlock_error_project_prefs_not_supported, desc.getName()); + prefs= new ErrorPreferences(message); + } + } fProviderPreferences.put(id, prefs); } catch (CoreException e) { JavaPlugin.log(e); @@ -369,4 +386,19 @@ private void restoreFromPreferences() { else fProviderViewer.setSelection(new StructuredSelection(provider), true); } + + @Override + public void disableProjectSettings() { + if(fContext != null) { + fContext.getNode(JavaUI.ID_PLUGIN).remove(PreferenceConstants.EDITOR_FOLDING_ENABLED); + } + } + + @Override + public void enableProjectSettings() { + if(fContext != null) { + fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED, fStore.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED)); + } + + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FoldingPreferencePage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FoldingPreferencePage.java index ed17e5a27c9..7a5ad35bd45 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FoldingPreferencePage.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FoldingPreferencePage.java @@ -15,8 +15,14 @@ package org.eclipse.jdt.internal.ui.preferences; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Label; +import org.eclipse.core.runtime.preferences.IScopeContext; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ProjectScope; + +import org.eclipse.ui.preferences.ScopedPreferenceStore; + +import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; import org.eclipse.jdt.internal.ui.JavaPlugin; @@ -24,44 +30,70 @@ /** * The page for setting the editor options. */ -public final class FoldingPreferencePage extends AbstractConfigurationBlockPreferencePage { +public final class FoldingPreferencePage extends AbstractConfigurationBlockPreferenceAndPropertyPage { + + public static final String PROPERTY_PAGE_ID= "org.eclipse.jdt.ui.propertyPages.FoldingPreferencePage"; //$NON-NLS-1$ + public static final String PREFERENCE_PAGE_ID= "org.eclipse.jdt.ui.preferences.FoldingPreferencePage"; //$NON-NLS-1$ + private OverlayPreferenceStore fOverlayStore; + + + public FoldingPreferencePage() { + setDescription(PreferencesMessages.JavaEditorPreferencePage_folding_title); + } /* - * @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#getHelpId() + * @see org.eclipse.jdt.internal.ui.preferences.AbstractConfigurationBlockPreferenceAndPropertyPage#getHelpId() */ @Override protected String getHelpId() { return IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE; } - /* - * @see org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setDescription() - */ @Override - protected void setDescription() { - String description= PreferencesMessages.JavaEditorPreferencePage_folding_title; - setDescription(description); + protected IPreferenceAndPropertyConfigurationBlock createConfigurationBlock(IScopeContext context) { + ScopedPreferenceStore scopedStore= new ScopedPreferenceStore(context, JavaUI.ID_PLUGIN); + fOverlayStore= new OverlayPreferenceStore( + scopedStore, + new OverlayPreferenceStore.OverlayKey[] {}); + FoldingConfigurationBlock foldingConfigurationBlock= new FoldingConfigurationBlock(fOverlayStore, context, isProjectPreferencePage()); + fOverlayStore.load(); + fOverlayStore.start(); + return foldingConfigurationBlock; } - /* - * @see org.org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setPreferenceStore() - */ @Override - protected void setPreferenceStore() { - setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore()); + protected boolean hasProjectSpecificOptions(IProject project) { + return JavaPlugin.getDefault().getFoldingStructureProviderRegistry().hasProjectSpecificOptions(new ProjectScope(project)); } + @Override + protected String getPreferencePageID() { + return PREFERENCE_PAGE_ID; + } @Override - protected Label createDescriptionLabel(Composite parent) { - return null; // no description for new look. + protected String getPropertyPageID() { + return PROPERTY_PAGE_ID; } - /* - * @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#createConfigurationBlock(org.eclipse.ui.internal.editors.text.OverlayPreferenceStore) - */ @Override - protected IPreferenceConfigurationBlock createConfigurationBlock(OverlayPreferenceStore overlayPreferenceStore) { - return new FoldingConfigurationBlock(overlayPreferenceStore); + public boolean performOk() { + boolean result= super.performOk(); + fOverlayStore.propagate(); + return result; } + + @Override + public void performDefaults() { + fOverlayStore.loadDefaults(); + super.performDefaults(); + + } + + @Override + public void dispose() { + super.dispose(); + fOverlayStore.stop(); + } + } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.java index a1886d94f9c..2f1ee7fd03d 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.java @@ -773,6 +773,7 @@ private PreferencesMessages() { public static String FoldingConfigurationBlock_combo_caption; public static String FoldingConfigurationBlock_info_no_preferences; public static String FoldingConfigurationBlock_error_not_exist; + public static String FoldingConfigurationBlock_error_project_prefs_not_supported; public static String FoldingConfigurationBlock_warning_providerNotFound_resetToDefault; public static String PropertiesFileEditorPreferencePage_key; public static String PropertiesFileEditorPreferencePage_value; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.properties index 6c16e9b0575..ccd147b642c 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.properties +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/PreferencesMessages.properties @@ -892,6 +892,7 @@ FoldingConfigurationBlock_enable= Enable f&olding FoldingConfigurationBlock_combo_caption= Select folding to &use: FoldingConfigurationBlock_info_no_preferences= The selected folding provider did not provide a preference control FoldingConfigurationBlock_error_not_exist= The ''{0}'' folding provider does not exist. +FoldingConfigurationBlock_error_project_prefs_not_supported=The ''{0}'' folding provider does not support project specific preferences. FoldingConfigurationBlock_warning_providerNotFound_resetToDefault= The ''{0}'' folding provider could not be found. Resetting to the default folding provider. # Properties File Editor diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/DefaultJavaFoldingPreferenceBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/DefaultJavaFoldingPreferenceBlock.java index f9f5e0e3ba2..926c930ee21 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/DefaultJavaFoldingPreferenceBlock.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/DefaultJavaFoldingPreferenceBlock.java @@ -30,10 +30,15 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; +import org.eclipse.core.runtime.preferences.IScopeContext; + import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.preferences.ScopedPreferenceStore; + +import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.ui.PreferenceConstants; -import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock; +import org.eclipse.jdt.ui.text.folding.IScopedJavaFoldingPreferenceBlock; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.preferences.OverlayPreferenceStore; @@ -45,7 +50,7 @@ * * @since 3.0 */ -public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock { +public class DefaultJavaFoldingPreferenceBlock implements IScopedJavaFoldingPreferenceBlock { private IPreferenceStore fStore; private OverlayPreferenceStore fOverlayStore; @@ -75,6 +80,16 @@ public DefaultJavaFoldingPreferenceBlock() { fOverlayStore= new OverlayPreferenceStore(fStore, fKeys); } + @Override + public void setScopeContext(IScopeContext context) { + if(context == null) { + fStore = JavaPlugin.getDefault().getPreferenceStore(); + } else { + fStore= new ScopedPreferenceStore(context, JavaUI.ID_PLUGIN); + } + fOverlayStore= new OverlayPreferenceStore(fStore, fKeys); + } + private OverlayKey[] createKeys() { ArrayList overlayKeys= new ArrayList<>(); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderDescriptor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderDescriptor.java index 1031717f02e..53610ebcf40 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderDescriptor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderDescriptor.java @@ -16,7 +16,10 @@ import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jdt.ui.PreferenceConstants; import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock; import org.eclipse.jdt.ui.text.folding.IJavaFoldingStructureProvider; @@ -116,4 +119,8 @@ public String getId() { public String getName() { return fName; } + + public boolean hasProjectSpecificOptions(IScopeContext context) { + return context.getNode(JavaUI.ID_PLUGIN).get(PreferenceConstants.EDITOR_FOLDING_ENABLED, null) != null; + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderRegistry.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderRegistry.java index dad8824488c..ad12210f8d8 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderRegistry.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/folding/JavaFoldingStructureProviderRegistry.java @@ -24,6 +24,7 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IScopeContext; import org.eclipse.jface.preference.IPreferenceStore; @@ -87,6 +88,17 @@ public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(Strin * @return the current provider according to the preferences */ public IJavaFoldingStructureProvider getCurrentFoldingProvider() { + JavaFoldingStructureProviderDescriptor desc= getCurrentFoldingStructureProviderDescriptor(); + + try { + return desc.createProvider(); + } catch (CoreException e) { + JavaPlugin.log(e); + return null; + } + } + + private JavaFoldingStructureProviderDescriptor getCurrentFoldingStructureProviderDescriptor() { IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); String currentProviderId= preferenceStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER); JavaFoldingStructureProviderDescriptor desc= getFoldingProviderDescriptor(currentProviderId); @@ -103,13 +115,7 @@ public IJavaFoldingStructureProvider getCurrentFoldingProvider() { preferenceStore.setToDefault(PreferenceConstants.EDITOR_FOLDING_PROVIDER); } - - try { - return desc.createProvider(); - } catch (CoreException e) { - JavaPlugin.log(e); - return null; - } + return desc; } /** @@ -142,4 +148,11 @@ public void reloadExtensions() { } } + public synchronized boolean hasProjectSpecificOptions(IScopeContext context) { + synchronized(this) { + ensureRegistered(); + } + + return getCurrentFoldingStructureProviderDescriptor().hasProjectSpecificOptions(context); + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/DefaultJavaFoldingStructureProvider.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/DefaultJavaFoldingStructureProvider.java index 61ae6e806a5..fd4a9ea079a 100755 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/DefaultJavaFoldingStructureProvider.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/text/folding/DefaultJavaFoldingStructureProvider.java @@ -30,6 +30,9 @@ import org.eclipse.core.runtime.Assert; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ProjectScope; + import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.BadLocationException; @@ -45,6 +48,8 @@ import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; import org.eclipse.jface.text.source.projection.ProjectionViewer; +import org.eclipse.ui.preferences.ScopedPreferenceStore; + import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.ITextEditor; @@ -72,6 +77,7 @@ import org.eclipse.jdt.core.compiler.InvalidInputException; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.ui.PreferenceConstants; import org.eclipse.jdt.internal.ui.JavaPlugin; @@ -922,7 +928,17 @@ private IJavaElement getInputElement() { } private void initializePreferences() { - IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); + IProject project= EditorUtility.getJavaProject(fEditor).getProject(); + IPreferenceStore store = null; + if (project != null) { + ProjectScope scope= new ProjectScope(project); + if (scope.getNode(JavaUI.ID_PLUGIN).get(PreferenceConstants.EDITOR_FOLDING_ENABLED, null) != null) { + store= new ScopedPreferenceStore(scope, JavaUI.ID_PLUGIN); + } + } + if (store == null){ + store= JavaPlugin.getDefault().getPreferenceStore(); + } fCollapseInnerTypes= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_INNERTYPES); fCollapseImportContainer= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_IMPORTS); fCollapseJavadoc= store.getBoolean(PreferenceConstants.EDITOR_FOLDING_JAVADOC); @@ -1306,7 +1322,10 @@ private int findPossibleRegionCommentStart(IScanner scanner, int token) { } private boolean startsWith(char[] source, int offset, int length, char[] prefix) { - for(int i=0;i