Skip to content

Commit 8676491

Browse files
Add toggle for "Jump to test method" code mining
Introduced a new preference to enable or disable the "Jump to test method" code mining link in the Java editor. This allows users to hide the link if it interferes with their typing or cursor position. - Added ENABLE_JUMP_TO_METHOD_CODE_MINING to PreferenceConstants. - Updated Preferences to handle the new setting. - Added a checkbox to the MoreUnit preference and property pages. - Modified MoreUnitCodeMiningProvider to respect the setting. - Added tooltips and UI indentation for better user experience. Closes #230 Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent 139c2d8 commit 8676491

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

org.moreunit.plugin/src/org/moreunit/codemining/MoreUnitCodeMiningProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ else if((element.getElementType() != IJavaElement.METHOD))
9292
addMining = true;
9393
}
9494
}
95-
else if(element instanceof IMethod)
95+
else if(element instanceof IMethod && preferences.shouldEnableJumpToMethodCodeMining(unit.getJavaProject()))
9696
{
9797
addMining = true;
9898
}

org.moreunit.plugin/src/org/moreunit/preferences/PreferenceConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public interface PreferenceConstants
2626
String TEST_CLASS_NAME_TEMPLATE = "org.moreunit.testClassNameTemplate";
2727
String GENERATE_COMMENTS_FOR_TEST_METHOD = "org.moreunit.generateCommentsForTestMethod";
2828
String ENABLE_MOREUNIT_CODE_MINING = "org.moreunit.enableMoreUnitCodeMining";
29+
String ENABLE_JUMP_TO_METHOD_CODE_MINING = "org.moreunit.enableJumpToMethodCodeMining";
2930

3031
String TEST_TYPE = "org.moreunit.test_type";
3132
String TEST_TYPE_VALUE_JUNIT_3 = "junit3";
@@ -47,6 +48,7 @@ public interface PreferenceConstants
4748
String DEFAULT_TEST_METHOD_DEFAULT_CONTENT = "throw new RuntimeException(\"not yet implemented\");";
4849
String DEFAULT_TEST_ANNOTATION_MODE = "OFF";
4950
boolean DEFAULT_ENABLE_MOREUNIT_CODEMINING = true;
51+
boolean DEFAULT_ENABLE_JUMP_TO_METHOD_CODE_MINING = true;
5052

5153
String TEXT_GENERAL_SETTINGS = "General settings for your unit tests (they can then be refined for each project):";
5254
String TEXT_TEST_SUPERCLASS = "Test superclass:";
@@ -65,6 +67,7 @@ public interface PreferenceConstants
6567
String TEXT_ENABLE_TEST_METHOD_SEARCH_BY_NAME = "Enable test method search \"by name\"";
6668
String TEXT_GENERATE_COMMENTS_FOR_TEST_METHOD = "Generate comments for test methods";
6769
String TEXT_ENABLE_MOREUNIT_CODEMINING = "Enable MoreUnit Code Mining";
70+
String TEXT_ENABLE_JUMP_TO_METHOD_CODE_MINING = "Enable \"Jump to method\" Code Mining (on the right side of the editor)";
6871
String TEXT_ANNOTATION_MODE = "Annotate tested methods";
6972
String TEST_ANNOTATION_MODE_DISABLED = "Disabled";
7073
String TEST_ANNOTATION_MODE_BY_NAME = "Search by method name";
@@ -78,6 +81,8 @@ public interface PreferenceConstants
7881
String TOOLTIP_EXTENDED_TEST_METHOD_SEARCH = "Enable this option if you want MoreUnit to propose jumping from a given method to any test method that calls it (and vice-versa).";
7982
String TOOLTIP_ENABLE_TEST_METHOD_SEARCH_BY_NAME = "Check this option if you want MoreUnit to blindly propose jumping from a given method to a test method with the same name, even though the test method does not call the method under test.";
8083
String TOOLTIP_TEST_ANNOTATION_EXTENDED_SEARCH = "This option will cause conflicts if other parts of Eclipse are searching for method method calls at the same time, which occurs often. Only activate if you know what you are doing.";
84+
String TOOLTIP_ENABLE_MOREUNIT_CODEMINING = "Code Mining adds information and links directly in the editor.";
85+
String TOOLTIP_ENABLE_JUMP_TO_METHOD_CODE_MINING = "Shows a \"Jump to test/tested method\" link at the end of method declarations.";
8186

