From e6de3d1f1ae715f32a3b2b62f3129c88ba699e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= <782854+gandarez@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:54:52 -0300 Subject: [PATCH] Add CPP lexer (#813) --- lexers/cpp.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lexers/cpp.go diff --git a/lexers/cpp.go b/lexers/cpp.go new file mode 100644 index 000000000..e3c289640 --- /dev/null +++ b/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 +})