Skip to content

Commit

Permalink
Consider baseLineNumber when calculating the column width
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus authored and alecthomas committed Jan 9, 2020
1 parent bac74c1 commit 9e22bd6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion formatters/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []chroma.
wrapInTable := f.lineNumbers && f.lineNumbersInTable

lines := chroma.SplitTokensIntoLines(tokens)
lineDigits := len(fmt.Sprintf("%d", len(lines)))
lineDigits := len(fmt.Sprintf("%d", f.baseLineNumber+len(lines)-1))
highlightIndex := 0

if wrapInTable {
Expand Down
45 changes: 45 additions & 0 deletions formatters/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,51 @@ func TestTableLineNumberNewlines(t *testing.T) {
</span>`)
}

func TestTableLineNumberSpacing(t *testing.T) {
testCases := []struct {
baseLineNumber int
expectedBuf string
}{{
7,
`<span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span>`,
}, {
6,
`<span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span>`,
}, {
5,
`<span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span>`,
}}
for i, testCase := range testCases {
f := New(
WithClasses(true),
WithLineNumbers(true),
LineNumbersInTable(true),
BaseLineNumber(testCase.baseLineNumber),
)
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
assert.NoError(t, err)
var buf bytes.Buffer
err = f.Format(&buf, styles.Fallback, it)
assert.NoError(t, err, "Test Case %d", i)
assert.Contains(t, buf.String(), testCase.expectedBuf, "Test Case %d", i)
}
}

func TestWithPreWrapper(t *testing.T) {
wrapper := preWrapper{
start: func(code bool, styleAttr string) string {
Expand Down

0 comments on commit 9e22bd6

Please sign in to comment.