8287
String TEST_METHOD_TYPE = "org.moreunit.test_methodType";
8388
String TEST_METHOD_TYPE_JUNIT3 = "testMethodTypeJunit3";

org.moreunit.plugin/src/org/moreunit/preferences/Preferences.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ protected static final IPreferenceStore initStore(IPreferenceStore store)
8080
store.setDefault(PreferenceConstants.TEST_METHOD_DEFAULT_CONTENT, PreferenceConstants.DEFAULT_TEST_METHOD_DEFAULT_CONTENT);
8181
store.setDefault(PreferenceConstants.TEST_ANNOTATION_MODE, PreferenceConstants.DEFAULT_TEST_ANNOTATION_MODE);
8282
store.setDefault(PreferenceConstants.ENABLE_MOREUNIT_CODE_MINING, PreferenceConstants.DEFAULT_ENABLE_MOREUNIT_CODEMINING);
83+
store.setDefault(PreferenceConstants.ENABLE_JUMP_TO_METHOD_CODE_MINING, PreferenceConstants.DEFAULT_ENABLE_JUMP_TO_METHOD_CODE_MINING);
8384
return store;
8485
}
8586

@@ -493,6 +494,20 @@ public void setEnableMoreUnitCodeMining(IJavaProject javaProject, boolean enable
493494
getProjectStore(javaProject).setValue(PreferenceConstants.ENABLE_MOREUNIT_CODE_MINING, enableMoreUnitCodeMining);
494495
}
495496

