alecthomas/chroma

`lexer.Match` does not work as intended

frapa opened this issue · 5 comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A Lexer can define multiple glob pattern that it matches using the <filename> tag in the lexer XML config. However, this does not work as intended, it seems to be matching only the first defined glob pattern.

For instance I passed "Dockerfile" and it returned the correct lexer, however passing "dev.Dockerfile" did not.

Note that the lexer correctly defined the glob pattern here.

I could have a look if I manage to fix this myself, if you accept contributions.

To Reproduce

lexer.Match("dev.Dockerfile") does not return the correct lexer.

I can't replicate this:

func TestMatch(t *testing.T) {
	assert.NotZero(t, lexers.Match("foo.Dockerfile"))
}

Passes fine. Reopen if you can provide a test case that fails.

Hi @alecthomas,

Thanks for having a look! For me that test fails. Proof:

image

As text

package main

import (
	"testing"

	"github.com/alecthomas/chroma/lexers"
	"github.com/stretchr/testify/assert"
)

func TestMatch(t *testing.T) {
        // First returns lexer (PASS)
	assert.NotEqual(t, nil, lexers.Match("Dockerfile"))
        // First SHOULD return lexer, but returns nil lexer (PASS)
	assert.Equal(t, nil, lexers.Match("foo.Dockerfile"))
        // Code from issue (FAILS)
	assert.NotZero(t, lexers.Match("foo.Dockerfile"))
}

I am currently on go 1.21.6.

This happens for me for different file types as well.

You are using the old version of Chroma. The correct import is v2:

github.com/alecthomas/chroma/v2/lexers

I realised the README doesn't explicitly state this so I've updated it.

Oh, I did not know that! Thanks a lot and thanks for updating the Readme!