diff --git a/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java b/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java index daec60f6..5dbbec1c 100644 --- a/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java +++ b/code/services-application/search-service/src/main/java/nu/marginalia/search/command/commands/BangCommand.java @@ -53,14 +53,13 @@ public class BangCommand implements SearchCommandInterface { if (!bm.isRelativeSpaceOrInvalid(bangKey.length())) continue; - StringBuilder ret = new StringBuilder(bm.prefix().trim()); + String prefix = bm.prefix().trim(); + String suffix = bm.suffix(bangKey.length()).trim(); - if (!ret.isEmpty()) - ret.append(" "); + String ret = STR."\{prefix} \{suffix}".trim(); - ret.append(bm.suffix(bangKey.length()).trim()); - - return Optional.of(ret.toString()); + return Optional.of(ret) + .filter(s -> !s.isBlank()); } return Optional.empty(); diff --git a/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java b/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java index 047b646d..90288678 100644 --- a/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java +++ b/code/services-application/search-service/src/test/java/nu/marginalia/search/command/commands/BangCommandTest.java @@ -34,4 +34,20 @@ class BangCommandTest { assertEquals(match.get(), "test"); } + @Test + public void testMatchPattern2() { + var match = bangCommand.matchBangPattern("test !g", "!g"); + + assertTrue(match.isPresent()); + assertEquals(match.get(), "test"); + } + + @Test + public void testMatchPattern3() { + var match = bangCommand.matchBangPattern("hello !g world", "!g"); + + assertTrue(match.isPresent()); + assertEquals(match.get(), "hello world"); + } + } \ No newline at end of file