Property definitions

nitc $ Message :: defaultinit
# A warning or an error
class Message
	super Comparable
	redef type OTHER: Message

	# The origin of the message in the source code, if any.
	var location: nullable Location

	# The category of the message.
	#
	# Used by quality-control tool for statistics or to enable/disable things individually.
	var tag: nullable String

	# The human-readable description of the message.
	#
	# eg. "Error: cannot find method `foo`."
	#
	# A good message should:
	#
	# * start with a message type like "Error:", "Syntax Error:", "Warning:".
	#   The type is capitalized and terminated by a column.
	#   The rest on the message starts with a lowercase letter and is terminated with a dot.
	#
	# * be short and fit on a single line.
	#
	# * have meaningful information first.
	#   This helps the reader and remain usable
	#   when truncated, by an IDE for instance.
	#
	# * enclose identifiers, keywords and pieces of code with back-quotes.
	var text: String

	# The severity level
	#
	# * 0 is advices (see `ToolContext::advice`)
	# * 1 is warnings (see `ToolContext::warning`)
	# * 2 is errors (see `ToolContext::error`)
	var level: Int

	# Comparisons are made on message locations.
	redef fun <(other: OTHER): Bool do
		if location == null then return true
		if other.location == null then return false

		return location.as(not null) < other.location.as(not null)
	end

	redef fun ==(other): Bool do
		if not other isa Message then return false
		return location == other.location and tag == other.tag and text == other.text
	end

	redef fun to_s: String
	do
		var l = location
		if l == null then
			return text
		else
			return "{l}: {text}"
		end
	end

	# A colored version of the message including the original source line
	fun to_color_string: String
	do
		var esc = 27.code_point
		#var red = "{esc}[0;31m"
		#var bred = "{esc}[1;31m"
		#var green = "{esc}[0;32m"
		var yellow = "{esc}[0;33m"
		var def = "{esc}[0m"

		var tag = tag
		if tag != null then
			tag = " ({tag})"
		else
			tag = ""
		end
		var l = location
		if l == null then
			return "{text}{tag}"
		else if l.file == null then
			return "{yellow}{l}{def}: {text}{tag}"
		else
			return "{yellow}{l}{def}: {text}{tag}\n{l.colored_line("1;31")}"
		end
	end
end
src/toolcontext.nit:29,1--116,3