-- lx syntax
-- based on lite language_diff plugin

local syntax = require("core.syntax")
local style = require("core.style")
local common = require("core.common")

-- Define custom syntax colors for diff additions and deletions
-- These allow the theme to distinguish between added and removed lines
style.syntax["diff_add"] = common.color("#72b886")
style.syntax["diff_del"] = common.color("#F36161")

syntax.add({
	name = "Diff",
	extensions = { "diff", "patch", "rej" },

	-- Magic header at very start of diff file
	headers = "^diff %-",
	comment = "#",

	line_patterns = {
		{ pattern = "# .+", type = "comment", consume_line = true },
	},

	patterns = {
		-- Highlighting the diff command line as a function call
		{ pattern = "diff .*", type = "function", line_start = true },
		-- New file permission metadata
		{ pattern = "new .*", type = "comment", line_start = true },
		-- Git index hashes
		{ pattern = "index .*", type = "comment", line_start = true },

		-- In vanilla lite, we can't split the type for '@@ -1,1 +1,1 @@ context'
		-- We choose 'number' as it is the most prominent part of the hunk header
		--  { pattern = "@@ [%d,%-%+ ]+ @@",     type = "literal",   line_start = true },
		{ pattern = "@@.-@@.-[\r\n]", type = "literal", line_start = true },
		--  { pattern = "@@.-[\r\n]",          type = "function",   line_start = true },

		-- Traditional hunk headers (--- 1,1 ----)
		{ pattern = "%-%-%- %d+,%d+ %-%-%-%-", type = "number", line_start = true },
		{ pattern = "%*%*%* %d+,%d+ %*%*%*%*", type = "number", line_start = true },

		-- File path markers (--- and +++)
		-- We use 'keyword' to make the file names stand out
		{ pattern = "%-%-%- .*", type = "function", line_start = true },
		{ pattern = "%+%+%+ .*", type = "function", line_start = true },
		{ pattern = "%*%*%* .*", type = "normal", line_start = true },

		-- Separator used in git patches
		{ pattern = "%-%-%-", type = "normal", line_start = true },

		-- The core diff content: deletions, additions, and context changes
		{ pattern = "%-.*", type = "diff_del", line_start = true },
		{ pattern = "%+.*", type = "diff_add", line_start = true },
		{ pattern = "<.*", type = "diff_del", line_start = true },
		{ pattern = ">.*", type = "diff_add", line_start = true },
		{ pattern = "!.*", type = "number", line_start = true },

		-- Author/Patch metadata
		-- Simplified to a single type since vanilla lite doesn't support capture-to-type mapping
		{ pattern = "From [a-fA-F%d]+ .*", type = "keyword", line_start = true },

		-- Standard headers (e.g., Author: Name)
		{ pattern = "[%a%-]+: ", type = "keyword", line_start = true },

		-- Statistics usually found at the bottom of a git format-patch
		{ pattern = " %d+ files? changed", type = "function", line_start = true },
		{ pattern = "%d+ insertions?%(%+%+%)", type = "diff_add" },
		{ pattern = "%d+ deletions?%(%-%-%)", type = "diff_del" },

		-- Email addresses wrapped in angle brackets
		-- Rendered as a string for visual distinction
		{ pattern = "<[%w%._%+%-%d]+@[%w%-]+%.[%w%-%.]+>", type = "string" },
	},

	outline_patterns = {
		{ pattern = "@@.-", line_start = true },
	},
})
