diff options
author | angelozerr | 2018-07-31 19:17:36 +0000 |
---|---|---|
committer | Sarika Sinha | 2018-11-20 09:51:06 +0000 |
commit | 5adfd41ede0e57898fc2e990b69332e4a0b8eaef (patch) | |
tree | 50b3d0e8dc13e21b5fb5b04a595be9568b755037 | |
parent | c041f9e6c97f4b11a01b3287b7776cfcabdc403c (diff) | |
download | eclipse.jdt.ui-5adfd41ede0e57898fc2e990b69332e4a0b8eaef.tar.gz eclipse.jdt.ui-5adfd41ede0e57898fc2e990b69332e4a0b8eaef.tar.xz eclipse.jdt.ui-5adfd41ede0e57898fc2e990b69332e4a0b8eaef.zip |
Bug 529127 - [code mining] Provide Java References/ImplementationI20181120-1800I20181120-0600
CodeMinings
Change-Id: Ib6a4234ea0a9c30241c5f26d1275f5e8d3649518
Signed-off-by: angelozerr <angelo.zerr@gmail.com>
20 files changed, 1065 insertions, 30 deletions
diff --git a/org.eclipse.jdt.ui/META-INF/MANIFEST.MF b/org.eclipse.jdt.ui/META-INF/MANIFEST.MF index c33bf6b0cb..2976dcf8c1 100644 --- a/org.eclipse.jdt.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.ui/META-INF/MANIFEST.MF @@ -58,6 +58,7 @@ Export-Package: org.eclipse.jdt.internal.corext;x-friends:="org.eclipse.jdt.juni org.eclipse.jdt.internal.ui.javadocexport;x-internal:=true, org.eclipse.jdt.internal.ui.javaeditor;x-friends:="org.eclipse.jdt.junit,org.eclipse.jdt.debug.ui", org.eclipse.jdt.internal.ui.javaeditor.breadcrumb;x-internal:=true, + org.eclipse.jdt.internal.ui.javaeditor.codemining;x-internal:=true, org.eclipse.jdt.internal.ui.javaeditor.saveparticipant;x-internal:=true, org.eclipse.jdt.internal.ui.javaeditor.selectionactions;x-internal:=true, org.eclipse.jdt.internal.ui.model;x-internal:=true, diff --git a/org.eclipse.jdt.ui/plugin.properties b/org.eclipse.jdt.ui/plugin.properties index 5d064411b1..54e773e52b 100644 --- a/org.eclipse.jdt.ui/plugin.properties +++ b/org.eclipse.jdt.ui/plugin.properties @@ -13,6 +13,7 @@ # Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 83258 [jar exporter] Deploy java application as executable jar # Troy Bishop <tjbishop@ca.ibm.com> - Add support for importing/exporting Java Code Style preferences - https://bugs.eclipse.org/bugs/show_bug.cgi?id=304395 # Eugene Lucash <e.lucash@gmail.com> - [quick assist] Add key binding for Extract method Quick Assist - https://bugs.eclipse.org/424166 +# Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 ############################################################################### pluginName= Java Development Tools UI @@ -131,6 +132,7 @@ preferenceKeywords.classpathcontainer=Java classpath container buildpath jre preferenceKeywords.wizards.java=Java preferenceKeywords.jarpackager=Java archive application +preferenceKeywords.codemining=Java editor code minings preferenceTransfer.java.compiler.name= Java Compiler Preferences preferenceTransfer.java.compiler.description= Java > Compiler preferences. preferenceTransfer.java.appearance.name= Java Appearance Preferences @@ -191,6 +193,7 @@ contentAssistPageName= Content Assist contentAssistAdvancedName= Advanced contentAssistFavoritesName= Favorites +editorCodeMiningPageName=Code Minings editorHoversPageName=Hovers editorSyntaxColoringPage=Syntax Coloring editorFoldingPage=Folding @@ -1191,3 +1194,5 @@ structureMergeViewers.java.label=Java Structure Compare projectConfigurator.java = Java projectConfigurator.jdt = JDT + +JavaElementCodeMiningProvider.label=Java Element code mining provider diff --git a/org.eclipse.jdt.ui/plugin.xml b/org.eclipse.jdt.ui/plugin.xml index 5822d29126..691a6c33b0 100644 --- a/org.eclipse.jdt.ui/plugin.xml +++ b/org.eclipse.jdt.ui/plugin.xml @@ -15,7 +15,8 @@ Francis Upton IV, Oakland Software <francisu@ieee.org> - Add support for Go Into with the ProjectExplorer - https://bugs.eclipse.org/bugs/show_bug.cgi?id=262091 Troy Bishop <tjbishop@ca.ibm.com> - Add support for importing/exporting Java Code Style preferences - https://bugs.eclipse.org/bugs/show_bug.cgi?id=304395 Eugene Lucash <e.lucash@gmail.com> - [quick assist] Add key binding for Extract method Quick Assist - https://bugs.eclipse.org/424166 - Frits Jalvingh <jal@etc.to> - Contribution for Bug 459831 - [launching] Support attaching external annotations to a JRE container + Frits Jalvingh <jal@etc.to> - Contribution for Bug 459831 - [launching] Support attaching external annotations to a JRE container + Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 --> <plugin> @@ -1740,8 +1741,15 @@ id="org.eclipse.jdt.ui.preferences.MembersOrderPreferencePage"> <keywordReference id="org.eclipse.jdt.ui.sortorder"/> </page> + <page + name="%editorCodeMiningPageName" + category="org.eclipse.jdt.ui.preferences.JavaEditorPreferencePage" + class="org.eclipse.jdt.internal.ui.preferences.JavaEditorCodeMiningPreferencePage" + id="org.eclipse.jdt.ui.preferences.JavaEditorCodeMiningPreferencePage"> + <keywordReference id="org.eclipse.jdt.ui.codemining"/> + </page> </extension> - + <extension point="org.eclipse.ui.keywords"> <keyword @@ -1831,14 +1839,16 @@ <keyword label="%preferenceKeywords.classpathcontainer" id="org.eclipse.jdt.ui.classpathcontainer"/> - <keyword label="%preferenceKeywords.wizards.java" id="org.eclipse.jdt.ui.wizards.java"/> <keyword label="%preferenceKeywords.jarpackager" id="org.eclipse.jdt.ui.jarpackager"/> - </extension> + <keyword + label="%preferenceKeywords.codemining" + id="org.eclipse.jdt.ui.codemining"/> + </extension> <!-- =========================================================================== --> <!-- Work in Progress --> <!-- =========================================================================== --> @@ -7141,4 +7151,42 @@ </action> </objectContribution> </extension> + + <extension + point="org.eclipse.core.expressions.propertyTesters"> + <propertyTester + id="org.eclipse.jdt.ui.preferences.JavaPreferencesPropertyTester" + type="java.lang.Object" + namespace="org.eclipse.jdt.ui" + properties="hasPreference" + class="org.eclipse.jdt.internal.ui.preferences.JavaPreferencesPropertyTester"> + </propertyTester> + </extension> + + <extension + point="org.eclipse.ui.workbench.texteditor.codeMiningProviders"> + <codeMiningProvider + class="org.eclipse.jdt.internal.ui.javaeditor.codemining.JavaElementCodeMiningProvider" + id="org.eclipse.jdt.ui.codemining.javaelement" + label="%JavaElementCodeMiningProvider.label"> + <enabledWhen> + <and> + <with variable="editorInput"> + <or> + <adapt type="org.eclipse.core.resources.IFile"> + <test property="org.eclipse.core.resources.contentTypeId" value="org.eclipse.jdt.core.javaSource" /> + </adapt> + <adapt type="org.eclipse.jdt.core.IClassFile"> + <instanceof value="org.eclipse.jdt.core.IClassFile" /> + </adapt> + </or> + </with> + <or> + <test property="org.eclipse.jdt.ui.hasPreference" value="java.codemining.references" forcePluginActivation="true"/> + <test property="org.eclipse.jdt.ui.hasPreference" value="java.codemining.implementations" forcePluginActivation="true"/> + </or> + </and> + </enabledWhen> + </codeMiningProvider> + </extension> </plugin> diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java index 5b1d9758c3..cbb6cca75f 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java @@ -58,8 +58,12 @@ public class JavaCodeMiningManager implements IPropertyChangeListener { */ private void enable() { if (fEditor != null) { - fReconciler= new JavaCodeMiningReconciler(); - fReconciler.install(fEditor, fSourceViewer); + if (fReconciler != null) { + fReconciler.updateCodeMinings(); + } else { + fReconciler= new JavaCodeMiningReconciler(); + fReconciler.install(fEditor, fSourceViewer); + } } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java index d1445d00d7..12f9c3cdd9 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java @@ -16,7 +16,6 @@ package org.eclipse.jdt.internal.ui.javaeditor; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.text.source.ISourceViewer; -import org.eclipse.jface.text.source.ISourceViewerExtension5; import org.eclipse.jdt.core.dom.CompilationUnit; @@ -46,7 +45,7 @@ public class JavaCodeMiningReconciler implements IJavaReconcilingListener { /** * Install this reconciler on the given editor. * - * @param editor the editor + * @param editor the editor * @param sourceViewer the source viewer */ public void install(JavaEditor editor, ISourceViewer sourceViewer) { @@ -73,9 +72,9 @@ public class JavaCodeMiningReconciler implements IJavaReconcilingListener { /** * Update Java code mining in the Java editor. */ - private void updateCodeMinings() { - if (fSourceViewer instanceof ISourceViewerExtension5) { - ((ISourceViewerExtension5) fSourceViewer).updateCodeMinings(); + void updateCodeMinings() { + if (fSourceViewer instanceof JavaSourceViewer) { + ((JavaSourceViewer) fSourceViewer).doUpdateCodeMinings(); } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java index 00ba9382c3..850e23a361 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java @@ -2948,6 +2948,8 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements } if (affectsJavaCodeMining(event)) { + // It's a code mining preference, recompute the list of code mining providers. + installCodeMiningProviders(); if (isJavaCodeMiningEnabled()) { installJavaCodeMining(); } else { @@ -4284,6 +4286,15 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements } } + @Override + protected void installCodeMiningProviders() { + if (JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CODEMINING_ENABLED)) { + super.installCodeMiningProviders(); + } else { + ((JavaSourceViewer) getSourceViewer()).resetCodeMinings(); + } + } + /** * Install Java code mining. * @@ -4314,6 +4325,9 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements * @since 3.14 */ boolean isJavaCodeMiningEnabled() { + if (!JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CODEMINING_ENABLED)) { + return false; + } // check if there is a registered Java code mining. ISourceViewer viewer= getViewer(); if (viewer instanceof ISourceViewerExtension5) { @@ -4330,12 +4344,7 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements * @since 3.14 */ protected boolean affectsJavaCodeMining(PropertyChangeEvent event) { - boolean isJavaCodeMiningPreference= isJavaCodeMiningPreference(event.getProperty()); - if (isJavaCodeMiningPreference) { - // It's a code mining preference, recompute the list of code mining providers. - installCodeMiningProviders(); - } - return isJavaCodeMiningPreference; + return isJavaCodeMiningPreference(event.getProperty()); } /** @@ -4351,7 +4360,7 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements * @since 3.14 */ private boolean isJavaCodeMiningPreference(String property) { - return property.startsWith(PreferenceConstants.EDITOR_JAVA_CODEMINING_PREFIX); + return PreferenceConstants.EDITOR_CODEMINING_ENABLED.equals(property) || property.startsWith(PreferenceConstants.EDITOR_JAVA_CODEMINING_PREFIX); } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaSourceViewer.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaSourceViewer.java index 39e9712145..f9df353bbd 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaSourceViewer.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaSourceViewer.java @@ -715,4 +715,31 @@ public class JavaSourceViewer extends ProjectionViewer implements IPropertyChang return null; } + @Override + public void updateCodeMinings() { + // Here we disable this method which is called by AbstractTextEditor#installCodeMiningProviders which updates code mining (draw minings in the UI) + // In JDT code mining case, the update code mining must be done when ICompilationUnit is reconciled (and not when editor is opened). + // The update code mining is done by JavaCodeMiningReconciler which calls doUpdateCodeMinings: + // - on install + // - when compilation unit is reconciled + } + + /** + * Update java code minings. + * + * @since 3.16 + */ + void doUpdateCodeMinings() { + super.updateCodeMinings(); + } + + /** + * Reset the registered code mining providers. + * + * @since 3.16 + */ + void resetCodeMinings() { + super.setCodeMiningProviders(null); + doUpdateCodeMinings(); + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/AbstractJavaElementLineHeaderCodeMining.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/AbstractJavaElementLineHeaderCodeMining.java new file mode 100644 index 0000000000..5b423eb882 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/AbstractJavaElementLineHeaderCodeMining.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2018 Angelo ZERR. + * 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 + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 + */ +package org.eclipse.jdt.internal.ui.javaeditor.codemining; + +import java.util.function.Consumer; + +import org.eclipse.swt.events.MouseEvent; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; +import org.eclipse.jface.text.codemining.LineHeaderCodeMining; + +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.ISourceRange; +import org.eclipse.jdt.core.ISourceReference; +import org.eclipse.jdt.core.JavaModelException; + +/** + * Abstract class for Java code mining. + * + * @since 3.16 + */ +public abstract class AbstractJavaElementLineHeaderCodeMining extends LineHeaderCodeMining { + + private final IJavaElement element; + + public AbstractJavaElementLineHeaderCodeMining(IJavaElement element, IDocument document, ICodeMiningProvider provider, + Consumer<MouseEvent> action) throws JavaModelException, BadLocationException { + super(getLineNumber(element, document), document, provider, action); + this.element= element; + } + + private static int getLineNumber(IJavaElement element, IDocument document) + throws JavaModelException, BadLocationException { + ISourceRange r= ((ISourceReference) element).getNameRange(); + int offset= r.getOffset(); + return document.getLineOfOffset(offset); + } + + /** + * Returns the java element. + * + * @return the java element. + */ + public IJavaElement getElement() { + return element; + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java new file mode 100644 index 0000000000..b2aa0f95d1 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaElementCodeMiningProvider.java @@ -0,0 +1,145 @@ +/** + * Copyright (c) 2018 Angelo ZERR. + * 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 + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 + */ +package org.eclipse.jdt.internal.ui.javaeditor.codemining; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import org.eclipse.core.runtime.IProgressMonitor; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider; +import org.eclipse.jface.text.codemining.ICodeMining; + +import org.eclipse.ui.texteditor.ITextEditor; + +import org.eclipse.jdt.core.Flags; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; + +import org.eclipse.jdt.ui.PreferenceConstants; + +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; +import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesPropertyTester; + +/** + * Java code mining provider to show code minings by using {@link IJavaElement}: + * + * <ul> + * <li>Show references</li> + * <li>Show implementations</li> + * </ul> + * + * @since 3.16 + */ +public class JavaElementCodeMiningProvider extends AbstractCodeMiningProvider { + + private final boolean showAtLeastOne; + + private final boolean showReferences; + + private final boolean showReferencesOnClasses; + + private final boolean showReferencesOnFields; + + private final boolean showReferencesOnMethods; + + private final boolean showImplementations; + + public JavaElementCodeMiningProvider() { + showAtLeastOne= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_CODEMINING_AT_LEAST_ONE); + showReferences= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES); + showReferencesOnClasses= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_CLASSES); + showReferencesOnFields= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_FIELDS); + showReferencesOnMethods= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_METHODS); + showImplementations= JavaPreferencesPropertyTester.isEnabled(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_IMPLEMENTATIONS); + } + + @Override + public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer, + IProgressMonitor monitor) { + return CompletableFuture.supplyAsync(() -> { + monitor.isCanceled(); + ITextEditor textEditor= super.getAdapter(ITextEditor.class); + ITypeRoot unit= EditorUtility.getEditorInputJavaElement(textEditor, true); + if (unit == null) { + return Collections.emptyList(); + } + try { + IJavaElement[] elements= unit.getChildren(); + List<ICodeMining> minings= new ArrayList<>(elements.length); + collectMinings(unit, textEditor, unit.getChildren(), minings, viewer, monitor); + monitor.isCanceled(); + return minings; + } catch (JavaModelException e) { + // Should never occur + } + return Collections.emptyList(); + }); + } + + /** + * Collect java code minings. + * + * @param unit the compilation unit + * @param textEditor the Java editor + * @param elements the java elements to track + * @param minings the current list of minings to update + * @param viewer the viewer + * @param monitor the monitor + * @throws JavaModelException thrown when java model error + */ + private void collectMinings(ITypeRoot unit, ITextEditor textEditor, IJavaElement[] elements, + List<ICodeMining> minings, ITextViewer viewer, IProgressMonitor monitor) throws JavaModelException { + for (IJavaElement element : elements) { + if (monitor.isCanceled()) { + return; + } + if (element.getElementType() == IJavaElement.TYPE) { + collectMinings(unit, textEditor, ((IType) element).getChildren(), minings, viewer, monitor); + } else if (!(element.getElementType() == IJavaElement.METHOD || element.getElementType() == IJavaElement.FIELD)) { + continue; + } + if (showReferences) { + try { + if ((showReferencesOnClasses && (element.getElementType() == IJavaElement.TYPE)) // Show references on classes + || (showReferencesOnMethods && (element.getElementType() == IJavaElement.METHOD)) // Show references on methods + || (showReferencesOnFields && (element.getElementType() == IJavaElement.FIELD)) // Show references on fields + ) { + minings.add(new JavaReferenceCodeMining(element, (JavaEditor) textEditor, viewer.getDocument(), + this, showAtLeastOne)); + } + } catch (BadLocationException e) { + // Should never occur + } + } + if (showImplementations) { + if (element instanceof IType) { + IType type= (IType) element; + if (type.isInterface() || Flags.isAbstract(type.getFlags())) { + try { + minings.add(new JavaImplementationCodeMining(type, viewer.getDocument(), this, + showAtLeastOne)); + } catch (BadLocationException e) { + // Should never occur + } + } + } + } + } + } +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java new file mode 100644 index 0000000000..c0f1dbc98d --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaImplementationCodeMining.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2018 Angelo ZERR. + * 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 + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 + */ +package org.eclipse.jdt.internal.ui.javaeditor.codemining; + +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +import org.eclipse.core.runtime.IProgressMonitor; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; + +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaModelException; + +/** + * Java implementation code mining. + * + * @since 3.16 + */ +public class JavaImplementationCodeMining extends AbstractJavaElementLineHeaderCodeMining { + + private final boolean showImplementationsAtLeastOne; + + public JavaImplementationCodeMining(IType element, IDocument document, ICodeMiningProvider provider, + boolean showImplementationsAtLeastOne) throws JavaModelException, BadLocationException { + super(element, document, provider, null); + this.showImplementationsAtLeastOne= showImplementationsAtLeastOne; + } + + @Override + protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) { + return CompletableFuture.runAsync(() -> { + try { + long implCount= countImplementations((IType) getElement(), monitor); + if (implCount == 0 && showImplementationsAtLeastOne) { + super.setLabel(""); //$NON-NLS-1$ + } else { + super.setLabel(implCount + " " + (implCount > 1 ? "implementations" : "implementation")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + } catch (JavaModelException e) { + // Should never occur + } + }); + } + + /** + * Return the count of implementation for the given java element type. + * + * @param type the java element type. + * @param monitor the monitor + * @return the count of implementation for the given java element type. + * @throws JavaModelException throws when Java error + */ + private static long countImplementations(IType type, IProgressMonitor monitor) throws JavaModelException { + IType[] results= type.newTypeHierarchy(monitor).getAllSubtypes(type); + return Stream.of(results).filter(t -> t.getAncestor(IJavaElement.COMPILATION_UNIT) != null).count(); + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaReferenceCodeMining.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaReferenceCodeMining.java new file mode 100644 index 0000000000..538f7b0527 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/codemining/JavaReferenceCodeMining.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2018 Angelo ZERR. + * 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 + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 + */ +package org.eclipse.jdt.internal.ui.javaeditor.codemining; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicLong; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; + +import org.eclipse.core.resources.ResourcesPlugin; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; + +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.search.IJavaSearchConstants; +import org.eclipse.jdt.core.search.IJavaSearchScope; +import org.eclipse.jdt.core.search.SearchEngine; +import org.eclipse.jdt.core.search.SearchMatch; +import org.eclipse.jdt.core.search.SearchParticipant; +import org.eclipse.jdt.core.search.SearchPattern; +import org.eclipse.jdt.core.search.SearchRequestor; + +import org.eclipse.jdt.ui.actions.FindReferencesAction; + +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; + +/** + * Java reference code mining. + * + * @since 3.16 + */ +public class JavaReferenceCodeMining extends AbstractJavaElementLineHeaderCodeMining { + + private final boolean showReferencesAtLeastOne; + + public JavaReferenceCodeMining(IJavaElement element, JavaEditor editor, IDocument document, + ICodeMiningProvider provider, boolean showReferencesAtLeastOne) + throws JavaModelException, BadLocationException { + super(element, document, provider, e -> new FindReferencesAction(editor).run(element)); + this.showReferencesAtLeastOne= showReferencesAtLeastOne; + } + + @Override + protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) { + return CompletableFuture.runAsync(() -> { + try { + monitor.isCanceled(); + long refCount= countReferences(getElement(), monitor); + monitor.isCanceled(); + if (refCount == 0 && showReferencesAtLeastOne) { + super.setLabel(""); //$NON-NLS-1$ + } else { + super.setLabel(refCount + " " + (refCount > 1 ? "references" : "reference")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + } catch (JavaModelException e) { + // Should never occur + } catch (CoreException e) { + // Should never occur + } + }); + } + + /** + * Return the number of references for the given java element. + * + * @param element the java element. + * @param monitor the monitor + * @return he number of references for the given java element. + * @throws JavaModelException throws when java error. + * @throws CoreException throws when java error. + */ + private static long countReferences(IJavaElement element, IProgressMonitor monitor) + throws JavaModelException, CoreException { + if (element == null) { + return 0; + } + final AtomicLong count= new AtomicLong(0); + SearchPattern pattern= SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES); + SearchEngine engine= new SearchEngine(); + engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, + createSearchScope(), new SearchRequestor() { + + @Override + public void acceptSearchMatch(SearchMatch match) throws CoreException { + Object o= match.getElement(); + if (o instanceof IJavaElement + && ((IJavaElement) o).getAncestor(IJavaElement.COMPILATION_UNIT) != null) { + count.incrementAndGet(); + } + } + }, monitor); + + return count.get(); + } + + /** + * Create Java workspace scope. + * + * @return the Java workspace scope. + * @throws JavaModelException when java error. + */ + private static IJavaSearchScope createSearchScope() throws JavaModelException { + IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); + return SearchEngine.createJavaSearchScope(projects, IJavaSearchScope.SOURCES); + } +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilterTextControl.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilterTextControl.java index f596ab9472..b0fc4c2074 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilterTextControl.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilterTextControl.java @@ -271,4 +271,26 @@ public class FilterTextControl { fClearButton.setVisible(visible); } } + + /** + * Enables the filter text control if the argument is <code>true</code>, and disables it + * otherwise. + * + * @param enabled the new enabled state + * + * @since 3.16 + */ + public void setEnabled(boolean enabled) { + if (fTextControl != null) { + fTextControl.setEnabled(enabled); + } + if (fClearButton != null) { + fClearButton.setEnabled(enabled); + } + boolean nativeField= fgUseNativeSearchField.booleanValue(); + if (!nativeField) { + // in the case of native field, composite is initialized with blank, we need to manage background to avoid having a blank square on the right when the control is disabled. + fComposite.setBackground(enabled ? fComposite.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND) : null); + } + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilteredPreferenceTree.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilteredPreferenceTree.java index 1d792e0a90..c8a1fc49cd 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilteredPreferenceTree.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/FilteredPreferenceTree.java @@ -266,7 +266,7 @@ public class FilteredPreferenceTree { * The scrolled area of the tree. */ protected ReflowControlScrolledPageContent fScrolledPageContent; - + /** * Job to update the UI in a separate thread. */ @@ -282,16 +282,31 @@ public class FilteredPreferenceTree { */ private Label fNoMatchFoundLabel; + /** + * The description which can be <code>null</code> + */ + private Label fDescription; + + /** + * The filter text control. + */ + private FilterTextControl fFilterTextControl; + private ToolItem fExpandAllItem; private ToolItem fCollapseAllItem; + public FilteredPreferenceTree(Composite parentComposite, String label, String hint) { + this(parentComposite, label, hint, true); + } + + public FilteredPreferenceTree(Composite parentComposite, String label, String hint, boolean showVerticalBar) { fParentComposite= parentComposite; fRoot= new PreferenceTreeNode<>(null, null, false); createDescription(label); createFilterBox(hint); - createScrolledArea(); + createScrolledArea(showVerticalBar); createNoMatchFoundLabel(); fRefreshJob= doCreateRefreshJob(); @@ -308,10 +323,10 @@ public class FilteredPreferenceTree { if (label == null) return; - Label description= new Label(fParentComposite, SWT.LEFT | SWT.WRAP); - description.setFont(fParentComposite.getFont()); - description.setText(label); - description.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); + fDescription= new Label(fParentComposite, SWT.LEFT | SWT.WRAP); + fDescription.setFont(fParentComposite.getFont()); + fDescription.setText(label); + fDescription.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); } private void createFilterBox(String hint) { @@ -325,9 +340,9 @@ public class FilteredPreferenceTree { composite.setFont(fParentComposite.getFont()); //TODO: Directly use the hint flags once Bug 293230 is fixed - FilterTextControl filterTextControl= new FilterTextControl(composite); + fFilterTextControl= new FilterTextControl(composite); - final Text filterBox= filterTextControl.getFilterControl(); + Text filterBox= fFilterTextControl.getFilterControl(); filterBox.setMessage(hint); filterBox.addModifyListener(new ModifyListener() { @@ -376,13 +391,13 @@ public class FilteredPreferenceTree { return item; } - private void createScrolledArea() { + private void createScrolledArea(boolean showVerticalBar) { fScrolledPageContent= new ReflowControlScrolledPageContent(fParentComposite); fScrolledPageContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, ((GridLayout) fParentComposite.getLayout()).numColumns, 0)); fScrolledPageContent.addControlListener(new ControlAdapter() { @Override public void controlResized(ControlEvent e) { - fScrolledPageContent.getVerticalBar().setVisible(true); + fScrolledPageContent.getVerticalBar().setVisible(showVerticalBar); } }); } @@ -529,4 +544,22 @@ public class FilteredPreferenceTree { fScrolledPageContent.setReflow(true); Display.getCurrent().asyncExec(() -> fScrolledPageContent.setRedraw(true)); } + + /** + * Enables the filtered preference tree if the argument is <code>true</code>, and disables it + * otherwise. + * + * @param enabled the new enabled state + * + * @since 3.16 + */ + public void setEnabled(boolean enabled) { + if (fDescription != null) { + fDescription.setEnabled(enabled); + } + fFilterTextControl.setEnabled(enabled); + fCollapseAllItem.setEnabled(enabled); + fExpandAllItem.setEnabled(enabled); + fRoot.getChildren().forEach(node -> node.setEnabled(enabled)); + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningConfigurationBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningConfigurationBlock.java new file mode 100644 index 0000000000..a2b947486e --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningConfigurationBlock.java @@ -0,0 +1,236 @@ +/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * 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
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127
+ */
+package org.eclipse.jdt.internal.ui.preferences;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Font;
+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.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.layout.PixelConverter;
+
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
+import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
+
+import org.eclipse.jdt.ui.PreferenceConstants;
+
+import org.eclipse.jdt.internal.ui.JavaPlugin;
+import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
+import org.eclipse.jdt.internal.ui.preferences.FilteredPreferenceTree.PreferenceTreeNode;
+import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
+import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
+
+/**
+ * Configures Java Editor code mining preferences.
+ *
+ * @since 3.16
+ */
+public class JavaEditorCodeMiningConfigurationBlock extends OptionsConfigurationBlock {
+
+ // Preference store keys
+
+ // --------------------- General
+
+ private static final Key PREF_CODEMINING_ENABLED= getJDTUIKey(
+ PreferenceConstants.EDITOR_CODEMINING_ENABLED);
+
+ private static final Key PREF_SHOW_CODEMINING_AT_LEAST_ONE= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_CODEMINING_AT_LEAST_ONE);
+
+ private static final Key PREF_SHOW_REFERENCES= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES);
+
+ private static final Key PREF_SHOW_REFERENCES_ON_CLASSES= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_CLASSES);
+
+ private static final Key PREF_SHOW_REFERENCES_ON_FIELDS= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_FIELDS);
+
+ private static final Key PREF_SHOW_REFERENCES_ON_METHODS= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_METHODS);
+
+ private static final Key PREF_SHOW_IMPLEMENTATIONS= getJDTUIKey(
+ PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_IMPLEMENTATIONS);
+
+ private static final String SETTINGS_SECTION_NAME= "JavaEditorCodeMiningConfigurationBlock"; //$NON-NLS-1$
+
+ private static final String[] TRUE_FALSE= new String[] { "true", "false" }; //$NON-NLS-1$ //$NON-NLS-2$
+
+ private PixelConverter fPixelConverter;
+
+ private Button atLeastOneCheckBox;
+
+ private PreferenceTree fFilteredPrefTree;
+
+ public JavaEditorCodeMiningConfigurationBlock(IStatusChangeListener context,
+ IWorkbenchPreferenceContainer container) {
+ super(context, null, getAllKeys(), container);
+ }
+
+ public static Key[] getAllKeys() {
+ return new Key[] { PREF_CODEMINING_ENABLED, PREF_SHOW_CODEMINING_AT_LEAST_ONE, PREF_SHOW_REFERENCES, PREF_SHOW_REFERENCES_ON_CLASSES, PREF_SHOW_REFERENCES_ON_FIELDS,
+ PREF_SHOW_REFERENCES_ON_METHODS,
+ PREF_SHOW_IMPLEMENTATIONS };
+ }
+
+ @Override
+ protected Control createContents(Composite parent) {
+ fPixelConverter= new PixelConverter(parent);
+ setShell(parent.getShell());
+
+ Composite mainComp= new Composite(parent, SWT.NONE);
+ mainComp.setFont(parent.getFont());
+ GridLayout layout= new GridLayout();
+ layout.marginHeight= 0;
+ layout.marginWidth= 0;
+ mainComp.setLayout(layout);
+
+ // Add enabled code mining checkbox
+ Button codeMiningEnabledCheckBox= addCheckBox(mainComp, PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_enableCodeMining_label, PREF_CODEMINING_ENABLED,
+ TRUE_FALSE, 0);
+
+ // - Only if there is at least one result
+ atLeastOneCheckBox= addCheckBox(mainComp,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showCodeMining_atLeastOne_label,
+ PREF_SHOW_CODEMINING_AT_LEAST_ONE, TRUE_FALSE, LayoutUtil.getIndent());
+
+ Composite commonComposite= createCodeMiningContent(mainComp);
+ GridData gridData= new GridData(GridData.FILL, GridData.FILL, true, true);
+ gridData.heightHint= fPixelConverter.convertHeightInCharsToPixels(20);
+ commonComposite.setLayoutData(gridData);
+
+ codeMiningEnabledCheckBox.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ atLeastOneCheckBox.setEnabled(codeMiningEnabledCheckBox.getSelection());
+ fFilteredPrefTree.setEnabled(codeMiningEnabledCheckBox.getSelection());
+ updateEnableStates();
+ }
+ });
+ atLeastOneCheckBox.setEnabled(codeMiningEnabledCheckBox.getSelection());
+ fFilteredPrefTree.setEnabled(codeMiningEnabledCheckBox.getSelection());
+ validateSettings(null, null, null);
+ return mainComp;
+ }
+
+ private Composite createCodeMiningContent(Composite parent) {
+ // Create filtered tree which contains code minings
+ fFilteredPrefTree= new PreferenceTree(this, parent,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_common_description, false);
+ final ScrolledPageContent sc1= fFilteredPrefTree.getScrolledPageContent();
+
+ int nColumns= 1;
+ Composite composite= sc1.getBody();
+ GridLayout layout= new GridLayout(nColumns, false);
+ layout.marginHeight= 0;
+ layout.marginWidth= 0;
+ composite.setLayout(layout);
+
+ // --- General
+ createGeneralSection(nColumns, composite);
+
+ IDialogSettings settingsSection= JavaPlugin.getDefault().getDialogSettings().getSection(SETTINGS_SECTION_NAME);
+ restoreSectionExpansionStates(settingsSection);
+
+ return sc1;
+ }
+
+ private void createGeneralSection(int nColumns, Composite parent) {
+ int defaultIndent= 0;
+ int extraIndent= LayoutUtil.getIndent();
+ String label= PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_section_general;
+ Key twistieKey= OptionsConfigurationBlock.getLocalKey("JavaEditorCodeMiningPreferencePage_section_general"); //$NON-NLS-1$
+ PreferenceTreeNode<?> section= fFilteredPrefTree.addExpandableComposite(parent, label, nColumns, twistieKey,
+ null, false);
+ ExpandableComposite excomposite= getExpandableComposite(twistieKey);
+
+ Composite inner= createInnerComposite(excomposite, nColumns, parent.getFont());
+
+ // - Show references
+ PreferenceTreeNode<Button> showReferences= fFilteredPrefTree.addCheckBox(inner,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showReferences_label, PREF_SHOW_REFERENCES,
+ TRUE_FALSE, defaultIndent, section);
+ // - Show references (On classes)
+ fFilteredPrefTree.addCheckBox(inner,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showReferences_onClasses_label,
+ PREF_SHOW_REFERENCES_ON_CLASSES, TRUE_FALSE, extraIndent, showReferences);
+ // - Show references (On fields)
+ fFilteredPrefTree.addCheckBox(inner,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showReferences_onFields_label,
+ PREF_SHOW_REFERENCES_ON_FIELDS, TRUE_FALSE, extraIndent, showReferences);
+ // - Show references (On methods)
+ fFilteredPrefTree.addCheckBox(inner,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showReferences_onMethods_label,
+ PREF_SHOW_REFERENCES_ON_METHODS, TRUE_FALSE, extraIndent, showReferences);
+
+ // - Show implementations
+ fFilteredPrefTree.addCheckBox(inner,
+ PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showImplementations_label,
+ PREF_SHOW_IMPLEMENTATIONS, TRUE_FALSE, defaultIndent, section);
+ }
+
+ private void updateEnableStates() {
+ boolean enabledCodeMining= getCheckBox(PREF_CODEMINING_ENABLED).getSelection();
+ if (enabledCodeMining) {
+ // Show references checkboxes
+ boolean showReferences= getCheckBox(PREF_SHOW_REFERENCES).getSelection();
+ getCheckBox(PREF_SHOW_REFERENCES_ON_CLASSES).setEnabled(showReferences);
+ getCheckBox(PREF_SHOW_REFERENCES_ON_FIELDS).setEnabled(showReferences);
+ getCheckBox(PREF_SHOW_REFERENCES_ON_METHODS).setEnabled(showReferences);
+ // Show implementations checkboxes
+ getCheckBox(PREF_SHOW_IMPLEMENTATIONS).getSelection();
+ } else {
+ atLeastOneCheckBox.setEnabled(false);
+ fFilteredPrefTree.setEnabled(false);
+ }
+ }
+
+ private Composite createInnerComposite(ExpandableComposite excomposite, int nColumns, Font font) {
+ Composite inner= new Composite(excomposite, SWT.NONE);
+ inner.setFont(font);
+ inner.setLayout(new GridLayout(nColumns, false));
+ excomposite.setClient(inner);
+ return inner;
+ }
+
+ @Override
+ protected void validateSettings(Key changedKey, String oldValue, String newValue) {
+ if (!areSettingsEnabled()) {
+ return;
+ }
+ if (changedKey != null) {
+ if (PREF_SHOW_REFERENCES.equals(changedKey) || PREF_SHOW_IMPLEMENTATIONS.equals(changedKey)) {
+ updateEnableStates();
+ }
+ } else {
+ updateEnableStates();
+ }
+ fContext.statusChanged(new StatusInfo());
+ }
+
+ @Override
+ protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
+ return null;
+ }
+
+ @Override
+ public void dispose() {
+ IDialogSettings section= JavaPlugin.getDefault().getDialogSettings().addNewSection(SETTINGS_SECTION_NAME);
+ storeSectionExpansionStates(section);
+ super.dispose();
+ }
+}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningPreferencePage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningPreferencePage.java new file mode 100644 index 0000000000..ecf67bdc50 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaEditorCodeMiningPreferencePage.java @@ -0,0 +1,96 @@ +/**
+ * Copyright (c) 2018 Angelo ZERR.
+ * 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
+ *
+ * Contributors:
+ * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127
+ */
+package org.eclipse.jdt.internal.ui.preferences;
+
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+
+import org.eclipse.core.resources.IProject;
+
+import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
+
+import org.eclipse.jdt.ui.PreferenceConstants;
+
+/**
+ * Code mining preference page.
+ * <p>
+ * Note: Must be public since it is referenced from plugin.xml
+ * </p>
+ *
+ * @since 3.16
+ */
+public class JavaEditorCodeMiningPreferencePage extends PropertyAndPreferencePage {
+
+ private JavaEditorCodeMiningConfigurationBlock fConfigurationBlock;
+
+ public JavaEditorCodeMiningPreferencePage() {
+ setPreferenceStore(PreferenceConstants.getPreferenceStore());
+ }
+
+ @Override
+ public void createControl(Composite parent) {
+ IWorkbenchPreferenceContainer container= (IWorkbenchPreferenceContainer) getContainer();
+ fConfigurationBlock= new JavaEditorCodeMiningConfigurationBlock(getNewStatusChangedListener(), container);
+
+ super.createControl(parent);
+ }
+
+ @Override
+ protected Control createPreferenceContent(Composite composite) {
+ return fConfigurationBlock.createContents(composite);
+ }
+
+ @Override
+ protected boolean hasProjectSpecificOptions(IProject project) {
+ return false;
+ }
+
+ @Override
+ protected String getPreferencePageID() {
+ return "org.eclipse.jdt.ui.preferences.JavaEditorCodeMiningPreferencePage"; //$NON-NLS-1$
+ }
+
+ @Override
+ protected String getPropertyPageID() {
+ return null;
+ }
+
+ @Override
+ public void dispose() {
+ if (fConfigurationBlock != null) {
+ fConfigurationBlock.dispose();
+ }
+ super.dispose();
+ }
+
+ @Override
+ protected void performDefaults() {
+ super.performDefaults();
+ if (fConfigurationBlock != null) {
+ fConfigurationBlock.performDefaults();
+ }
+ }
+
+ @Override
+ public boolean performOk() {
+ if (fConfigurationBlock != null && !fConfigurationBlock.performOk()) {
+ return false;
+ }
+ return super.performOk();
+ }
+
+ @Override
+ public void performApply() {
+ if (fConfigurationBlock != null) {
+ fConfigurationBlock.performApply();
+ }
+ }
+}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaPreferencesPropertyTester.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaPreferencesPropertyTester.java new file mode 100644 index 0000000000..b7222ef81c --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/JavaPreferencesPropertyTester.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2018 Angelo ZERR. + * 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 + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 + */ +package org.eclipse.jdt.internal.ui.preferences; + +import org.eclipse.core.expressions.PropertyTester; + +import org.eclipse.jdt.ui.PreferenceConstants; + +/** + * Property tester which checks that a given preference is true from the Java preference store. + * + * @since 3.16 + */ +public class JavaPreferencesPropertyTester extends PropertyTester { + + @Override + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { + String preferenceName= expectedValue.toString(); + return isEnabled(preferenceName); + } + + /** + * Returns <code>true</code> if the given preference name is set to true from the Java UI + * preferences and <code>false</code> otherwise. + * + * @param preferenceName the preference name check. + * @return <code>true</code> if the given preference name is set to true from the Java UI + * preferences and <code>false</code> otherwise. + */ + public static boolean isEnabled(String preferenceName) { + return PreferenceConstants.getPreferenceStore() + .getBoolean(preferenceName); + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/OptionsConfigurationBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/OptionsConfigurationBlock.java index 059b3f23cd..2593d0edce 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/OptionsConfigurationBlock.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/OptionsConfigurationBlock.java @@ -231,14 +231,27 @@ public abstract class OptionsConfigurationBlock { private final OptionsConfigurationBlock fConfigBlock; /** - * Constructs a new instance of PreferenceTree according to the parameters. + * Constructs a new instance of PreferenceTree with vertical bar according to the parameters. * * @param configBlock the Options Configuration block * @param parentComposite the parent composite * @param label the label, or <code>null</code> if none */ public PreferenceTree(OptionsConfigurationBlock configBlock, Composite parentComposite, String label) { - super(parentComposite, label, PreferencesMessages.OptionsConfigurationBlock_TypeFilterText); + this(configBlock, parentComposite, label, true); + } + + /** + * Constructs a new instance of PreferenceTree according to the parameters. + * + * @param configBlock the Options Configuration block + * @param parentComposite the parent composite + * @param label the label, or <code>null</code> if none + * @param showVerticalBar <code>true</code> if vertical bar must be displayed and + * <code>false</code> otherwise. + */ + public PreferenceTree(OptionsConfigurationBlock configBlock, Composite parentComposite, String label, boolean showVerticalBar) { + super(parentComposite, label, PreferencesMessages.OptionsConfigurationBlock_TypeFilterText, showVerticalBar); fConfigBlock= configBlock; } 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 e3216a0495..3cbd00e6a4 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 @@ -20,6 +20,7 @@ * Warning option for bug 410218 - https://bugs.eclipse.org/461999 * Gábor Kövesdán - Contribution for Bug 350000 - [content assist] Include non-prefix matches in auto-complete suggestions * Jesper S Møller - Contributions for bug 529432 - Allow JDT UI to target Java 10 + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 *******************************************************************************/ package org.eclipse.jdt.internal.ui.preferences; @@ -880,4 +881,14 @@ public final class PreferencesMessages extends NLS { public static String FilteredPreferencesTree_expandAll_tooltip; public static String FilterTextControl_Clear; public static String FilterTextControl_ClearFilterField; + + public static String JavaEditorCodeMiningConfigurationBlock_common_description; + public static String JavaEditorCodeMiningConfigurationBlock_enableCodeMining_label; + public static String JavaEditorCodeMiningConfigurationBlock_showCodeMining_atLeastOne_label; + public static String JavaEditorCodeMiningConfigurationBlock_section_general; + public static String JavaEditorCodeMiningConfigurationBlock_showReferences_label; + public static String JavaEditorCodeMiningConfigurationBlock_showReferences_onClasses_label; + public static String JavaEditorCodeMiningConfigurationBlock_showReferences_onFields_label; + public static String JavaEditorCodeMiningConfigurationBlock_showReferences_onMethods_label; + public static String JavaEditorCodeMiningConfigurationBlock_showImplementations_label; } 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 505a05c428..37e3331eff 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 @@ -1006,3 +1006,14 @@ FilterTextControl_ClearFilterField=Clear filter field JavaCompilerPropertyPage_ignore_optional_problems_label=&Ignore optional compile problems JavaCompilerPropertyPage_invalid_element_selection=Java Compiler settings cannot be changed for the selected element. + +# Code minings +JavaEditorCodeMiningConfigurationBlock_enableCodeMining_label=&Enable code minings +JavaEditorCodeMiningConfigurationBlock_showCodeMining_atLeastOne_label=Only if there is at &least one result +JavaEditorCodeMiningConfigurationBlock_common_description=&Select the code minings that you wish to enable/disable: +JavaEditorCodeMiningConfigurationBlock_section_general=General +JavaEditorCodeMiningConfigurationBlock_showReferences_label=Show &references +JavaEditorCodeMiningConfigurationBlock_showReferences_onClasses_label=On &classes +JavaEditorCodeMiningConfigurationBlock_showReferences_onFields_label=On &fields +JavaEditorCodeMiningConfigurationBlock_showReferences_onMethods_label=On &methods +JavaEditorCodeMiningConfigurationBlock_showImplementations_label=Show &implementations diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java index 4585c758b2..d0bf105b63 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java @@ -12,6 +12,7 @@ * IBM Corporation - initial API and implementation * Guven Demir <guven.internet+eclipse@gmail.com> - [package explorer] Alternative package name shortening: abbreviation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=299514 * Red Hat Inc - modify to use CodeGenerationSettingsConstants + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide Java References/Implementation CodeMinings - Bug 529127 *******************************************************************************/ package org.eclipse.jdt.ui; @@ -3794,6 +3795,77 @@ public class PreferenceConstants { public final static String DECORATE_TEST_CODE_CONTAINER_ICONS= "decorateTestCodeContainerIcons"; //$NON-NLS-1$ /** + * A named preference that controls whether codemining is enabled in the Java editor. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_CODEMINING_ENABLED= "editor_codemining_enabled"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Only if there is at least one result". + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_CODEMINING_AT_LEAST_ONE = "java.codemining.atLeastOne"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Show references" codemining. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_REFERENCES = "java.codemining.references"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Show references" on classes. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_CLASSES= "java.codemining.references.onClasses"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Show references" on fields. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_FIELDS= "java.codemining.references.onFields"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Show references" on methods. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_METHODS= "java.codemining.references.onMethods"; //$NON-NLS-1$ + + /** + * A named preference that stores the value for "Show implementations" + * codemining. + * <p> + * Value is of type <code>Boolean</code>. + * </p> + * + * @since 3.16 + */ + public static final String EDITOR_JAVA_CODEMINING_SHOW_IMPLEMENTATIONS = "java.codemining.implementations"; //$NON-NLS-1$ + + /** * Initializes the given preference store with the default values. * * @param store the preference store to be initialized @@ -4129,6 +4201,16 @@ public class PreferenceConstants { store.setValue(PREF_DEFAULT_EXPAND_WITH_CONSTRUCTORS_MEMBERS, ExpandWithConstructorsConfigurationBlock.serializeMembers(Arrays.asList(oldPrefStr))); store.setToDefault(CallHierarchyContentProvider.OLD_PREF_DEFAULT_EXPAND_WITH_CONSTRUCTORS); } + + // Code minings preferences + store.setDefault(PreferenceConstants.EDITOR_CODEMINING_ENABLED, false); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_CODEMINING_AT_LEAST_ONE, + true); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_REFERENCES, false); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_CLASSES, true); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_FIELDS, false); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_METHODS, true); + store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_IMPLEMENTATIONS, false); } /** |