Skip to content

Commit

Permalink
Bug fix to UlIndentRule to align with parent ordered list items
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmook committed Mar 14, 2019
1 parent 08269d4 commit ba800c6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.appmattus.markdown.rules

import com.appmattus.markdown.processing.MarkdownDocument
import com.appmattus.markdown.dsl.RuleSetup
import com.appmattus.markdown.errors.ErrorReporter
import com.appmattus.markdown.rules.extentions.indent
import com.appmattus.markdown.rules.extentions.level
import com.appmattus.markdown.processing.MarkdownDocument
import com.vladsch.flexmark.ast.BulletListItem
import com.vladsch.flexmark.ast.ListItem
import com.vladsch.flexmark.ast.OrderedListItem
import com.vladsch.flexmark.util.ast.Document

/**
* # Unordered list indentation
Expand Down Expand Up @@ -32,7 +34,8 @@ import com.appmattus.markdown.rules.extentions.level
* [Sub-lists not indenting](http://support.markedapp.com/discussions/problems/21-sub-lists-not-indenting) for a
* description of the problem.
*
* Based on [MD007](https://github.com/markdownlint/markdownlint/blob/master/lib/mdl/rules.rb)
* Based on [MD007](https://github.com/markdownlint/markdownlint/blob/master/lib/mdl/rules.rb) and
* [MD007](https://github.com/DavidAnson/markdownlint/blob/master/lib/md007.js)
*/
class UlIndentRule(
private val indent: Int = 2,
Expand All @@ -44,9 +47,32 @@ class UlIndentRule(

override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) {
document.unorderedListItems.forEach {
if (it.indent() != it.level() * indent) {
val parentListItem = it.parentListItemOrNull()

var expectedMarkerPos = parentListItem?.let {
document.chars.getColumnAtIndex(parentListItem.openingMarker.startOffset) + indent
} ?: 0

if (parentListItem is OrderedListItem) {
// When contained in an ordered list, align with it's content
expectedMarkerPos = document.chars.getColumnAtIndex(parentListItem.childChars.startOffset)
}

val actualMarkerPos = document.chars.getColumnAtIndex(it.openingMarker.startOffset)

if (expectedMarkerPos != actualMarkerPos) {
errorReporter.reportError(it.startOffset, it.endOffset, description)
}
}
}

fun BulletListItem.parentListItemOrNull(): ListItem? {
var cur = parent
while (cur !is Document) {
cur = cur.parent
if (cur is ListItem)
return cur
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import org.spekframework.spek2.style.gherkin.Feature

object UlIndentRuleTest : Spek({
Feature("UlIndentRule") {
FileRuleScenario(listOf("ul_indent_bugfixes.md")) { UlIndentRule() }

FileRuleScenario(listOf("ul_indent_bugfixes_4_spaces.md")) { UlIndentRule(indent = 4) }

FileRuleScenario(listOf("bulleted_list_2_space_indent.md")) { UlIndentRule(indent = 4) }

FileRuleScenario(listOf("spaces_after_list_marker.md")) { UlIndentRule(indent = 4) }
Expand Down
2 changes: 1 addition & 1 deletion markdown/src/test/resources/spaces_after_list_marker.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ And with incorrect spacing:
Ordered lists with children:

1. Foo {ListMarkerSpaceRule}
* Hi
* Hi {UlIndentRule} complains as should align with content above
1. Bar {ListMarkerSpaceRule}
1. Baz {ListMarkerSpaceRule}

Expand Down
13 changes: 13 additions & 0 deletions markdown/src/test/resources/ul_indent_bugfixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. List item
- List item {UlIndentRule} top level item as not indented enough
- List item
- List item
- List item {UlIndentRule} top level item as not indented enough
1. List item

1. List item
- List item
- List item
- List item
- List item
1. List item
13 changes: 13 additions & 0 deletions markdown/src/test/resources/ul_indent_bugfixes_4_spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. List item
- List item
- List item
- List item
- List item
1. List item

1. List item
- List item
- List item
- List item
- List item
1. List item

0 comments on commit ba800c6

Please sign in to comment.