Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Feb 7, 2021
1 parent 2ffadce commit 56bbdf0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
20 changes: 12 additions & 8 deletions parser/code_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ func (b *codeBlockParser) Continue(node ast.Node, reader text.Reader, pc Context

// if code block line starts with a tab, keep a tab as it is.
if segment.Padding != 0 {
offsetWithPadding := reader.LineOffset()
sl, ss := reader.Position()
reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop))
if offsetWithPadding == reader.LineOffset() {
segment.Padding = 0
segment.Start--
}
reader.SetPosition(sl, ss)
preserveLeadingTabInCodeBlock(&segment, reader)
}

node.Lines().Append(segment)
Expand Down Expand Up @@ -90,3 +83,14 @@ func (b *codeBlockParser) CanInterruptParagraph() bool {
func (b *codeBlockParser) CanAcceptIndentedLine() bool {
return true
}

func preserveLeadingTabInCodeBlock(segment *text.Segment, reader text.Reader) {
offsetWithPadding := reader.LineOffset()
sl, ss := reader.Position()
reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop))
if offsetWithPadding == reader.LineOffset() {
segment.Padding = 0
segment.Start--
}
reader.SetPosition(sl, ss)
}
9 changes: 1 addition & 8 deletions parser/fcode_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C
fdata := pc.Get(fencedCodeBlockInfoKey).(*fenceData)
// if code block line starts with a tab, keep a tab as it is.
if segment.Padding != 0 {
offsetWithPadding := reader.LineOffset()
sl, ss := reader.Position()
reader.SetPosition(sl, text.NewSegment(ss.Start-1, ss.Stop))
if offsetWithPadding == reader.LineOffset() {
segment.Padding = 0
segment.Start--
}
reader.SetPosition(sl, ss)
preserveLeadingTabInCodeBlock(&segment, reader)
}
w, pos := util.IndentWidth(line, reader.LineOffset())
if w < 4 {
Expand Down

0 comments on commit 56bbdf0

Please sign in to comment.