Skip to content

Commit

Permalink
per-project folding preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Dec 31, 2024
1 parent 308cb73 commit 56cd9af
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 35 deletions.
11 changes: 11 additions & 0 deletions org.eclipse.jdt.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,17 @@
<keywordReference id="org.eclipse.jdt.ui.saveparticipant"/>
</page>

<page
name="%editorFoldingPage"
class="org.eclipse.jdt.internal.ui.preferences.FoldingPreferencePage"
category="org.eclipse.jdt.ui.propertyPages.JavaEditorPropertyPage"
id="org.eclipse.jdt.ui.propertyPages.FoldingPreferencePage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature" value="org.eclipse.jdt.core.javanature"/>
</adapt>
</enabledWhen>
</page>
<page
name="%classpathContainerPageName"
class="org.eclipse.jdt.internal.ui.preferences.ClasspathContainerPreferencePage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.PixelConverter;
Expand All @@ -46,8 +47,10 @@

import org.eclipse.jdt.internal.corext.util.Messages;

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.OverlayKey;
Expand All @@ -61,7 +64,7 @@
*
* @since 3.0
*/
class FoldingConfigurationBlock implements IPreferenceConfigurationBlock {
class FoldingConfigurationBlock implements IPreferenceAndPropertyConfigurationBlock {

private static class ErrorPreferences implements IJavaFoldingPreferenceBlock {
private String fMessage;
Expand Down Expand Up @@ -117,14 +120,17 @@ public void dispose() {
private final Map<String, IJavaFoldingPreferenceBlock> fProviderPreferences;
private final Map<String, Control> fProviderControls;

private IScopeContext fContext;

public FoldingConfigurationBlock(OverlayPreferenceStore store) {

public FoldingConfigurationBlock(OverlayPreferenceStore store, IScopeContext context) {
Assert.isNotNull(store);
fStore= store;
fStore.addKeys(createOverlayStoreKeys());
fProviderDescriptors= createListModel();
fProviderPreferences= new HashMap<>();
fProviderControls= new HashMap<>();
this.fContext= context;
}

private Map<String, JavaFoldingStructureProviderDescriptor> createListModel() {
Expand Down Expand Up @@ -296,6 +302,9 @@ void updateListDependencies() {
}
}
}
if(prefs instanceof IScopedJavaFoldingPreferenceBlock scopedPrefs && fContext != null) {
scopedPrefs.setScopeContext(fContext);
}

Control control= fProviderControls.get(id);
if (control == null) {
Expand Down Expand Up @@ -369,4 +378,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));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,85 @@
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;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +50,7 @@
*
* @since 3.0
*/
public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock {
public class DefaultJavaFoldingPreferenceBlock implements IScopedJavaFoldingPreferenceBlock {

private IPreferenceStore fStore;
private OverlayPreferenceStore fOverlayStore;
Expand Down Expand Up @@ -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<OverlayKey> overlayKeys= new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -142,4 +148,11 @@ public void reloadExtensions() {
}
}

public synchronized boolean hasProjectSpecificOptions(IScopeContext context) {
synchronized(this) {
ensureRegistered();
}

return getCurrentFoldingStructureProviderDescriptor().hasProjectSpecificOptions(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Math.min(length, prefix.length);i++) {
if (length < prefix.length) {
return false;
}
for(int i=0;i<prefix.length;i++) {
if (source[offset+i] != prefix[i]) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.eclipse.jdt.ui.text.folding;

import org.eclipse.core.runtime.preferences.IScopeContext;

/**
* Extends {@link IJavaFoldingPreferenceBlock} for supporting preferences in a given scope (e.g. projects).
* @since 3.34
*/
public interface IScopedJavaFoldingPreferenceBlock extends IJavaFoldingPreferenceBlock {
/**
* Marks this preference block to be configured in an {@link IScopeContext}.
* @param context The scope context the preferences apply to.
*/
default void setScopeContext(IScopeContext context) {

}
}

0 comments on commit 56cd9af

Please sign in to comment.