Skip to content

Commit

Permalink
Fix FindAndReplaceTarget to work with IFindReplaceLogic
Browse files Browse the repository at this point in the history
  • Loading branch information
merks committed Oct 1, 2024
1 parent 20eeab9 commit e810c43
Showing 1 changed file with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,34 @@ public void applyStyles(TextStyle textStyle)
*/
private static final Field F_TARGET_FIELD;

/**
* Provides access to the {@code findReplaceLogic} of a {@code org.eclipse.ui.texteditor.FindReplaceDialog}.
*/
private static final Field FIND_REPLACE_LOGIC_FIELD;

static
{
Field fIsRegExCheckBoxField = null;
Field fSelectedRangeRadioButtonField = null;
Field fTarget = null;
Field findReplaceLogic = null;
try
{
Class<?> findReplaceDialogClass = CommonPlugin.loadClass("org.eclipse.ui.workbench.texteditor", "org.eclipse.ui.texteditor.FindReplaceDialog");
fIsRegExCheckBoxField = findReplaceDialogClass.getDeclaredField("fIsRegExCheckBox");
fIsRegExCheckBoxField.setAccessible(true);
fSelectedRangeRadioButtonField = findReplaceDialogClass.getDeclaredField("fSelectedRangeRadioButton");
fSelectedRangeRadioButtonField.setAccessible(true);
fTarget = findReplaceDialogClass.getDeclaredField("fTarget");
fTarget.setAccessible(true);
try
{
fTarget = findReplaceDialogClass.getDeclaredField("fTarget");
fTarget.setAccessible(true);
}
catch (Throwable throwable)
{
findReplaceLogic = findReplaceDialogClass.getDeclaredField("findReplaceLogic");
findReplaceLogic.setAccessible(true);
}
}
catch (Throwable throwable)
{
Expand All @@ -193,6 +207,7 @@ public void applyStyles(TextStyle textStyle)
F_IS_REGEX_CHECK_BOX_FIELD = fIsRegExCheckBoxField;
F_SELECTED_RANGE_RADIO_BUTTON_FIELD = fSelectedRangeRadioButtonField;
F_TARGET_FIELD = fTarget;
FIND_REPLACE_LOGIC_FIELD = findReplaceLogic;
}

/**
Expand Down Expand Up @@ -755,22 +770,42 @@ public void run()
{
// It may be the case that the properties view has focus and the editor being closed is the last one.
// The find-and-replace target is not updated in this case.
// So set it to null to ensure that the
// So set it to null to ensure that the target is updated.
//
Object fieldValue = getFieldValue(dialog, F_TARGET_FIELD);
if (fieldValue == FindAndReplaceTarget.this)
if (F_TARGET_FIELD != null)
{
try
{
Method method = dialog.getClass().getMethod("updateTarget", IFindReplaceTarget.class, boolean.class, boolean.class);
method.setAccessible(true);
method.invoke(dialog, null, false, false);
}
catch (Exception exception)
Object fieldValue = getFieldValue(dialog, F_TARGET_FIELD);
if (fieldValue == FindAndReplaceTarget.this)
{
// Ignore.
try
{
Method method = dialog.getClass().getMethod("updateTarget", IFindReplaceTarget.class, boolean.class, boolean.class);
method.setAccessible(true);
method.invoke(dialog, null, false, false);
}
catch (Exception exception)
{
// Ignore.
}
}
}
else
{
Object fieldValue = getFieldValue(dialog, FIND_REPLACE_LOGIC_FIELD);
try
{
Object target = FIND_REPLACE_LOGIC_FIELD.getType().getMethod("getTarget").invoke(fieldValue);
if (target == FindAndReplaceTarget.this)
{
fieldValue.getClass().getMethod("updateTarget", IFindReplaceTarget.class, boolean.class).invoke(fieldValue, null, false);
}
}
catch (Exception exception)
{
// Ignore.
}

}
disposeCleanup = null;
}
};
Expand Down

0 comments on commit e810c43

Please sign in to comment.