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

Fix "Protected Terms" formatting #10415 #10524

Merged
merged 11 commits into from
Oct 23, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where there was a failure to access the url link for "eprint" for the ArXiv entry.[#10474](https://github.com/JabRef/jabref/issues/10474)
- We fixed an issue where it was not possible to connect to a shared database once a group with entries was added or other metadata modified [#10336](https://github.com/JabRef/jabref/issues/10336)
- We fixed an issue where middle-button paste in X not always worked [#7905](https://github.com/JabRef/jabref/issues/7905)
- We fixed an issue where the added protected term has unwanted leading and trailing whitespaces, where the formatted text has unwanted empty brackets and where the word after or at the cursor in the textbox can be added to the list [#10415](https://github.com/JabRef/jabref/issues/10415).
yuyan-z marked this conversation as resolved.
Show resolved Hide resolved

## [5.10] – 2023-09-02

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ private class ProtectSelectionAction extends SimpleCommand {
@Override
public void execute() {
String selectedText = textInputControl.getSelectedText();
textInputControl.replaceSelection("{" + selectedText + "}");
String firstStr = "{";
String lastStr = "}";
// If the selected text contains spaces at the beginning and end, then add spaces before or after the brackets
if (selectedText.startsWith(" ")) {
firstStr = " {";
}
if (selectedText.endsWith(" ")) {
lastStr = "} ";
}
textInputControl.replaceSelection(firstStr + selectedText.strip() + lastStr);
}
}

Expand Down Expand Up @@ -99,12 +108,29 @@ public AddToProtectedTermsAction(ProtectedTermsList list) {
Objects.requireNonNull(list);

this.list = list;
this.executable.bind(textInputControl.selectedTextProperty().isNotEmpty());
this.executable.bind(textInputControl.focusedProperty());
}

@Override
public void execute() {
list.addProtectedTerm(textInputControl.getSelectedText());
// If no selected term, then add the word after or at the cursor
if (textInputControl.getSelectedText().isEmpty()) {
int beginIdx = textInputControl.getCaretPosition();
int endIdx = textInputControl.getCaretPosition();
String text = textInputControl.getText();
// While the beginIdx > 0 and the previous char is not a space
while (beginIdx > 0 && text.charAt(beginIdx - 1) != ' ') {
--beginIdx;
}
// While the endIdx < length and the current char is not a space
while (endIdx < text.length() && text.charAt(endIdx) != ' ') {
++endIdx;
}
list.addProtectedTerm(text.substring(beginIdx, endIdx));
} else {
// Remove leading and trailing whitespaces
list.addProtectedTerm(textInputControl.getSelectedText().strip());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ public ProtectTermsFormatter(ProtectedTermsLoader protectedTermsLoader) {

private String format(String text, List<String> listOfWords) {
String result = text;
// Treat longer terms first to avoid substring issues
listOfWords.sort(new StringLengthComparator());
// For each word in the list
for (String listOfWord : listOfWords) {
// Add {} if the character before is a space, -, /, (, [, ", or } or if it is at the start of the string but not if it is followed by a }
result = result.replaceAll("(^|[- /\\[(}\"])" + listOfWord + "($|[^a-zA-Z}])", "$1\\{" + listOfWord + "\\}$2");
}
// Remove the empty brackets
result = result.replace("{}", "");
return result;
}

Expand Down