Skip to content

Commit

Permalink
Improve list management in replies (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwrobel committed Nov 8, 2022
1 parent a113762 commit f945c40
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed

- Improve list management in replies (#86).

### Fixed

- Fix mention recognition in messages (#87).
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/com/lescastcodeurs/bot/ShowNoteReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import com.lescastcodeurs.bot.slack.SlackReply;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public final class ShowNoteReply {

public static final int DEFAULT_ORDER = 999;

private static final Pattern LIST_ITEM_PATTERN = Pattern.compile("^ *-");

private final SlackReply reply;

public ShowNoteReply(SlackReply reply) {
Expand Down Expand Up @@ -47,10 +50,21 @@ public String timestamp() {
}

public Stream<String> comments() {
return reply
.asMarkdown()
String markdown = reply.asMarkdown();
boolean isPlainList = markdown.startsWith("-");

return markdown
.lines()
.filter(not(String::isBlank))
.map(line -> (!line.startsWith("-") && !line.startsWith(" -")) ? "- " + line : line);
.map(
line -> {
if (LIST_ITEM_PATTERN.matcher(line).find()) {
// Lists that do not belong to plain lists messages have to be shifted. See
// corresponding tests.
return isPlainList ? line : " " + line;
} else {
return "- " + line;
}
});
}
}
82 changes: 82 additions & 0 deletions src/test/java/com/lescastcodeurs/bot/ShowNoteReplyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,88 @@ void multilineMessagesAreSplit() {
assertEquals(List.of("- test 1", "- test 2"), reply.comments().toList());
}

@Test
void plainListIsConvertedAsIs() {
SlackReply message =
new SlackReply(
Messages.of("""
- test 1
- test 2
- test 3
"""));
var reply = new ShowNoteReply(message);

assertEquals(List.of("- test 1", "- test 2", "- test 3"), reply.comments().toList());
}

@Test
void nonPlainListAreConvertedToSublist() {
SlackReply message =
new SlackReply(
Messages.of(
"""
Test:
- test 1
- test 2
- test 3
"""));
var reply = new ShowNoteReply(message);

assertEquals(
List.of("- Test:", " - test 1", " - test 2", " - test 3"), reply.comments().toList());
}

@Test
void nonPlainListAreConvertedToSublist2() {
SlackReply message =
new SlackReply(
Messages.of(
"""
Test A:
- test A1
- test A12
- test A2
- test A3
Test B:
- test B1
- test B2
- test B3
"""));
var reply = new ShowNoteReply(message);

assertEquals(
List.of(
"- Test A:",
" - test A1",
" - test A12",
" - test A2",
" - test A3",
"- Test B:",
" - test B1",
" - test B2",
" - test B3"),
reply.comments().toList());
}

@Test
void plainListWithSublistIsConvertedAsIs() {
SlackReply message =
new SlackReply(
Messages.of(
"""
- test 1
- subtest 1
- subtest 2
- test 2
"""));
var reply = new ShowNoteReply(message);

assertEquals(
List.of("- test 1", " - subtest 1", " - subtest 2", "- test 2"),
reply.comments().toList());
}

@Test
void excludeReactionAlwaysWins() {
SlackReply message =
Expand Down

0 comments on commit f945c40

Please sign in to comment.