From 7d9649fba7f3b2ab8916b2bec5afb339dd3963b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 29 Jul 2026 18:20:37 -0700 Subject: [PATCH] Fix an issue with formatting indented list items. The previous code used a regular expression to skip over the indentation preceding a list item marker (as in ` - `). However, the CommonMark API has a method for that, `ListItem.getMarkerIndent()`. By using that to determine the "real" start of the list item token, we avoid an issue where the value of that token (`- `) did not correspond to the text at the position we had recorded. PiperOrigin-RevId: 956195264 --- .../java/javadoc/MarkdownPositions.java | 15 ++++++++------ .../java/JavadocFormattingTest.java | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java index 274520761..27ebfc441 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/javadoc/MarkdownPositions.java @@ -123,12 +123,13 @@ void visit(Node node) { // Returns true if this method visited the children of the given ListItem. private boolean visitListItem(ListItem listItem) { - int startPosition = listItem.getSourceSpans().getFirst().getInputIndex(); + int startPosition = + listItem.getSourceSpans().getFirst().getInputIndex() + listItem.getMarkerIndent(); Matcher matcher = LIST_ITEM_START_PATTERN.matcher(input).region(startPosition, input.length()); verify(matcher.lookingAt()); ListItemOpenTag openToken = new ListItemOpenTag(matcher.group(1)); - addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN); + addSpan(listItem, openToken, LIST_ITEM_CLOSE_TOKEN, startPosition); if (listItem.getFirstChild() instanceof Paragraph paragraph) { // A ListItem typically contains a Paragraph, but we don't want to visit that Paragraph // because that would lead us to introduce a line break after the list introduction @@ -215,8 +216,12 @@ private void visitNodeList(Node node) { * end position. */ private void addSpan(Node node, Token startToken, Token endToken) { + addSpan(node, startToken, endToken, startPosition(node)); + } + + private void addSpan(Node node, Token startToken, Token endToken, int startPosition) { // We could write the first part more simply as a `put`, but we do it this way for symmetry. - positionToToken.get(startPosition(node)).addLast(startToken); + positionToToken.get(startPosition).addLast(startToken); positionToToken.get(endPosition(node)).addFirst(endToken); } @@ -253,8 +258,6 @@ public String toString() { private static final ListCloseTag LIST_CLOSE_TOKEN = new ListCloseTag(""); private static final ListItemCloseTag LIST_ITEM_CLOSE_TOKEN = new ListItemCloseTag(""); - // The leading \s here works around what appears to be a CommonMark bug. We shouldn't ever see - // space at the purported start of a list item? private static final Pattern LIST_ITEM_START_PATTERN = - Pattern.compile("(?:\\s*)(([-+*]|[0-9]+[.)])(?:\\s|$))"); + Pattern.compile("(([-+*]|[0-9]+[.)])(?:\\s|$))"); } diff --git a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java index 60f59bdc4..b834bdd5f 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/JavadocFormattingTest.java @@ -1753,6 +1753,26 @@ class Test {} doFormatTest(input, expected); } + @Test + public void markdownIndentedListItem() { + assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue(); + String input = +""" +/// A list: +/// - `foo`: enabled by default +/// - `bar`: disabled by default +class Test {} +"""; + String expected = +""" +/// A list: +/// - `foo`: enabled by default +/// - `bar`: disabled by default +class Test {} +"""; + doFormatTest(input, expected); + } + @Test public void markdownEmptyListItem() { assume().that(MARKDOWN_JAVADOC_SUPPORTED).isTrue();