mirror of
https://github.com/alecthomas/chroma.git
synced 2025-02-05 13:05:18 +02:00
3044bf5f32
This PR changes `CommentSingle` to not consume the newline at the end as a part of comment. That solves the problems of single line comment being not parsed at the end of the line or at the end of the file. Which was reported earlier as the reason to not highlight single line comment properly. Disabling `EnsureNL: true` does not add unnecessary newline element for `Text`, `CommentSymbol` symbols. Using chroma in console with syntax highlighting was unusable becasue of this, since typing e.g. `b := ` adds newline each time space is at the end when host app asks for highlighted text from `quick`. Tokens behavior: <table> <tr> <td> Before </td> <td> After </td> </tr> <tr> <td> ``` go t.Run("Single space", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, " ") expected := []chroma.Token{ {chroma.Text, " \n"}, } assert.Equal(t, expected, tokens) }) t.Run("Assignment unfinished", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, "i = ") expected := []chroma.Token{ { chroma.NameOther, "i" }, { chroma.Text, " " }, { chroma.Punctuation, "=" }, { chroma.Text, " \n" }, } assert.Equal(t, expected, tokens) }) t.Run("Single comment", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, "// W") expected := []chroma.Token{ { chroma.CommentSingle, "// W\n" }, } assert.Equal(t, expected, tokens) }) ``` </td> <td> ``` go t.Run("Single space", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, " ") expected := []chroma.Token{ {chroma.Text, " "}, } assert.Equal(t, expected, tokens) }) t.Run("Assignment unfinished", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, "i = ") expected := []chroma.Token{ { chroma.NameOther, "i" }, { chroma.Text, " " }, { chroma.Punctuation, "=" }, { chroma.Text, " " }, } assert.Equal(t, expected, tokens) }) t.Run("Single comment", func(t *testing.T) { tokens, _ := chroma.Tokenise(Go, nil, "// W") expected := []chroma.Token{ { chroma.CommentSingle, "// W" }, } assert.Equal(t, expected, tokens) }) ``` </td> </tr> </table>
22 lines
345 B
Plaintext
22 lines
345 B
Plaintext
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
fmt.Println("Hello World!")
|
|
}
|
|
|
|
var n int = 0x21 + 1_000
|
|
var n2 float64 = 1e3
|
|
|
|
func hello(a int) {
|
|
fmt.Println("Hello World!").Hello()
|
|
|
|
return func() int {
|
|
return i
|
|
}
|
|
} // One last thing
|
|
|
|
type Int interface {
|
|
~int | ~int8 | ~int16 | ~int32 | ~int64
|
|
} // The very last comment w/o LF |