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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BLE in SearchEngine and add test #428

Merged
merged 3 commits into from
Feb 25, 2022

Conversation

paul-griffith
Copy link
Contributor

Fixes #427

Catching and rethrowing the BLE is pretty hokey... but to be fair, catching it and dumping the stack trace wasn't much better 馃槈

Copy link
Owner

@bobbylight bobbylight left a comment

Choose a reason for hiding this comment

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

One question and one suggestion!

@@ -72,7 +72,7 @@ public static SearchResult find(JTextArea textArea, SearchContext context) {
boolean doMarkAll = textArea instanceof RTextArea && context.getMarkAll();

String text = context.getSearchFor();
if (text==null || text.length()==0) {
if (text==null || text.length()==0 || textArea.getText().isEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

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

This empty-check is expensive for large documents, can you update this to be:

Suggested change
if (text==null || text.length()==0 || textArea.getText().isEmpty()) {
if (text == null || text.isEmpty() || textArea.getDocument().getLength() == 0) {

@@ -262,7 +262,7 @@ private static String getFindInText(JTextArea textArea, int start,
}
} catch (BadLocationException ble) {
// Never happens; findIn will be null anyway.
ble.printStackTrace();
throw new RuntimeException(ble);
Copy link
Owner

Choose a reason for hiding this comment

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

I'm worried this will screw up the EDT, if an uncaught exception is thrown. At least that's my recollection. That's why I'm currently just doing a printStackTrace() (which I agree with you is pretty hokey). Is that a concern?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I think you're right about the EDT... but if it's not re-thrown, it can't actually be caught in a test. I guess that's probably the lesser of two evils here though.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I'm not necessaarily happy with it but like the comment says - it's not supposed to happen :). Fortunately you caught a bug where it does!

RSTA follows this pattern of calling Throwable.printStackTrace() everywhere that e.g. BadLocationException is thrown by Swing API's but shouldn't happen "in real life" due to using offsets fetched from the Document, etc. I was reluctant to rethrow checked exceptions like BadLocationException everywhere, since that adds complexity for client apps, and also didn't want to add a logging dependency to keep RSTA lightweight. I'm happy folks are looking at other ways to approach this kind of thing though.

In any case, thanks for the PR!

@paul-griffith
Copy link
Contributor Author

I'm not sure what happened with the failing build - seems unrelated to my changes?

@bobbylight bobbylight merged commit c953940 into bobbylight:master Feb 25, 2022
@bobbylight
Copy link
Owner

@paul-griffith Yes, the Java 17 build currently fails; we have one test that uses Mockito and it looks like something changed starting in Java 15, where the mock's behavior is different and no longer works properly. It's not related to your change, or RSTA at all. I've opened this Mockito ticket to track it. If it can't be addressed easily I'll try to look at a different way to verify stuff in that test to avoid the problem.

@bobbylight bobbylight added this to the 3.1.7 milestone Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BadLocationException thrown in SearchEngine if searchWrap = true and searchForward = false in empty document
2 participants