Skip to content

Commit

Permalink
Add CPP lexer (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Aug 18, 2023
1 parent 68d976f commit e6de3d1
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lexers/cpp.go
@@ -0,0 +1,36 @@
package lexers

import (
"regexp"

. "github.com/alecthomas/chroma/v2" // nolint
)

var (
cppAnalyserIncludeRe = regexp.MustCompile(`#include <[a-z_]+>`)
cppAnalyserNamespaceRe = regexp.MustCompile(`using namespace `)
)

var CPP = Register(MustNewXMLLexer(
embedded,
"embedded/c++.xml",
).SetConfig(
&Config{
Name: "C++",
Aliases: []string{"cpp", "c++"},
Filenames: []string{"*.cpp", "*.hpp", "*.c++", "*.h++", "*.cc", "*.hh", "*.cxx", "*.hxx", "*.C", "*.H", "*.cp", "*.CPP", "*.cppm", "*.ixx"},
MimeTypes: []string{"text/x-c++hdr", "text/x-c++src"},
Priority: 0.1,
EnsureNL: true,
},
)).SetAnalyser(func(text string) float32 {
if cppAnalyserIncludeRe.MatchString(text) {
return 0.2
}

if cppAnalyserNamespaceRe.MatchString(text) {
return 0.4
}

return 0
})

0 comments on commit e6de3d1

Please sign in to comment.