Markdown templates for Nit model MEntities.

This module introduces Markdown rendering services in model entities. With the no_color option set to false, the output can be highlighted for console.

Redefined classes

redef class MAttributeDef

nitc :: term_model $ MAttributeDef

A local definition of an attribute
redef class MClass

nitc :: term_model $ MClass

A named class
redef class MClassDef

nitc :: term_model $ MClassDef

A definition (an introduction or a refinement) of a class in a module
redef class MClassType

nitc :: term_model $ MClassType

A type based on a class.
redef class MDoc

nitc :: term_model $ MDoc

Structured documentation of a MEntity object
redef abstract class MEntity

nitc :: term_model $ MEntity

A named and possibly documented entity in the model.
redef class MGenericType

nitc :: term_model $ MGenericType

A type based on a generic class.
redef class MMethodDef

nitc :: term_model $ MMethodDef

A local definition of a method
redef class MNullableType

nitc :: term_model $ MNullableType

A type prefixed with "nullable"
redef class MParameter

nitc :: term_model $ MParameter

A parameter in a signature
redef class MParameterType

nitc :: term_model $ MParameterType

The type associated to a formal parameter generic type of a class
redef abstract class MPropDef

nitc :: term_model $ MPropDef

A definition of a property (local property)
redef abstract class MProperty

nitc :: term_model $ MProperty

A service (global property) that generalize method, attribute, etc.
redef class MSignature

nitc :: term_model $ MSignature

A signature of a method
redef class MVirtualType

nitc :: term_model $ MVirtualType

A virtual formal type.
redef class MVirtualTypeDef

nitc :: term_model $ MVirtualTypeDef

A local definition of a virtual type
redef class MVisibility

nitc :: term_model $ MVisibility

A visibility (for modules, class and properties)

All class definitions

redef class MAttributeDef

nitc :: term_model $ MAttributeDef

A local definition of an attribute
redef class MClass

nitc :: term_model $ MClass

A named class
redef class MClassDef

nitc :: term_model $ MClassDef

A definition (an introduction or a refinement) of a class in a module
redef class MClassType

nitc :: term_model $ MClassType

A type based on a class.
redef class MDoc

nitc :: term_model $ MDoc

Structured documentation of a MEntity object
redef abstract class MEntity

nitc :: term_model $ MEntity

A named and possibly documented entity in the model.
redef class MGenericType

nitc :: term_model $ MGenericType

A type based on a generic class.
redef class MMethodDef

nitc :: term_model $ MMethodDef

A local definition of a method
redef class MNullableType

nitc :: term_model $ MNullableType

A type prefixed with "nullable"
redef class MParameter

nitc :: term_model $ MParameter

A parameter in a signature
redef class MParameterType

nitc :: term_model $ MParameterType

The type associated to a formal parameter generic type of a class
redef abstract class MPropDef

nitc :: term_model $ MPropDef

A definition of a property (local property)
redef abstract class MProperty

nitc :: term_model $ MProperty

A service (global property) that generalize method, attribute, etc.
redef class MSignature

nitc :: term_model $ MSignature

A signature of a method
redef class MVirtualType

nitc :: term_model $ MVirtualType

A virtual formal type.
redef class MVirtualTypeDef

nitc :: term_model $ MVirtualTypeDef

A local definition of a virtual type
redef class MVisibility

nitc :: term_model $ MVisibility

A visibility (for modules, class and properties)
package_diagram nitc::term_model term_model nitc::model_collect model_collect nitc::term_model->nitc::model_collect nitc::model_filters model_filters nitc::model_collect->nitc::model_filters ...nitc::model_filters ... ...nitc::model_filters->nitc::model_filters nitc::term term nitc::term->nitc::term_model nitc::nitx nitx nitc::nitx->nitc::term nitc::nitx... ... nitc::nitx...->nitc::nitx

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module model_examples

nitc :: model_examples

Examples for Model entities
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

module model_collect

nitc :: model_collect

Collect things from the model.

Children

module term

nitc :: term

