mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-25 21:39:02 +02:00
Consider baseLineNumber when calculating the column width
This commit is contained in:
parent
bac74c1016
commit
9e22bd6e2f
@ -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 {
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user