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

Switch colors of search icon for the two search modes #3871

Merged
merged 10 commits into from
Apr 12, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The new default removes the linked file from the entry instead of deleting the f
- We fixed an issue where linked files would be deleted from bibliography entries despite choosing the "Cancel" option in the dialog menu.
- We fixed the name of the group editing window to "Add group" instead of "Edit Group" when adding a new group. [koppor#277](https://github.com/koppor/jabref/issues/277)
- We fixed an issue where the "Copy linked files" dialog produced an error when the entry had no file [#3818](https://github.com/JabRef/jabref/issues/3818)
- We fixed the coloring of the search box icon in case a user switches to advanced search mode [#3870](https://github.com/JabRef/jabref/issues/3870)

### Removed
- We removed the [Look and Feels from JGoodies](http://www.jgoodies.com/freeware/libraries/looks/), because the open source version is not compatible with Java 9.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/search/SearchQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private String getLocalizedRegularExpressionDescription() {
}

public boolean isGrammarBasedSearch() {
return rule instanceof GrammarBasedSearchRule;
return rule instanceof GrammarBasedSearchRule || rule instanceof RegexBasedSearchRule;
Copy link
Member

Choose a reason for hiding this comment

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

Should regex really be treated as grammar-based search? I would not treat it like that, because regex search is orthogonal to grammar-based search. One can activate it in addition.

Copy link
Member Author

Choose a reason for hiding this comment

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

At least in the documentation: https://help.jabref.org/en/Search#search-modes they are listed under the heading of "advanced search". But I can easily change that of course.

Copy link
Member

Choose a reason for hiding this comment

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

I updated the help accordingly to also reflect that regular expressions are orthogonal. 😇

Could you additionally add a comment there that this is the "Advanced Search" described in the help? I know, this contradicts DDD, but I think, it is OK in this place.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I adapted the code accordingly.

}

public String getQuery() {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/jabref/model/search/rules/SearchRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import org.jabref.model.strings.StringUtil;

import java.util.regex.Pattern;

public class SearchRules {

private SearchRules() {
}

private static final Pattern simpleExpression = Pattern.compile("(\\w|\\d|\\s)*");

/**
* Returns the appropriate search rule that fits best to the given parameter.
*/
public static SearchRule getSearchRuleByQuery(String query, boolean caseSensitive, boolean regex) {
if (StringUtil.isBlank(query)) {
if (isSimpleQuery(query)) {
Copy link
Member

Choose a reason for hiding this comment

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

Reads good!

return new ContainBasedSearchRule(caseSensitive);
}

Expand All @@ -25,6 +29,10 @@ public static SearchRule getSearchRuleByQuery(String query, boolean caseSensitiv
}
}

private static boolean isSimpleQuery(String query) {
return StringUtil.isBlank(query) || simpleExpression.matcher(query).matches();
Copy link
Member

Choose a reason for hiding this comment

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

Do you really need both? The current regex has a star (*) at the end, which means zero (0) ore more matches. So, the empty string should match, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good suggestion, that sounds reasonable. I removed the StringUtil dependency and as far as I can see there is no difference in the search.

}

private static SearchRule getSearchRule(boolean caseSensitive, boolean regex) {
if (regex) {
return new RegexBasedSearchRule(caseSensitive);
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/org/jabref/logic/search/SearchQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public void testToString() {

@Test
public void testIsContainsBasedSearch() {
assertFalse(new SearchQuery("asdf", true, false).isContainsBasedSearch());
assertFalse(new SearchQuery("asdf", true, true).isContainsBasedSearch());
assertTrue(new SearchQuery("asdf", true, false).isContainsBasedSearch());
assertTrue(new SearchQuery("asdf", true, true).isContainsBasedSearch());
assertFalse(new SearchQuery("author=asdf", true, false).isContainsBasedSearch());
}

@Test
public void testIsGrammarBasedSearch() {
assertTrue(new SearchQuery("asdf", true, false).isGrammarBasedSearch());
assertTrue(new SearchQuery("asdf", true, true).isGrammarBasedSearch());
assertFalse(new SearchQuery("asdf", true, false).isGrammarBasedSearch());
assertFalse(new SearchQuery("asdf", true, true).isGrammarBasedSearch());
assertTrue(new SearchQuery("author=asdf", true, false).isGrammarBasedSearch());
}

Expand Down Expand Up @@ -186,4 +186,13 @@ public void isMatchedForNormalAndFieldBasedSearchMixed() {

}

@Test
public void testSimpleTerm() {
String query = "progress";

SearchQuery result = new SearchQuery(query, false, false);

assertFalse(result.isGrammarBasedSearch());
}

}