Descendants

module a_star-m

a_star-m

module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
# Markdown templates for Nit model MEntities.
#
# This module introduces Markdown rendering services in model entities.
# With the `no_color` option set to `false`, the output can be highlighted for console.
module term_model

import model_collect
import console

redef class MDoc
	# Returns the full comment formatted for the console
	fun cs_comment(no_color: nullable Bool, indent: nullable Int): String do
		var res = new FlatBuffer
		indent = indent or else 0
		for line in content do
			res.append "{" " * indent}{line}\n"
		end
		if no_color == null or not no_color then
			return res.write_to_string.green
		end
		return res.write_to_string
	end

	# Returns the synopsis formatted for the console
	fun cs_short_comment(no_color: nullable Bool): String do
		if no_color == null or not no_color then
			return content.first.green
		end
		return content.first
	end
end

redef class MEntity

	# Text icon to be displayed in front of the entity
	fun cs_icon(no_color: nullable Bool): String do
		return public_visibility.cs_icon(no_color)
	end

	# Source code location of this MEntity formatted for console
	fun cs_location(no_color: nullable Bool): String do
		if no_color == null or not no_color then
			return location.to_s.bold.gray
		end
		return location.to_s
	end

	# Returns `self.full_name` formatted for console
	fun cs_full_name(no_color: nullable Bool): String do
		if no_color == null or not no_color then
			return full_name.bold.blue
		end
		return full_name
	end

	# Returns `self` signature formatted for console.
	fun cs_signature(no_color: nullable Bool): String do return ""

	# Returns the comment of this MEntity formatted for console.
	fun cs_comment(no_color: nullable Bool): nullable String do
		var mdoc = mdoc_or_fallback
		if mdoc == null then return null
		# FIXME add markdown for console
		return mdoc.cs_comment(no_color)
	end

	# Returns the comment of this MEntity formatted for console.
	fun cs_short_comment(no_color: nullable Bool): nullable String do
		var mdoc = mdoc_or_fallback
		if mdoc == null then return null
		return mdoc.cs_short_comment(no_color)
	end

	# Returns the complete MEntity declaration (modifiers + name + signature).
	#
	# * MPackage: `package foo`
	# * MGroup: `group foo`
	# * MModule: `module foo`
	# * MClass: `private abstract class Foo[E: Object]`
	# * MClassDef: `redef class Foo[E]`
	# * MProperty: `private fun foo(e: Object): Int`
	# * MPropdef: `redef fun foo(e)`
	fun cs_declaration(no_color: nullable Bool): String do
		var tpl = new FlatBuffer
		tpl.append collect_modifiers.join(" ")
		tpl.append " "
		tpl.append name
		tpl.append cs_signature(no_color)
		if no_color == null or not no_color then
			return tpl.write_to_string.bold
		end
		return tpl.write_to_string
	end

	# Returns `self` as a list element that can be displayed in console.
	#
	# Displays `cs_icon`, `cs_name`, `cs_short_comment, `cs_namespace`,
	# `cs_declaration` and `cs_location`.
	fun cs_list_item(no_color: nullable Bool): String do
		var tpl = new FlatBuffer
		tpl.append " "
		tpl.append cs_icon(no_color)
		tpl.append " "
		tpl.append cs_full_name(no_color)
		var comment = cs_short_comment(no_color)
		if comment != null then
			tpl.append "\n   "
			if no_color == null or not no_color then
				tpl.append "# ".green
			else
				tpl.append "# "
			end
			tpl.append comment
		end
		tpl.append "\n   "
		tpl.append cs_declaration(no_color)
		tpl.append "\n   "
		tpl.append cs_location(no_color)
		return tpl.write_to_string
	end

	# Source code associated to this MEntity.
	#
	# Uses `cs_location` to locate the source code.
	fun cs_source_code: String do
		var tpl = new Buffer
		tpl.append "\n~~~\n"
		tpl.append location.text
		tpl.append "\n~~~\n"
		return tpl.write_to_string
	end
end

