Skip to content

Commit

Permalink
Fix code mining parameter name logic (#1441)
Browse files Browse the repository at this point in the history
- add two new preferences regarding the default filter and the
  filtering of implied parameter names
- for filtering of implied parameter names, do not filter out
  parameter names that are 3 characters or less that are contained
  in the argument but filter them out if they are equal
- bump up jdt.ui minor version as new preference constants are added
- fixes #1437
  • Loading branch information
jjohnstn committed Jun 7, 2024
1 parent 5bf7d72 commit f035a98
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.ui
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.jdt.ui; singleton:=true
Bundle-Version: 3.32.200.qualifier
Bundle-Version: 3.33.0.qualifier
Bundle-Activator: org.eclipse.jdt.internal.ui.JavaPlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %providerName
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</parent>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.ui</artifactId>
<version>3.32.200-SNAPSHOT</version>
<version>3.33.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2023 Angelo Zerr and others.
* Copyright (c) 2017, 2024 Angelo Zerr and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -16,6 +16,11 @@

import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.jface.text.codemining.ICodeMining;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
Expand All @@ -25,10 +30,12 @@
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;

import org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor;

import org.eclipse.jdt.ui.PreferenceConstants;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jface.text.codemining.ICodeMining;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;

public class CalleeJavaMethodParameterVisitor extends HierarchicalASTVisitor {

Expand Down Expand Up @@ -147,16 +154,26 @@ private boolean skipParameterNameCodeMining(String[] parameterNames, List<?> arg
if (parameterNames.length < parameterIndex) {
return true;
}
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
boolean filter= store.getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_FILTER_IMPLIED_PARAMETER_NAMES);
if (!filter) {
return false;
}
String parameterName= parameterNames[parameterIndex].toLowerCase();
String expression= arguments.get(parameterIndex).toString().toLowerCase();
return expression.contains(parameterName);
return parameterName.length() > 3 && expression.contains(parameterName) || parameterName.equals(expression);
}

private boolean skipParameterNamesCodeMinings(IMethod method) {
return method.getNumberOfParameters() <= 1;
}

private boolean skipParameterNamesCodeMinings(IMethod method, String[] parameterNames) {
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
boolean filter= store.getBoolean(PreferenceConstants.EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES);
if (!filter) {
return false;
}
// add default filtering to skip parameter names (based on original plug-in defaults)
String typeName= method.getDeclaringType().getTypeQualifiedName();
if (typeName.equals("Math")) { //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public class JavaEditorCodeMiningConfigurationBlock extends OptionsConfiguration
private static final Key PREF_SHOW_PARAMETER_NAMES= getJDTUIKey(
PreferenceConstants.EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAMES);

private static final Key PREF_FILTER_IMPLIED_PARAMETER_NAMES= getJDTUIKey(
PreferenceConstants.EDITOR_JAVA_CODEMINING_FILTER_IMPLIED_PARAMETER_NAMES);

private static final Key PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES= getJDTUIKey(
PreferenceConstants.EDITOR_JAVA_CODEMINING_DEFAULT_FILTER_FOR_PARAMETER_NAMES);

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 @@ -95,7 +101,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_SHOW_IMPLEMENTATIONS, PREF_SHOW_PARAMETER_NAMES, PREF_IGNORE_INEXACT_MATCHES, PREF_FILTER_IMPLIED_PARAMETER_NAMES, PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES };
}

@Override
Expand Down Expand Up @@ -183,6 +189,7 @@ private void createGeneralSection(int nColumns, Composite parent) {

Composite inner= createInnerComposite(excomposite, nColumns, parent.getFont());


// - Show references
PreferenceTreeNode<Button> showReferences= fFilteredPrefTree.addCheckBox(inner,
PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showReferences_label, PREF_SHOW_REFERENCES,
Expand Down Expand Up @@ -210,6 +217,16 @@ private void createGeneralSection(int nColumns, Composite parent) {
PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_showParameterNames_label,
PREF_SHOW_PARAMETER_NAMES, TRUE_FALSE, defaultIndent, section);

// - Filter implied parameter names
fFilteredPrefTree.addCheckBox(inner,
PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_filterImpliedParameterNames_label,
PREF_FILTER_IMPLIED_PARAMETER_NAMES, TRUE_FALSE, extraIndent, section);

// - Filter known method parameter names
fFilteredPrefTree.addCheckBox(inner,
PreferencesMessages.JavaEditorCodeMiningConfigurationBlock_defaultFilterForParameterNames_label,
PREF_DEFAULT_FILTER_FOR_PARAMETER_NAMES, TRUE_FALSE, extraIndent, section);

}

private void updateEnableStates() {
Expand All @@ -226,7 +243,9 @@ private void updateEnableStates() {
getCheckBox(PREF_SHOW_REFERENCES_ON_METHODS).setEnabled(showReferences);
// Show implementations checkboxes
getCheckBox(PREF_SHOW_IMPLEMENTATIONS).getSelection();
getCheckBox(PREF_SHOW_PARAMETER_NAMES).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);
} else {
atLeastOneCheckBox.setEnabled(false);
ignoreInexactReferenceMatches.setEnabled(false);
Expand All @@ -248,7 +267,8 @@ protected void validateSettings(Key changedKey, String oldValue, String newValue
return;
}
if (changedKey != null) {
if (PREF_CODEMINING_ENABLED.equals(changedKey) || PREF_SHOW_REFERENCES.equals(changedKey) || PREF_SHOW_IMPLEMENTATIONS.equals(changedKey)) {
if (PREF_CODEMINING_ENABLED.equals(changedKey) || PREF_SHOW_REFERENCES.equals(changedKey) || PREF_SHOW_IMPLEMENTATIONS.equals(changedKey)
|| PREF_SHOW_PARAMETER_NAMES.equals(changedKey)) {
updateEnableStates();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ private PreferencesMessages() {
public static String JavaEditorCodeMiningConfigurationBlock_showReferences_onMethods_label;
public static String JavaEditorCodeMiningConfigurationBlock_showImplementations_label;
public static String JavaEditorCodeMiningConfigurationBlock_showParameterNames_label;
public static String JavaEditorCodeMiningConfigurationBlock_filterImpliedParameterNames_label;
public static String JavaEditorCodeMiningConfigurationBlock_defaultFilterForParameterNames_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 @@ -1060,6 +1060,8 @@ JavaEditorCodeMiningConfigurationBlock_showReferences_onFields_label=&Fields
JavaEditorCodeMiningConfigurationBlock_showReferences_onMethods_label=&Methods
JavaEditorCodeMiningConfigurationBlock_showImplementations_label=Show &implementations
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

#Launching preferences
JavaLaunchingConfigurationBlock_application_name_fully_qualified_label=Use fully qualified name for new Java Application
Expand Down
27 changes: 27 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 @@ -3926,6 +3926,31 @@ private PreferenceConstants() {
*/
public static final String EDITOR_JAVA_CODEMINING_SHOW_PARAMETER_NAMES = "java.codemining.parameterNames"; //$NON-NLS-1$

/**
* A named preference that stores the value for "Default Filter for code mining parameter names" when showing parameter names
* in codemining. This will filter out parameter names for methods in java.lang.Math, org.slf4j.Logging,
* Immutable*.of() methods, Arrays.asList(), and various standard methods such as set() methods, setProperties()
* methods, compare() methods, parameter names that start with "begin", "end", "first", "last", to name a few.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*
* @since 3.33
*/
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 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.
* For example, if the parameter name is "format" and the parameter passed is "stringFormat".
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*
* @since 3.33
*/
public static final String EDITOR_JAVA_CODEMINING_FILTER_IMPLIED_PARAMETER_NAMES = "java.codemining.filter.implied.parameterNames"; //$NON-NLS-1$

/**
* A named preference that stores the maximum number of chain completions
* to be proposed at one time.
Expand Down Expand Up @@ -4347,6 +4372,8 @@ public static void initializeDefaultValues(IPreferenceStore store) {
store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_REFERENCES_ON_METHODS, false);
store.setDefault(EDITOR_JAVA_CODEMINING_SHOW_IMPLEMENTATIONS, false);
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);

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

0 comments on commit f035a98

Please sign in to comment.