Skip to content

Commit

Permalink
Simplify handling of too-short indent calculation, and remove the 'co…
Browse files Browse the repository at this point in the history
…uldn't infer' error. Closes #2985
  • Loading branch information
tabatkins committed Jan 9, 2025
1 parent 2623e7a commit 138406a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
8 changes: 0 additions & 8 deletions bikeshed/Spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,6 @@ def assembleDocument(self) -> Spec:
if "mixed-indents" in self.md.complainAbout:
if self.md.indentInfo and self.md.indentInfo.char:
checkForMixedIndents(self.lines, self.md.indentInfo)
elif len(self.lines) > 50:
# Only complain about a failed inference if it's long
# enough that I could reasonably infer something.
m.warn(
"`Complain About: mixed-indents yes` is active, but I couldn't infer the document's indentation. Be more consistent, or turn this lint off.",
)
else:
pass

# Deal with further <pre> blocks, and markdown
self.lines = datablocks.transformDataBlocks(self, self.lines)
Expand Down
14 changes: 10 additions & 4 deletions bikeshed/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,14 +1096,19 @@ class IndentInfo:
char: str | None = None
spaceLines: int = 0
tabLines: int = 0
totalLines: int = 0
neitherLines: int = 0

def tooShort(self) -> bool:
return (self.spaceLines + self.tabLines) < 20


def inferIndent(lines: t.Sequence[Line]) -> IndentInfo:
# Infer what indent character the document is using.
# If the document uses space indentation,
# infer what the indent size is by seeing what % of indents
# are divisible by various values.
# (If it uses tabs, or no indents at all, returns None.)
# A sufficiently short document, or one with too few indented lines,
# will fail to infer anything.
indentSizes: Counter[int] = Counter()
info = IndentInfo()
for line in lines:
Expand All @@ -1115,9 +1120,10 @@ def inferIndent(lines: t.Sequence[Line]) -> IndentInfo:
info.spaceLines += 1
elif line.text[0:1] == "\t":
info.tabLines += 1
info.totalLines += 1
else:
info.neitherLines += 1
# If very few lines are indented at all, just bail
if (info.spaceLines + info.tabLines) < info.totalLines / 20:
if info.tooShort():
return info
# If tab indents predominate, assume tab-indent.
if info.spaceLines < info.tabLines:
Expand Down

0 comments on commit 138406a

Please sign in to comment.