diff --git a/CHANGELOG.md b/CHANGELOG.md index 041b6581347..3c4869a0d16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java index d66eb82a777..cd7f3a59e47 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java @@ -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); } } @@ -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()); + } } } diff --git a/src/main/java/org/jabref/logic/formatter/casechanger/ProtectTermsFormatter.java b/src/main/java/org/jabref/logic/formatter/casechanger/ProtectTermsFormatter.java index 3a933130f3b..002a4a6be95 100644 --- a/src/main/java/org/jabref/logic/formatter/casechanger/ProtectTermsFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/casechanger/ProtectTermsFormatter.java @@ -23,12 +23,15 @@ public ProtectTermsFormatter(ProtectedTermsLoader protectedTermsLoader) { private String format(String text, List 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; }