Skip to content

Commit 0eb694b

Browse files
Update the search syntax highlight for web search (#13801)
* Renamed DefaultLuceneQueryTransformer class to DefaultSearchQueryTransformer * Changed the highlighting logic for web search to use the new ANTLR based parser syntax * Ran rewrite * Removed invalid DOI check as the new parser doesn't support checking for validity of DOI * Reused the existing l10n for error message * Re-use existing code * Modern Java * Fix comment * Re-add test * Fix position --------- Co-authored-by: Oliver Kopp <[email protected]>
1 parent 90d38ec commit 0eb694b

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

jabgui/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.jabref.logic.importer.WebFetchers;
2323
import org.jabref.logic.l10n.Localization;
2424
import org.jabref.logic.util.BackgroundTask;
25+
import org.jabref.model.search.query.SearchQuery;
2526
import org.jabref.model.strings.StringUtil;
2627
import org.jabref.model.util.OptionalUtil;
2728

@@ -30,12 +31,9 @@
3031
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
3132
import de.saxsys.mvvmfx.utils.validation.ValidationStatus;
3233
import de.saxsys.mvvmfx.utils.validation.Validator;
33-
import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException;
34-
import org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser;
35-
import org.apache.lucene.queryparser.flexible.standard.parser.ParseException;
36-
import org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser;
37-
38-
import static org.jabref.logic.importer.fetcher.transformers.AbstractQueryTransformer.NO_EXPLICIT_FIELD;
34+
import org.antlr.v4.runtime.RecognitionException;
35+
import org.antlr.v4.runtime.Token;
36+
import org.antlr.v4.runtime.misc.ParseCancellationException;
3937

4038
public class WebSearchPaneViewModel {
4139

@@ -47,7 +45,6 @@ public class WebSearchPaneViewModel {
4745
private final StateManager stateManager;
4846

4947
private final Validator searchQueryValidator;
50-
private final SyntaxParser parser = new StandardSyntaxParser();
5148

5249
public WebSearchPaneViewModel(GuiPreferences preferences, DialogService dialogService, StateManager stateManager) {
5350
this.dialogService = dialogService;
@@ -85,18 +82,23 @@ public WebSearchPaneViewModel(GuiPreferences preferences, DialogService dialogSe
8582
}
8683

8784
try {
88-
parser.parse(queryText, NO_EXPLICIT_FIELD);
85+
// The result is ignored because we just check for validity
86+
SearchQuery.getStartContext(queryText);
8987
return null;
90-
} catch (ParseException e) {
91-
String element = e.currentToken.image;
92-
int position = e.currentToken.beginColumn;
93-
if (element == null) {
94-
return ValidationMessage.error(Localization.lang("Invalid query. Check position %0.", position));
95-
} else {
96-
return ValidationMessage.error(Localization.lang("Invalid query element '%0' at position %1", element, position));
88+
} catch (ParseCancellationException e) {
89+
// RecognitionException can point out the exact error
90+
if (e.getCause() instanceof RecognitionException recEx) {
91+
Token offendingToken = recEx.getOffendingToken();
92+
93+
// The character position is 0-based, so we add 1 for user-friendliness.
94+
int line = offendingToken.getLine();
95+
int charPositionInLine = offendingToken.getCharPositionInLine() + 1;
96+
97+
return ValidationMessage.error(Localization.lang("Invalid query element '%0' at position %1", line, charPositionInLine));
9798
}
98-
} catch (QueryNodeParseException e) {
99-
return ValidationMessage.error("");
99+
100+
// Fallback for other failing reasons
101+
return ValidationMessage.error(Localization.lang("Invalid query"));
100102
}
101103
});
102104
}

jabgui/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ void canExtractDOIFromQueryText() {
7171
}
7272

7373
@Test
74-
void queryConsistingOfInvalidDOIIsInvalid() {
74+
void queryConsistingOfInvalidDOIIsValid() {
7575
viewModel.queryProperty().setValue("101.1007/JHEP02(2023)082");
76-
assertFalse(viewModel.queryValidationStatus().validProperty().getValue());
76+
// There is currently no interpretation of nearly-valid identifiers, therefore, this is concidered as "regular" search term
77+
assertTrue(viewModel.queryValidationStatus().validProperty().getValue());
7778
}
7879

7980
@Test
@@ -90,7 +91,7 @@ void canExtractISBNFromQueryText() {
9091

9192
@Test
9293
void queryConsistingOfArXivIdIsValid() {
93-
viewModel.queryProperty().setValue("arXiv:2110.02957");
94+
viewModel.queryProperty().setValue("arXiv=2110.02957");
9495
assertTrue(viewModel.queryValidationStatus().validProperty().getValue());
9596
}
9697

jablib/src/main/resources/l10n/JabRef_en.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ You\ must\ restart\ JabRef\ for\ this\ to\ come\ into\ effect.=You must restart
10011001

10021002
Could\ not\ find\ fetcher\ '%0'=Could not find fetcher '%0'
10031003
Running\ query\ '%0'\ with\ fetcher\ '%1'.=Running query '%0' with fetcher '%1'.
1004-
Invalid\ query.\ Check\ position\ %0.=Invalid query. Check position %0.
1004+
Invalid\ query=Invalid query
10051005
Invalid\ query\ element\ '%0'\ at\ position\ %1=Invalid query element '%0' at position %1
10061006

10071007
Move\ file=Move file

0 commit comments

Comments
 (0)