Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide name for parameters in 1-arg methods when using code minings #1813

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.LongSupplier;
Expand Down Expand Up @@ -46,6 +47,7 @@
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.intro.IIntroManager;
import org.eclipse.ui.intro.IIntroPart;
Expand All @@ -54,6 +56,7 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;

import org.eclipse.jdt.ui.PreferenceConstants;

Expand Down Expand Up @@ -381,7 +384,17 @@ public void testBug549126() throws Exception {
}

@Test
public void testRecordConstructorOneParameter() throws Exception {
public void testRecordConstructorOneParameterPreferenceFalse() throws Exception {
assertParameterNamesShown(false, 0);
}

@Test
public void testRecordConstructorOneParameterPreferenceTrue() throws Exception {
assertParameterNamesShown(true, 1);
}

private void assertParameterNamesShown(boolean preferenceValue, int expectedShownNames) throws JavaModelException, PartInitException, InterruptedException, ExecutionException {
PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG, preferenceValue);
String contents= """
public class Ant {
record Ca (int size){
Expand All @@ -398,7 +411,7 @@ record Ca (int size){
JavaSourceViewer viewer= (JavaSourceViewer)editor.getViewer();
waitReconciled(viewer);

assertEquals(0, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size());
assertEquals(expectedShownNames, fParameterNameCodeMiningProvider.provideCodeMinings(viewer, new NullProgressMonitor()).get().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,15 @@ private boolean skipParameterNameCodeMining(String[] parameterNames, List<?> arg
}

private boolean skipParameterNamesCodeMinings(IMethod method) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this method altogether.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated: agreed on adding a parameter which makes this method necessary.

return method.getNumberOfParameters() <= 1;
boolean showNamesOfSingleArguments= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG);
return !showNamesOfSingleArguments && method.getNumberOfParameters() == 1
|| method.getNumberOfParameters() == 0;
}

private boolean skipParameterNamesCodeMinings(IMethodBinding methodBinding) {
return methodBinding.getParameterNames().length <= 1;
boolean showNamesOfSingleArguments= PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG);
return !showNamesOfSingleArguments && methodBinding.getParameterNames().length == 1
|| methodBinding.getParameterNames().length == 0;
}

private boolean skipParameterNamesCodeMinings(IMethod method, String[] parameterNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public class JavaEditorCodeMiningConfigurationBlock extends OptionsConfiguration
private static final Key PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES= getJDTUIKey(
PreferenceConstants.EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES);

private static final Key PREF_SHOW_ONE_PARAMETER= getJDTUIKey(
PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG);

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$
Expand All @@ -101,7 +104,7 @@ public JavaEditorCodeMiningConfigurationBlock(IStatusChangeListener context,
public static Key[] getAllKeys() {
return new Key[] { PREF_CODEMINING_ENABLED, PREF_SHOW_CODEMINING_AT_LEAST_ONE, PREF_SHOW_REFERENCES, PREF_SHOW_REFERENCES_ON_TYPES, PREF_SHOW_REFERENCES_ON_FIELDS,
PREF_SHOW_REFERENCES_ON_METHODS,
PREF_SHOW_IMPLEMENTATIONS, PREF_SHOW_PARAMETER_NAMES, PREF_IGNORE_INEXACT_MATCHES, PREF_FILTER_IMPLIED_PARAMETER_NAMES, PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES };
PREF_SHOW_IMPLEMENTATIONS, PREF_SHOW_PARAMETER_NAMES, PREF_IGNORE_INEXACT_MATCHES, PREF_FILTER_IMPLIED_PARAMETER_NAMES, PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES, PREF_SHOW_ONE_PARAMETER };
}

@Override
Expand Down Expand Up @@ -227,6 +230,9 @@ private void createGeneralSection(int nColumns, Composite parent) {
PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_defaultFilterForParameterNames_label,
PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES, TRUE_FALSE, extraIndent, section);

fFilteredPrefTree.addCheckBox(inner,
PreferencesMessages.JavaEditorCodeMiningShowOneParameter_label,
PREF_SHOW_ONE_PARAMETER, TRUE_FALSE, extraIndent, section);
}

private void updateEnableStates() {
Expand All @@ -244,8 +250,11 @@ private void updateEnableStates() {
// Show implementations checkboxes
getCheckBox(PREF_SHOW_IMPLEMENTATIONS).getSelection();
boolean showParameterNames= getCheckBox(PREF_SHOW_PARAMETER_NAMES).getSelection();

getCheckBox(PREF_FILTER_IMPLIED_PARAMETER_NAMES).setEnabled(showParameterNames);
getCheckBox(PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES).setEnabled(showParameterNames);
getCheckBox(PREF_SHOW_ONE_PARAMETER).setEnabled(showParameterNames);

} else {
atLeastOneCheckBox.setEnabled(false);
ignoreInexactReferenceMatches.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ private PreferencesMessages() {
public static String JavaEditorCodeMiningConfigurationBlock_showParameterNames_label;
public static String JavaEditorCodeMiningConfigurationBlock_filterImpliedParameterNames_label;
public static String JavaEditorCodeMiningConfigurationBlock_defaultFilterForParameterNames_label;
public static String JavaEditorCodeMiningShowOneParameter_label;

public static String JavaLaunchingConfigurationBlock_application_name_fully_qualified_label;
public static String JavaLaunchingConfigurationBlock_applet_name_fully_qualified_label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ JavaEditorCodeMiningConfigurationBlock_showImplementations_label=Show &implement
JavaEditorCodeMiningConfigurationBlock_showParameterNames_label=Show method &parameter names
JavaEditorCodeMiningConfigurationBlock_defaultFilterForParameterNames_label=&Default filter for some specified methods and method parameter names (e.g. compare())
JavaEditorCodeMiningConfigurationBlock_filterImpliedParameterNames_label=Filter parameter &names that are implied by parameter
JavaEditorCodeMiningShowOneParameter_label=Show code minings when method has one parameter

#Launching preferences
JavaLaunchingConfigurationBlock_application_name_fully_qualified_label=Use fully qualified name for new Java Application
Expand Down
12 changes: 12 additions & 0 deletions org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3939,6 +3939,17 @@ private PreferenceConstants() {
*/
public static final String EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES = "java.codemining.defalt.filter.for.parameterNames"; //$NON-NLS-1$

/**
* A named preference that stores the value if a method with one parameter should be shown with code minings or if the code minings are
* only shown for 2+ parameters
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*
* @since 3.34
*/
public static final String EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG = "java.codemining.show.one.parameter"; //$NON-NLS-1$

/**
* A named preference that stores the value for "Filter matching parameter names" when showing parameter names
* in codemining. This will filter out parameter names when the passed parameter name implies the parameter name.
Expand Down Expand Up @@ -4374,6 +4385,7 @@ public static void initializeDefaultValues(IPreferenceStore store) {
store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAMES, false);
store.setDefault(EDITOR_JAVA_CODEMINING_FILTER_IMPLIED_PARAMETER_NAMES, true);
store.setDefault(EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES, true);
store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAME_SINGLE_ARG, true);

// Javadoc hover & view
JavaElementLinks.initDefaultPreferences(store);
Expand Down
Loading