-- lx format
local format = require("core.format")
local quotes = require("core.text.quotes")
local vocabulary = require("format.ahkv1.vocabulary")

format.register({
	name = "AutoHotkey v1 Script",
	shortname = "AHKv1",
	extensions = { "ahk" },
	comment = ";",

	patterns = {
		-- Comments
		{ pattern = ";.*", type = "comment" },
		{ pattern = { "/%*", "%*/" }, type = "comment" },

		-- Hotkeys and hotstrings (surface syntax)
		{ pattern = "%s*(:[%*%?%w%-_]+:.-)\n", type = { "meta" }, line_start = true },
		{ pattern = "%s*([^%s:][^:]-::)", type = { "meta" }, line_start = true },

		-- #IfWin... directives (WinTitle is typically a title string, followed by optional ahk_* criteria)
		{ pattern = "%s*(#IfWinActive%s*,?%s*)(.-)(%s+ahk_%a+)%f[%s]",     type = { "keyword", "string", "keyword2" }, line_start = true },
		{ pattern = "%s*(#IfWinNotActive%s*,?%s*)(.-)(%s+ahk_%a+)%f[%s]",  type = { "keyword", "string", "keyword2" }, line_start = true },
		{ pattern = "%s*(#IfWinExist%s*,?%s*)(.-)(%s+ahk_%a+)%f[%s]",      type = { "keyword", "string", "keyword2" }, line_start = true },
		{ pattern = "%s*(#IfWinNotExist%s*,?%s*)(.-)(%s+ahk_%a+)%f[%s]",   type = { "keyword", "string", "keyword2" }, line_start = true },

		-- Same directives when there is no ahk_* criterion on the line
		{ pattern = "%s*(#IfWinActive%s*,?%s*)(.-)%s*$",     type = { "keyword", "string" }, line_start = true },
		{ pattern = "%s*(#IfWinNotActive%s*,?%s*)(.-)%s*$",  type = { "keyword", "string" }, line_start = true },
		{ pattern = "%s*(#IfWinExist%s*,?%s*)(.-)%s*$",      type = { "keyword", "string" }, line_start = true },
		{ pattern = "%s*(#IfWinNotExist%s*,?%s*)(.-)%s*$",   type = { "keyword", "string" }, line_start = true },

		{ pattern = "%s*#.-\n", type = "preprocessor", line_start = true },

		-- WinTitle criteria (AHK v1)
		{ pattern = "(ahk_exe%s+)([%w%._%-%\\:/]+)",     type = { "keyword2", "string" } }, -- process name or path
		{ pattern = "(ahk_class%s+)([%w%._%-%#]+)",      type = { "keyword2", "string" } }, -- class name (often includes #)
		{ pattern = "(ahk_group%s+)([%w%._%-%#]+)",      type = { "keyword2", "string" } }, -- group name
		{ pattern = "(ahk_pid%s+)(%d+)",                 type = { "keyword2", "number" } }, -- PID is decimal
		{ pattern = "(ahk_id%s+)(0x[%da-fA-F]+)",        type = { "keyword2", "hex" } },    -- HWND often written in hex

{pattern=" not ", type = "error" },

		-- Active window shorthand (WinTitle = A)
		{ pattern = "%s*#IfWin%a+%s*,?%s*A%s*$", type = "literal", line_start = true },

		-- Continuation section start (basic)
		{ pattern = "%s*%(", type = "meta", line_start = true },

		-- Expression assignment
		{ pattern = "([%a_][%w_]*)%s*(:=)", type = { "variable", "operator" } },

		-- Strings
		{ pattern = { '"', '"' }, type = "string" }, -- normal/empty strings
		{ pattern = '""', type = "string" }, -- doubled quote inside strings ("" escapes a double-quoted string in AHK v1)
		{ pattern = { "'", "'" }, type = "string" }, -- single-quoted strings

		-- Legacy percent expansion (sparingly)
		{ pattern = "%%[%a_][%w_]*%%", type = "variable" },

		-- Braced keys: {Enter}, {F5}, {Enter 5}, {Tab}, etc
		-- Braced keys: {Enter}, {Enter 5}, {F12}, etc
		{ pattern = "({)([%a%d][%w%d%s]*)(})", type = { "operator", "constant", "operator" } },

		-- Numbers
		{ pattern = "0x[%da-fA-F]+", type = "hex" },
		{ pattern = "-?%d+[%d%.eE]*", type = "number" },
		{ pattern = "-?%.?%d+", type = "number" },

		-- Assignment operators
		{ pattern = ":=", type = "operator" },
		{ pattern = "%.=", type = "operator" },

		-- Operators and punctuation
		{ pattern = "[%+%-=/%*%^<>!~|&%?:%.%,;%[%]%(%)%{%}]", type = "operator" },

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

		-- Variables (expression style)
		{ pattern = "%b%%", type = "variable" }, -- %var% (best-effort)
--		{ pattern = "[%a_][%w_]*", type = "symbol" },
	},

	outline_patterns = {
		{ pattern = "%s*[%a_][%w_]*%s*%b()", line_start = true },
		{ pattern = "%s*class%s+[%a_][%w_]*", line_start = true },
		{ pattern = "%s*[^%s:][^:]-::", line_start = true },
		{ pattern = "%s*:[%*%?%w%-_]+:.-$", line_start = true },
	},

	block_comment = {
		block_open = "/*",
		block_close = "*/",
		block_line_prefix = " ",
		close_line_prefix = " ",
		open_on_own_line = false,
		close_on_own_line = true,
		strip_line_prefix = "^%s*%*?%s?",
		allow_nested = false,
	},

	quotes = {
		primary = "double",
		secondary = "single",
		profiles = {
			double = quotes.profile.c_style_double(),
			single = quotes.profile.c_style_single(),
		},
	},

	symbols = vocabulary,
})
