-- lx syntax
local syntax = require("core.syntax")

syntax.add({
	name = "R",
	shortname = "R",
	author = "liqube",
	extensions = { "r", "R", "Rprofile", "rds", "rda", "rdata" },
	comment = "#",

	patterns = {
		-- Roxygen docs
		{ pattern = "#'.-\n", type = "comment2" },

		-- Line comments (rest of line)
		{ pattern = "#.-\n", type = "comment" },

		-- Strings (handle escapes)
		{ pattern = { '"', '"', "\\" }, type = "string" },
		{ pattern = { "'", "'", "\\" }, type = "string" },

		-- Backtick function calls: `foo bar`(...)
		{ pattern = "`[^`\n]+`%f[(]", type = "function" },

		-- Backtick identifiers: `foo bar`
		{ pattern = { "`", "`", "\\" }, type = "symbol" },

		-- Namespaces and slot access / member access
		{ pattern = ":::", type = "operator" },
		{ pattern = "::", type = "operator" },
		{ pattern = "@", type = "operator" },
		{ pattern = "%$", type = "operator" },

		-- Common multi-char operators (order matters: longer first)
		{ pattern = "<<%-", type = "operator" }, -- <<-
		{ pattern = "<%-", type = "operator" },  -- <-
		{ pattern = "%-%>%>", type = "operator" }, -- ->>
		{ pattern = "%-%>", type = "operator" }, -- ->
		{ pattern = "|>", type = "operator" },

		{ pattern = "==", type = "operator" },
		{ pattern = "~=", type = "operator" },
		{ pattern = "<=", type = "operator" },
		{ pattern = ">=", type = "operator" },
		{ pattern = "&&", type = "operator" },
		{ pattern = "||", type = "operator" },

		-- Infix operators like %>%  %in%  %*%  %/%  %+%  etc
		{ pattern = "%%[^%%\n]+%%", type = "operator" },

		-- Single-char operators / punctuation that matters in R
		{ pattern = "[%+%-%*/%^=<>!&|~:,;%(%)%[%]{}]", type = "operator" },

		-- Numbers (hex, decimal, scientific, integer suffix L, complex suffix i)
		{ pattern = "0x[%da-fA-F]+", type = "hex" },
		{ pattern = "%d+%.%d*([eE][%+%-]?%d+)?[Li]?", type = "number" }, -- 1. 1.0 1.2e3  3.1L  2.0i
		{ pattern = "%.%d+([eE][%+%-]?%d+)?[Li]?", type = "number" },     -- .5 .5e2
		{ pattern = "%d+([eE][%+%-]?%d+)?[Li]?", type = "number" },       -- 10 10e-3 10L 10i

		-- Function calls: identifier immediately followed by "(" (allow dot-leading and dots inside)
		-- R names: (letter or . not followed by digit) then letters/digits/._ ; underscore also appears in practice
		{ pattern = "[%a_][%w_%.]*%f[(]", type = "function" },
		{ pattern = "%.[%a_][%w_%.]*%f[(]", type = "function" },

		-- Symbols / identifiers (again allow dot-leading names)
		{ pattern = "[%a_][%w_%.]*", type = "symbol" },
		{ pattern = "%.[%a_][%w_%.]*", type = "symbol" },
	},

	-- Outline jump points: common function definitions
	outline_patterns = {
		-- name <- function(...)
		{ pattern = "^%s*([%a_][%w_%.]*|%.[%a_][%w_%.]*)%s*<%-%s*function%s*%(", type = "function" },
		-- name = function(...)
		{ pattern = "^%s*([%a_][%w_%.]*|%.[%a_][%w_%.]*)%s*=%s*function%s*%(", type = "function" },
	},

	symbols = {
		-- Keywords
		["if"] = "keyword",
		["else"] = "keyword",
		["repeat"] = "keyword",
		["while"] = "keyword",
		["for"] = "keyword",
		["in"] = "keyword",
		["next"] = "keyword",
		["break"] = "keyword",
		["function"] = "keyword",

		-- Literals / special values
		["TRUE"] = "literal",
		["FALSE"] = "literal",
		["NULL"] = "literal",
		["NA"] = "literal",
		["NaN"] = "literal",
		["Inf"] = "literal",

		-- Typed NAs (these are values, not keywords)
		["NA_integer_"] = "literal",
		["NA_real_"] = "literal",
		["NA_complex_"] = "literal",
		["NA_character_"] = "literal",

		-- Common special symbols
		["..."] = "keyword2",
		["..1"] = "keyword2",
		["..2"] = "keyword2",
		["..3"] = "keyword2",
		["pi"] = "literal",
	},
})