497+
public boolean shouldEnableJumpToMethodCodeMining(IJavaProject javaProject)
498+
{
499+
if(storeToRead(javaProject).contains(PreferenceConstants.ENABLE_JUMP_TO_METHOD_CODE_MINING))
500+
{
501+
return storeToRead(javaProject).getBoolean(PreferenceConstants.ENABLE_JUMP_TO_METHOD_CODE_MINING);
502+
}
503+
return storeToRead(javaProject).getDefaultBoolean(PreferenceConstants.ENABLE_JUMP_TO_METHOD_CODE_MINING);
504+
}
505+
506+
public void setEnableJumpToMethodCodeMining(IJavaProject javaProject, boolean enableJumpToMethodCodeMining)
507+
{
508+
getProjectStore(javaProject).setValue(PreferenceConstants.ENABLE_JUMP_TO_METHOD_CODE_MINING, enableJumpToMethodCodeMining);
509+
}
510+
496511
public MethodSearchMode getMethodSearchMode(IJavaProject javaProject)
497512
{
498513
boolean searchByCall = getBooleanValue(PreferenceConstants.EXTENDED_TEST_METHOD_SEARCH, javaProject);

org.moreunit.plugin/src/org/moreunit/properties/OtherMoreunitPropertiesBlock.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class OtherMoreunitPropertiesBlock implements SelectionListener
4343
private Text superClassTextField;
4444
private Button addCommentsToTestMethodCheckbox;
4545
private Button enableMoreUnitCodeMining;
46+
private Button enableJumpToMethodCodeMiningCheckbox;
4647
private Button extendedSearchCheckbox;
4748
private Button enableSearchByNameCheckbox;
4849

@@ -110,6 +111,7 @@ private void createCompositeWith2ColsParent(final Composite parentWith2Cols)
110111
createTestAnnotationModeRadioButtons(parentWith2Cols);
111112
createAddCommentsToTestMethodsCheckbox(parentWith2Cols);
112113
createEnableMoreUnitCodeMiningCheckbox(parentWith2Cols);
114+
createEnableJumpToMethodCodeMiningCheckbox(parentWith2Cols);
113115

114116
checkStateOfMethodPrefixButton();
115117
}
@@ -329,8 +331,32 @@ private void createEnableMoreUnitCodeMiningCheckbox(Composite parent)
329331
{
330332
enableMoreUnitCodeMining = new Button(parent, SWT.CHECK);
331333
enableMoreUnitCodeMining.setText(PreferenceConstants.TEXT_ENABLE_MOREUNIT_CODEMINING);
334+
enableMoreUnitCodeMining.setToolTipText(PreferenceConstants.TOOLTIP_ENABLE_MOREUNIT_CODEMINING);
332335
enableMoreUnitCodeMining.setLayoutData(layoutForOneLineControls);
333336
enableMoreUnitCodeMining.setSelection(preferences.shouldEnableMoreUnitCodeMining(javaProject));
337+
enableMoreUnitCodeMining.addSelectionListener(new SelectionAdapter()
338+
{
339+
@Override
340+
public void widgetSelected(SelectionEvent e)
341+
{
342+
enableJumpToMethodCodeMiningCheckbox.setEnabled(enableMoreUnitCodeMining.getSelection());
343+
}
344+
});
345+
}
346+
347+
private void createEnableJumpToMethodCodeMiningCheckbox(Composite parent)
348+
{
349+
enableJumpToMethodCodeMiningCheckbox = new Button(parent, SWT.CHECK);
350+
enableJumpToMethodCodeMiningCheckbox.setText(PreferenceConstants.TEXT_ENABLE_JUMP_TO_METHOD_CODE_MINING);
351+
enableJumpToMethodCodeMiningCheckbox.setToolTipText(PreferenceConstants.TOOLTIP_ENABLE_JUMP_TO_METHOD_CODE_MINING);
352+
353+
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
354+
gridData.horizontalSpan = 2;
355+
gridData.horizontalIndent = 20;
356+
enableJumpToMethodCodeMiningCheckbox.setLayoutData(gridData);
357+
358+
enableJumpToMethodCodeMiningCheckbox.setSelection(preferences.shouldEnableJumpToMethodCodeMining(javaProject));
359+
enableJumpToMethodCodeMiningCheckbox.setEnabled(enableMoreUnitCodeMining.getSelection());
334360
}
335361

336362
public void saveProperties()
@@ -359,6 +385,7 @@ public void saveProperties()
359385
preferences.setShouldUseTestMethodSearchByName(javaProject, enableSearchByNameCheckbox.getSelection());
360386
preferences.setGenerateCommentsForTestMethod(javaProject, addCommentsToTestMethodCheckbox.getSelection());
361387
preferences.setEnableMoreUnitCodeMining(javaProject, enableMoreUnitCodeMining.getSelection());
388+
preferences.setEnableJumpToMethodCodeMining(javaProject, enableJumpToMethodCodeMiningCheckbox.getSelection());
362389

363390
if(testAnnotationsByNameAndByCallButton.getSelection())
364391
{
@@ -452,6 +479,7 @@ public void setEnabled(boolean enabled)
452479
extendedSearchCheckbox.setEnabled(enabled);
453480
enableSearchByNameCheckbox.setEnabled(enabled);
454481
addCommentsToTestMethodCheckbox.setEnabled(enabled);
482+
enableJumpToMethodCodeMiningCheckbox.setEnabled(enabled && enableMoreUnitCodeMining.getSelection());
455483
testCaseNamePatternArea.setEnabled(enabled);
456484
testAnnotationsDisabledButton.setEnabled(enabled);
457485
testAnnotationsByNameButton.setEnabled(enabled);

0 commit comments

Comments
 (0)