-- lx format
local format = require("core.format")
local patterns = require("core.format.patterns")
local python_references = require("format.python.references")
local quotes = require("core.text.quotes")

format.register({
	name = "Python Script",
	shortname = "Python",
	extensions = { "py", "pyw", "pyi", "rpy" },
	headers = {
		"^#!.*[ /]python",
	},
	comment = "#",

	file_references = python_references.new(),

	patterns = {
		patterns.shebang_line(),
		{ pattern = { "#", "\n" }, type = "comment" },
		{ pattern = { '[ruU]?"', '"', "\\" }, type = "string" },
		{ pattern = { "[ruU]?'", "'", "\\" }, type = "string" },
		{ pattern = { '"""', '"""' }, type = "string" },
		{ pattern = "0x[%da-fA-F]+", type = "number" },
		{ pattern = "-?%d+[%d%.eE]*", type = "number" },
		{ pattern = "-?%.?%d+", type = "number" },
		{ pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" },
		{ pattern = "[%a_][%w_]*%f[(]", type = "function" },
		{ pattern = "[%a_][%w_]*", type = "symbol" },
	},
	guard_comment = {
		guard_open = "if False:",
		guard_close = "",
		guard_body_prefix = "    ",
		guard_align_to_indent = true,
		allow_nested = true,
	},
	quotes = {
		primary = "double",
		secondary = "single",
		profiles = {
			double = quotes.profile.c_style_double(),
			single = quotes.profile.c_style_single(),
		},
	},
	symbols = {
		-- Logic & Operators
		["and"] = "keyword", -- Short-circuiting logical AND
		["in"] = "keyword", -- Membership or iteration
		["is"] = "keyword", -- Identity comparison
		["not"] = "keyword", -- Logical negation
		["or"] = "keyword", -- Short-circuiting logical OR

		-- Control Flow
		["break"] = "keyword", -- Exit loop
		["continue"] = "keyword", -- Skip to next loop iteration
		["elif"] = "keyword", -- Else-if branch
		["else"] = "keyword", -- Fallback branch
		["for"] = "keyword", -- Sequence iteration
		["if"] = "keyword", -- Conditional branch
		["pass"] = "keyword", -- Null operation placeholder
		["return"] = "keyword", -- Exit function and return value
		["while"] = "keyword", -- Conditional loop
		["yield"] = "keyword", -- Pause generator and return value

		-- Declarations & Structure
		["class"] = "keyword", -- Define a class
		["def"] = "keyword", -- Define a function or method
		["lambda"] = "keyword", -- Anonymous inline function
		["type"] = "keyword", -- Type alias statement (Python 3.12+)

		-- Asynchronous Programming
		["async"] = "keyword", -- Define coroutine
		["await"] = "keyword", -- Suspend execution for awaitable

		-- Structural Pattern Matching
		["case"] = "keyword", -- Pattern branch in match
		["match"] = "keyword", -- Pattern matching statement

		-- Exceptions & Debugging
		["assert"] = "keyword", -- Debugging check
		["except"] = "keyword", -- Catch exception block
		["finally"] = "keyword", -- Clean-up block after try
		["raise"] = "keyword", -- Explicitly trigger exception
		["try"] = "keyword", -- Start error-handling block

		-- Scoping & Modules
		--  ["as"]       = "keyword", -- Alias in imports or with-blocks
		["del"] = "keyword", -- Delete reference to object
		["from"] = "keyword", -- Relative/selective import
		["global"] = "keyword", -- Declare global scope variable
		["import"] = "keyword", -- Bring module into scope
		["nonlocal"] = "keyword", -- Declare outer-scope variable
		["with"] = "keyword", -- Context manager block

		-- Object Context (keyword2)
		["cls"] = "keyword2", -- Conventional class-method reference
		["self"] = "keyword2", -- Conventional instance reference

		-- Literals
		["False"] = "literal",
		["None"] = "literal",
		["True"] = "literal",
	},
})
