mirror of
https://github.com/alecthomas/chroma.git
synced 2025-07-17 01:22:22 +02:00
Consider baseLineNumber when calculating the column width
This commit is contained in:
committed by
Alec Thomas
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
|
wrapInTable := f.lineNumbers && f.lineNumbersInTable
|
||||||
|
|
||||||
lines := chroma.SplitTokensIntoLines(tokens)
|
lines := chroma.SplitTokensIntoLines(tokens)
|
||||||
lineDigits := len(fmt.Sprintf("%d", len(lines)))
|
lineDigits := len(fmt.Sprintf("%d", f.baseLineNumber+len(lines)-1))
|
||||||
highlightIndex := 0
|
highlightIndex := 0
|
||||||
|
|
||||||
if wrapInTable {
|
if wrapInTable {
|
||||||
|
@ -108,6 +108,51 @@ func TestTableLineNumberNewlines(t *testing.T) {
|
|||||||
</span>`)
|
</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) {
|
func TestWithPreWrapper(t *testing.T) {
|
||||||
wrapper := preWrapper{
|
wrapper := preWrapper{
|
||||||
start: func(code bool, styleAttr string) string {
|
start: func(code bool, styleAttr string) string {
|
||||||
|
Reference in New Issue
Block a user