language_cpp: improvements and fixes

* Removed pcall(require, "plugins.language_c") since it doesn't works
  as it seems to have been intended.
* Removed duplicate keywords
* Added support for magic and uppercase constants.
* Basically merged most changes from the lite-xl-plugins repo.
This commit is contained in:
jgmdev 2022-03-11 17:02:42 -04:00
parent eeb2b28a03
commit 4b0531cdfc
1 changed files with 55 additions and 23 deletions

View File

@ -1,6 +1,4 @@
-- mod-version:2 -- lite-xl 2.0 -- mod-version:2 -- lite-xl 2.0
pcall(require, "plugins.language_c")
local syntax = require "core.syntax" local syntax = require "core.syntax"
syntax.add { syntax.add {
@ -12,27 +10,52 @@ syntax.add {
comment = "//", comment = "//",
block_comment = { "/*", "*/" }, block_comment = { "/*", "*/" },
patterns = { patterns = {
{ pattern = "//.-\n", type = "comment" }, { pattern = "//.-\n", type = "comment" },
{ pattern = { "/%*", "%*/" }, type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" },
{ pattern = { '"', '"', '\\' }, type = "string" }, { pattern = { '"', '"', '\\' }, type = "string" },
{ pattern = { "'", "'", '\\' }, type = "string" }, { pattern = { "'", "'", '\\' }, type = "string" },
{ pattern = "0x%x+", type = "number" }, { pattern = "0x%x+", type = "number" },
{ pattern = "%d+[%d%.eE]*f?", type = "number" }, { pattern = "%d+[%d%.eE]*f?", type = "number" },
{ pattern = "%.?%d+f?", type = "number" }, { pattern = "%.?%d+f?", type = "number" },
{ pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" }, {
pattern = "^%s*#define%s+()[%a_][%a%d_]*",
type = { "keyword", "symbol" }
},
{
pattern = "#include%s+()<.->",
type = { "keyword", "string" }
},
{ pattern = "[%+%-=/%*%^%%<>!~|:&]", type = "operator" },
{ pattern = "struct%s()[%a_][%w_]*", type = {"keyword", "keyword2"} }, { pattern = "struct%s()[%a_][%w_]*", type = {"keyword", "keyword2"} },
{ pattern = "class%s()[%a_][%w_]*", type = {"keyword", "keyword2"} }, { pattern = "class%s()[%a_][%w_]*", type = {"keyword", "keyword2"} },
{ pattern = "union%s()[%a_][%w_]*", type = {"keyword", "keyword2"} }, { pattern = "union%s()[%a_][%w_]*", type = {"keyword", "keyword2"} },
{ pattern = "namespace%s()[%a_][%w_]*", type = {"keyword", "keyword2"} }, { pattern = "namespace%s()[%a_][%w_]*", type = {"keyword", "keyword2"} },
{ pattern = "[%a_][%w_]*::", type = "symbol" }, { pattern = "[%a_][%w_]*%f[(]", type = "function" },
{ pattern = "::", type = "symbol" }, -- Match scope operator element access
{ pattern = "[%a_][%w_]*", type = "symbol" }, { pattern = "[%a_][%w_]*[%s]*%f[:]", type = "literal" },
{ pattern = "#include%s()<.->", type = {"keyword", "string"} }, -- Uppercase constants of at least 2 chars in len
{ pattern = "#[%a_][%w_]*", type = "keyword" }, {
pattern = "_?%u[%u_][%u%d_]*%f[%s%+%*%-%.%(%)%?%^%%=/<>~|&;:,!]",
type = "number"
},
-- Magic constants
{ pattern = "__[%u%l]+__", type = "number" },
-- Somehow makes macros properly work
{ pattern = "#[%a_][%w_]*", type = "normal" },
-- Everything else to make the tokenizer work properly
{ pattern = "[%a_][%w_]*", type = "symbol" },
}, },
symbols = { symbols = {
["alignof"] = "keyword", ["alignof"] = "keyword",
["alignas"] = "keyword", ["alignas"] = "keyword",
["and"] = "keyword",
["and_eq"] = "keyword",
["not"] = "keyword",
["not_eq"] = "keyword",
["or"] = "keyword",
["or_eq"] = "keyword",
["xor"] = "keyword",
["xor_eq"] = "keyword",
["private"] = "keyword", ["private"] = "keyword",
["protected"] = "keyword", ["protected"] = "keyword",
["public"] = "keyword", ["public"] = "keyword",
@ -40,9 +63,12 @@ syntax.add {
["nullptr"] = "keyword", ["nullptr"] = "keyword",
["operator"] = "keyword", ["operator"] = "keyword",
["asm"] = "keyword", ["asm"] = "keyword",
["bitand"] = "keyword",
["bitor"] = "keyword",
["catch"] = "keyword", ["catch"] = "keyword",
["throw"] = "keyword", ["throw"] = "keyword",
["try"] = "keyword", ["try"] = "keyword",
["class"] = "keyword",
["compl"] = "keyword", ["compl"] = "keyword",
["explicit"] = "keyword", ["explicit"] = "keyword",
["export"] = "keyword", ["export"] = "keyword",
@ -52,8 +78,8 @@ syntax.add {
["constinit"] = "keyword", ["constinit"] = "keyword",
["const_cast"] = "keyword", ["const_cast"] = "keyword",
["dynamic_cast"] = "keyword", ["dynamic_cast"] = "keyword",
["reinterpret_cast"] = "keyword", ["reinterpret_cast"] = "keyword",
["static_cast"] = "keyword", ["static_cast"] = "keyword",
["static_assert"] = "keyword", ["static_assert"] = "keyword",
["template"] = "keyword", ["template"] = "keyword",
["this"] = "keyword", ["this"] = "keyword",
@ -64,7 +90,6 @@ syntax.add {
["co_yield"] = "keyword", ["co_yield"] = "keyword",
["decltype"] = "keyword", ["decltype"] = "keyword",
["delete"] = "keyword", ["delete"] = "keyword",
["export"] = "keyword",
["friend"] = "keyword", ["friend"] = "keyword",
["typeid"] = "keyword", ["typeid"] = "keyword",
["typename"] = "keyword", ["typename"] = "keyword",
@ -72,6 +97,7 @@ syntax.add {
["override"] = "keyword", ["override"] = "keyword",
["virtual"] = "keyword", ["virtual"] = "keyword",
["using"] = "keyword", ["using"] = "keyword",
["namespace"] = "keyword",
["new"] = "keyword", ["new"] = "keyword",
["noexcept"] = "keyword", ["noexcept"] = "keyword",
["if"] = "keyword", ["if"] = "keyword",
@ -85,6 +111,8 @@ syntax.add {
["continue"] = "keyword", ["continue"] = "keyword",
["return"] = "keyword", ["return"] = "keyword",
["goto"] = "keyword", ["goto"] = "keyword",
["struct"] = "keyword",
["union"] = "keyword",
["typedef"] = "keyword", ["typedef"] = "keyword",
["enum"] = "keyword", ["enum"] = "keyword",
["extern"] = "keyword", ["extern"] = "keyword",
@ -96,7 +124,6 @@ syntax.add {
["case"] = "keyword", ["case"] = "keyword",
["default"] = "keyword", ["default"] = "keyword",
["auto"] = "keyword", ["auto"] = "keyword",
["const"] = "keyword",
["void"] = "keyword2", ["void"] = "keyword2",
["int"] = "keyword2", ["int"] = "keyword2",
["short"] = "keyword2", ["short"] = "keyword2",
@ -106,12 +133,18 @@ syntax.add {
["char"] = "keyword2", ["char"] = "keyword2",
["unsigned"] = "keyword2", ["unsigned"] = "keyword2",
["bool"] = "keyword2", ["bool"] = "keyword2",
["true"] = "keyword2", ["true"] = "literal",
["false"] = "keyword2", ["false"] = "literal",
["NULL"] = "literal",
["wchar_t"] = "keyword2",
["char8_t"] = "keyword2",
["char16_t"] = "keyword2",
["char32_t"] = "keyword2",
["#include"] = "keyword", ["#include"] = "keyword",
["#if"] = "keyword", ["#if"] = "keyword",
["#ifdef"] = "keyword", ["#ifdef"] = "keyword",
["#ifndef"] = "keyword", ["#ifndef"] = "keyword",
["#elif"] = "keyword",
["#else"] = "keyword", ["#else"] = "keyword",
["#elseif"] = "keyword", ["#elseif"] = "keyword",
["#endif"] = "keyword", ["#endif"] = "keyword",
@ -119,6 +152,5 @@ syntax.add {
["#warning"] = "keyword", ["#warning"] = "keyword",
["#error"] = "keyword", ["#error"] = "keyword",
["#pragma"] = "keyword", ["#pragma"] = "keyword",
}, },
} }