redef class MClass
	redef fun cs_icon(no_color) do return intro.cs_icon(no_color)
	redef fun cs_signature(no_color) do return intro.cs_signature(no_color)
end

redef class MClassDef
	redef fun cs_icon(no_color) do
		if is_intro then return visibility.cs_icon(no_color)
		return visibility.cs_visibility_color("*")
	end

	# Returns the MClassDef generic signature with static bounds.
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		var mparameters = mclass.mparameters
		if not mparameters.is_empty then
			tpl.append "["
			for i in [0..mparameters.length[ do
				tpl.append "{mparameters[i].name}: "
				tpl.append bound_mtype.arguments[i].cs_signature(no_color)
				if i < mparameters.length - 1 then tpl.append ", "
			end
			tpl.append "]"
		end
		return tpl.write_to_string
	end
end

redef class MProperty
	redef fun cs_icon(no_color) do return intro.cs_icon(no_color)
	redef fun cs_signature(no_color) do return intro.cs_signature(no_color)
end

redef class MPropDef
	redef fun cs_icon(no_color) do
		if is_intro then return visibility.cs_icon(no_color)
		return visibility.cs_visibility_color("*", no_color)
	end
end

redef class MAttributeDef
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		var static_mtype = self.static_mtype
		if static_mtype != null then
			tpl.append ": "
			tpl.append static_mtype.cs_signature(no_color)
		end
		return tpl.write_to_string
	end
end

redef class MMethodDef
	redef fun cs_signature(no_color) do
		return msignature.as(not null).cs_signature(no_color)
	end
end

redef class MVirtualTypeDef
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		var bound = self.bound
		if bound == null then return tpl.write_to_string
		tpl.append ": "
		tpl.append bound.cs_signature(no_color)
		return tpl.write_to_string
	end
end

redef class MClassType
	redef fun cs_signature(no_color) do return name
end

redef class MNullableType
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		tpl.append "nullable "
		tpl.append mtype.cs_signature(no_color)
		return tpl.write_to_string
	end
end

redef class MGenericType
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		tpl.append mclass.name
		tpl.append "["
		for i in [0..arguments.length[ do
			tpl.append arguments[i].cs_signature(no_color)
			if i < arguments.length - 1 then tpl.append ", "
		end
		tpl.append "]"
		return tpl.write_to_string
	end
end

redef class MParameterType
	redef fun cs_signature(no_color) do return name
end

redef class MVirtualType
	redef fun cs_signature(no_color) do return name
end

redef class MSignature
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		if not mparameters.is_empty then
			tpl.append "("
			for i in [0..mparameters.length[ do
				tpl.append mparameters[i].cs_signature(no_color)
				if i < mparameters.length - 1 then tpl.append ", "
			end
			tpl.append ")"
		end
		var return_mtype = self.return_mtype
		if return_mtype != null then
			tpl.append ": "
			tpl.append return_mtype.cs_signature(no_color)
		end
		return tpl.write_to_string
	end
end

redef class MParameter
	redef fun cs_signature(no_color) do
		var tpl = new FlatBuffer
		tpl.append "{name}: "
		tpl.append mtype.cs_signature(no_color)
		if is_vararg then tpl.append "..."
		return tpl.write_to_string
	end
end

redef class MVisibility
	# Visibility icon
	fun cs_icon(no_color: nullable Bool): String do
		var icon
		if self == private_visibility then
			icon = cs_visibility_color("-", no_color)
		else if self == protected_visibility then
			icon = cs_visibility_color("#", no_color)
		else
			icon = cs_visibility_color("+", no_color)
		end
		if no_color != null and no_color then return icon
		return icon.bold
	end

	# Colorize `string` depending on the visibility
	fun cs_visibility_color(string: String, no_color: nullable Bool): String do
		if no_color != null and no_color then return string
		if self == private_visibility then
			return string.red
		else if self == protected_visibility then
			return string.yellow
		else
			return string.green
		end
	end
end
src/doc/templates/term_model.nit:15,1--308,3