Skip to content

Commit

Permalink
Fix "Protected Terms" formatting #10415 (#10524)
Browse files Browse the repository at this point in the history
* Update ProtectTermsFormatter.java

* Update ProtectedTermsMenu.java

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update ProtectedTermsMenu.java

* Update ProtectedTermsMenu.java

* Update CHANGELOG.md

* Update CHANGELOG.md

---------

Co-authored-by: Oliver Kopp <[email protected]>
  • Loading branch information
yuyan-z and koppor authored Oct 23, 2023
1 parent 8cd15bc commit d55b7ff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Fixed

- 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 at the cursor in the textbox can be added to the list [#10415](https://github.com/JabRef/jabref/issues/10415).

### Removed

## [5.11] – 2023-10-22
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

0 comments on commit d55b7ff

Please sign in to comment.