--- Build strict file-reference rules for Visual Studio solutions
-- @module formats.sln.references

local sln_references = {}

-- Pattern breakdown: skip the project type and display name, ending immediately before the project path quote
local project_prefix = '^%s*Project%b()%s*=%s*"[^"]*"%s*,%s*'
local assignment_prefix = "^%s*[^=]-=%s*"

--- Return whether one solution target looks like a local project file
-- @param reference table parsed file reference
-- @return boolean accepted
local function is_project_file(reference)
	local path = reference.path

	return path:match("[^/\\]+%.[^/\\]+$") ~= nil
		and (path:match("^%a:[/\\]") ~= nil or path:match("^%a[%w+%.%-]*:") == nil)
end

--- Return whether one assignment repeats one simple file path on both sides
-- @param reference table parsed file reference
-- @param line_text string complete source line
-- @return boolean accepted
local function is_solution_item(reference, line_text)
	local left = line_text:match("^%s*(.-)%s*=")

	return left == reference.path
		and line_text:sub(reference.col2):match("^%s*$") ~= nil
		and is_project_file(reference)
end

--- Return project files and simple solution items
-- @return table rules
function sln_references.new()
	return {
		{ prefix = project_prefix, open = "\"", close = "\"", validate = is_project_file },
		{ prefix = assignment_prefix, validate = is_solution_item },
	}
end

return sln_references
