-- lx format
local format = require("core.format")
local cmake_references = require("format.cmake.references")

format.register({
	name = "CMakeLists",
	shortname = "CMake",
	author = "liqube",
	date = "2026-03-08";

	extensions = { "cmake" },
	filenames = { "CMakeLists.txt" },
	comment = "#",
	block_comment = { "#[[", "]]" },

	file_references = cmake_references.new(),

	patterns = {
		-- Comments
		{ pattern = { "#%[=*%[", "%]=*%]" }, type = "comment" },
		{ pattern = "#.*", type = "comment" },

		-- Bracket args
		{ pattern = { "%[=*%[", "%]=*%]" }, type = "string" },

		-- Strings
		{ pattern = { '"', '"', "\\" }, type = "string" },

		-- Variable-like references
		{ pattern = "%$?ENV%b{}", type = "symbol" },
		{ pattern = "%$?CACHE%b{}", type = "symbol" },
		{ pattern = "%${[^}%s]+}", type = "symbol" },

		-- Simple, non-nested matching for generator expressions: $<...>
		{ pattern = "%$<[^>\n]+>", type = "symbol" },

		-- Common CMAKE_* variables
		{ pattern = "(CMAKE_[%w_]+)[%s%)\n]", type = { "label" } },

		-- Commands
		{ pattern = "[%a_][%w_]*%s*%f[(]", type = "function" },

		-- Assignments
		{ pattern = "([%a_][%w_]*)%s*(=)", type = { "variable", "operator" }, not_preceded_by = "%-" },

		-- Words
		{ pattern = "[%a_][%w_]*", type = "normal" },
	},

-- TODO: These are a bit chaotic and far from complete
	symbols = {
		["ON"] = "literal",
		["OFF"] = "literal",
		["TRUE"] = "literal",
		["FALSE"] = "literal",
		["YES"] = "literal",
		["NO"] = "literal",
		["Y"] = "literal",
		["N"] = "literal",
		["IGNORE"] = "literal",
		["NOTFOUND"] = "literal",

		["AND"] = "operator",
		["OR"] = "operator",
		["NOT"] = "error", -- TODO: Use different color

		["POLICY"] = "keyword2",
		["TARGET"] = "keyword2",
		["TEST"] = "keyword2",
		["DEFINED"] = "keyword2",

		["EXISTS"] = "function",
		["IS_READABLE"] = "function",
		["IS_WRITABLE"] = "function",
		["IS_EXECUTABLE"] = "function",
		["IS_DIRECTORY"] = "function",
		["IS_SYMLINK"] = "function",
		["IS_ABSOLUTE"] = "function",
		["IS_NEWER_THAN"] = "function",
		["GLOB"] = "function",
		["FILTER"] = "function",
		["COPY"] = "function",
		["MOVE"] = "function",

		["EQUAL"] = "success",
		["LESS"] = "operator",
		["LESS_EQUAL"] = "success",
		["GREATER"] = "operator",
		["GREATER_EQUAL"] = "success",

		["STREQUAL"] = "success", -- TODO: Different style
		["STRLESS"] = "operator",
		["STRLESS_EQUAL"] = "success",
		["STRGREATER"] = "operator",
		["STRGREATER_EQUAL"] = "success",

		["VERSION_EQUAL"] = "operator",
		["VERSION_LESS"] = "operator",
		["VERSION_LESS_EQUAL"] = "operator",
		["VERSION_GREATER"] = "operator",
		["VERSION_GREATER_EQUAL"] = "operator",

		-- IDEA: operator_lt, operator_gt, operator_eq, operator_neq, operator_not, operator_lteq, operator_gteq
		["PATH_EQUAL"] = "success",
		["IN"] = "operator",
		["IN_LIST"] = "success",
		["LISTS"] = "keyword",
		["ITEMS"] = "keyword2",
		["MATCHES"] = "success",

		["PRIVATE"] = "keyword",
		["PUBLIC"] = "keyword",
		["INTERFACE"] = "keyword",
		["LINK_PRIVATE"] = "keyword",
		["LINK_PUBLIC"] = "keyword",

		["STATIC"] = "keyword2",
		["SHARED"] = "keyword2",
		["MODULE"] = "keyword2",
		["OBJECT"] = "keyword2",
		["SOURCES"] = "keyword",
		["INTERFACE_LIBRARY"] = "keyword2", -- Rare textual form
		["IMPORTED"] = "keyword2",
		["ALIAS"] = "keyword2",
		["GLOBAL"] = "keyword2",
		["UNKNOWN"] = "keyword2",

		-- Common built-ins often used as bare words
		["WIN32"] = "literal",
		["UNIX"] = "literal",
		["APPLE"] = "literal",
		["MSVC"] = "literal",
		["MINGW"] = "literal",
		["CYGWIN"] = "literal",
		["BORLAND"] = "literal",

		["WINDOWS"] = "literal",
		["RISCOS"] = "literal",

		["MSVC_RUNTIME_LIBRARY"] = "keyword",

		["OPTIONAL"] = "keyword",
		["REQUIRED"] = "keyword",
		["QUIET"] = "keyword",
		["EXACT"] = "keyword",

		["CACHE"] = "keyword",
		["BOOL"] = "keyword",
		["STRING"] = "keyword",
		["PATH"] = "keyword",
		["FILEPATH"] = "keyword",
		["INTERNAL"] = "keyword",
		["FORCE"] = "keyword2",

		["OUTPUT"] = "keyword",
		["COMMAND"] = "keyword2",
		["ARGS"] = "keyword2",
		["NAMES"] = "keyword2",
		["FILES"] = "keyword2";
		["PROGRAMS"] = "keyword",
		["STATUS"] = "keyword",
		["COMMENT"] = "keyword",
		["DEPENDS"] = "keyword",
		["WORKING_DIRECTORY"] = "keyword",
		["BYPRODUCTS"] = "keyword",
		["VERBATIM"] = "keyword",
		["APPEND"] = "function",
		["EXPORT"] = "function",
		["REPLACE"] = "function";
		["REMOVE_DUPLICATES"] = "function",
		["AFTER"] = "keyword2",
		["USES_TERMINAL"] = "keyword",
		["COMMAND_EXPAND_LISTS"] = "keyword",
        ["RESULT_VARIABLE"] = "keyword",
        ["OUTPUT_VARIABLE"] = "keyword",
        ["ERROR_VARIABLE"] = "keyword",
        ["INSTALL_PATH"] = "keyword",
        ["ALIAS_NAMESPACE"] = "keyword",

		["PRE_BUILD"] = "keyword",
		["PRE_LINK"] = "keyword",
		["POST_BUILD"] = "keyword",

		["DESTINATION"] = "keyword",
		["COMPONENT"] = "keyword",
		["CONFIGURATIONS"] = "keyword",
		["PATTERN"] = "keyword",
		["REGEX"] = "keyword",
		["EXCLUDE"] = "keyword",
		["RENAME"] = "keyword",
		["PERMISSIONS"] = "keyword",
		["LANGUAGES"] = "keyword2",
		["VERSION"] = "keyword",
		["RESOURCE_FILES"] = "keyword",
		["RESOURCE_FILE"] = "keyword",
			["LIBRARY"] = "keyword",
			["TARGETS"] = "keyword",
		["ARCHIVE"] = "keyword",
		["RUNTIME"] = "keyword",
		["FRAMEWORK"] = "keyword",
		["DIRECTORY"] = "keyword",
		["NAMESPACE"] = "keyword",

		["NONE"] = "keyword",
		["C"] = "keyword",
		["CXX"] = "keyword",
		["CSharp"] = "keyword",
		["CUDA"] = "keyword",
		["OBJC"] = "keyword",
		["OBJCXX"] = "keyword",
		["Fortran"] = "keyword",
		["HIP"] = "keyword",
		["ISPC"] = "keyword",
		["Swift"] = "keyword",
		["ASM"] = "keyword",
		["ASM_NASM"] = "keyword",
		["ASM_MARMASM"] = "keyword",
		["ASM_MASM"] = "keyword",
		["ASM-ATT"] = "keyword",

		["COMPILE_OPTIONS"] = "keyword",
		["COMPILE_FLAGS"] = "keyword",
		["CONFIGURE_DEPENDS"] = "keyword",
		["WIN32_EXECUTABLE"] = "keyword",

		-- Properties
		["PROPERTY"] = "keyword2",
		["PROPERTIES"] = "keyword2",

		-- TODO: Style "property"
		["HEADER_FILE_ONLY"] = "keyword",
		["GENERATED"] = "keyword",
		["SYMBOLIC"] = "keyword",
		["LANGUAGE"] = "keyword",
		["LABELS"] = "keyword",
		["LOCATION"] = "keyword",
		["TYPE"] = "keyword",
			["SOVERSION"] = "keyword",
		["OUTPUT_NAME"] = "keyword",
		["EXPORT_NAME"] = "keyword",
		["FOLDER"] = "keyword",
		["POSITION_INDEPENDENT_CODE"] = "keyword",
		["C_STANDARD"] = "keyword",
		["C_STANDARD_REQUIRED"] = "keyword",
		["C_EXTENSIONS"] = "keyword",
		["CXX_STANDARD"] = "keyword",
		["CXX_STANDARD_REQUIRED"] = "keyword",
		["CXX_EXTENSIONS"] = "keyword",
		["CUDA_STANDARD"] = "keyword",
		["CUDA_STANDARD_REQUIRED"] = "keyword",
		["CUDA_EXTENSIONS"] = "keyword",

		["INCLUDE_DIRECTORIES"] = "keyword",
		["COMPILE_DEFINITIONS"] = "keyword",
			["COMPILE_FEATURES"] = "keyword",
		["LINK_OPTIONS"] = "keyword",
		["LINK_DIRECTORIES"] = "keyword",
		["LINK_LIBRARIES"] = "keyword",
		["INTERFACE_INCLUDE_DIRECTORIES"] = "keyword",
		["INTERFACE_COMPILE_DEFINITIONS"] = "keyword",
		["INTERFACE_COMPILE_OPTIONS"] = "keyword",
		["INTERFACE_COMPILE_FEATURES"] = "keyword",
		["INTERFACE_LINK_OPTIONS"] = "keyword",
		["INTERFACE_LINK_DIRECTORIES"] = "keyword",
		["INTERFACE_LINK_LIBRARIES"] = "keyword",
		["INTERFACE_SOURCES"] = "keyword",

		["ENVIRONMENT"] = "keyword",
		["TIMEOUT"] = "keyword",

			["SOURCE_DIR"] = "keyword",
		["BINARY_DIR"] = "keyword",
		["RULE_LAUNCH_COMPILE"] = "keyword",
		["RULE_LAUNCH_LINK"] = "keyword",
		["RULE_LAUNCH_CUSTOM"] = "keyword",

		["ARCHIVE_OUTPUT_DIRECTORY"] = "keyword",
		["LIBRARY_OUTPUT_DIRECTORY"] = "keyword",
		["RUNTIME_OUTPUT_DIRECTORY"] = "keyword",
		["PDB_OUTPUT_DIRECTORY"] = "keyword",
		["COMPILE_PDB_OUTPUT_DIRECTORY"] = "keyword",

		["ARCHIVE_OUTPUT_NAME"] = "keyword",
		["LIBRARY_OUTPUT_NAME"] = "keyword",
		["RUNTIME_OUTPUT_NAME"] = "keyword",
		["PDB_NAME"] = "keyword",
		["COMPILE_PDB_NAME"] = "keyword",

		["DEBUG_POSTFIX"] = "keyword",
		["PREFIX"] = "keyword",
		["SUFFIX"] = "keyword",
		["IMPORT_PREFIX"] = "keyword",
		["IMPORT_SUFFIX"] = "keyword",

			["IMPORTED_GLOBAL"] = "keyword",
		["IMPORTED_IMPLIB"] = "keyword",
		["IMPORTED_LIBNAME"] = "keyword",
		["IMPORTED_LINK_DEPENDENT_LIBRARIES"] = "keyword",
		["IMPORTED_LINK_INTERFACE_LANGUAGES"] = "keyword",
		["IMPORTED_LINK_INTERFACE_LIBRARIES"] = "keyword",
		["IMPORTED_LINK_OPTIONS"] = "keyword",
		["IMPORTED_LOCATION"] = "keyword",
		["IMPORTED_NO_SONAME"] = "keyword",
		["IMPORTED_OBJECTS"] = "keyword",
		["IMPORTED_SONAME"] = "keyword",

		["ALIASED_TARGET"] = "keyword",
		["NAME"] = "keyword",
		["ENABLED_LANGUAGES"] = "keyword",
		["SUBDIRECTORIES"] = "keyword",
		["BUILDSYSTEM_TARGETS"] = "keyword",
		["TESTS"] = "keyword",
		["VARIABLES"] = "keyword",
		["MACROS"] = "keyword",

		["copy_if_different"] = "function",

		["FATAL_ERROR"] = "error",
		["WARNING"] = "warning",
	}
})
