Documentation commands

A DocCommand returns data about a model, an entity or a piece of documentation.

Each command assumes a different goal like getting the comment of an entity, getting a list of packages, getting an UML class diagram etc.

Commands are used by documentation tools to build up documentation ressources like Nitweb, Nitx, Nitdoc or documentation cards within READMEs.

Introduced classes

abstract class CmdEntities

nitc :: CmdEntities

A list of mentities
class CmdEntity

nitc :: CmdEntity

A command about a MEntity
abstract class CmdEntityList

nitc :: CmdEntityList

A command about a MEntity that returns a list of mentities
abstract class CmdError

nitc :: CmdError

Command Error
abstract class CmdList

nitc :: CmdList

A command that returns a list of results
abstract class CmdMessage

nitc :: CmdMessage

Command message
class CmdSuccess

nitc :: CmdSuccess

Command Success
abstract class CmdWarning

nitc :: CmdWarning

Command Warning
abstract class DocCommand

nitc :: DocCommand

Documentation command
class ErrorMEntityConflict

nitc :: ErrorMEntityConflict

Multiple MEntities matching mentity_name
class ErrorMEntityNoName

nitc :: ErrorMEntityNoName

No MEntity name provided
class ErrorMEntityNotFound

nitc :: ErrorMEntityNotFound

No MEntity matching mentity_name

All class definitions

abstract class CmdEntities

nitc $ CmdEntities

A list of mentities
class CmdEntity

nitc $ CmdEntity

A command about a MEntity
abstract class CmdEntityList

nitc $ CmdEntityList

A command about a MEntity that returns a list of mentities
abstract class CmdError

nitc $ CmdError

Command Error
abstract class CmdList

nitc $ CmdList

A command that returns a list of results
abstract class CmdMessage

nitc $ CmdMessage

Command message
class CmdSuccess

nitc $ CmdSuccess

Command Success
abstract class CmdWarning

nitc $ CmdWarning

Command Warning
abstract class DocCommand

nitc $ DocCommand

Documentation command
class ErrorMEntityConflict

nitc $ ErrorMEntityConflict

Multiple MEntities matching mentity_name
class ErrorMEntityNoName

nitc $ ErrorMEntityNoName

No MEntity name provided
class ErrorMEntityNotFound

nitc $ ErrorMEntityNotFound

No MEntity matching mentity_name
package_diagram nitc::commands_base commands_base nitc::model_index model_index nitc::commands_base->nitc::model_index nitc::model_collect model_collect nitc::model_index->nitc::model_collect trees::trie trie nitc::model_index->trees::trie trees::bktree bktree nitc::model_index->trees::bktree ...nitc::model_collect ... ...nitc::model_collect->nitc::model_collect ...trees::trie ... ...trees::trie->trees::trie ...trees::bktree ... ...trees::bktree->trees::bktree nitc::commands_model commands_model nitc::commands_model->nitc::commands_base nitc::commands_catalog commands_catalog nitc::commands_catalog->nitc::commands_model nitc::commands_graph commands_graph nitc::commands_graph->nitc::commands_model nitc::commands_ini commands_ini nitc::commands_ini->nitc::commands_model nitc::commands_main commands_main nitc::commands_main->nitc::commands_model nitc::commands_usage commands_usage nitc::commands_usage->nitc::commands_model nitc::commands_catalog... ... nitc::commands_catalog...->nitc::commands_catalog nitc::commands_graph... ... nitc::commands_graph...->nitc::commands_graph nitc::commands_ini... ... nitc::commands_ini...->nitc::commands_ini nitc::commands_main... ... nitc::commands_main...->nitc::commands_main nitc::commands_usage... ... nitc::commands_usage...->nitc::commands_usage

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 abstract_tree

trees :: abstract_tree

Introduce tree structures abstraction
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module bintree

trees :: bintree

Binary Tree data-structure
module bitset

core :: bitset

Services to handle BitSet
module bktree

trees :: bktree

Implementation of BKTree
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_collect

nitc :: model_collect

Collect things from the model.
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 rbtree

trees :: rbtree

A red–black tree is a data structure which is a type of self-balancing binary search tree.
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 trees

trees :: trees

General module for tree data structures
module trie

trees :: trie

A trie (or prefix tree) is a datastructure used to perform prefix searches.
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_index

nitc :: model_index

Search things from the Model

Children

module commands_model

nitc :: commands_model

Doc commands about a Model or a MEntity

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module api_auth

nitc :: api_auth

module api_base

nitc :: api_base

Base classes used by nitweb.
module api_docdown

nitc :: api_docdown

Nitdoc specific Markdown format handling for Nitweb
module api_feedback

nitc :: api_feedback

Feedback related features
module api_light

nitc :: api_light

Highlight and collect messages from a piece of code
module api_model

nitc :: api_model

module commands_catalog

nitc :: commands_catalog

Commands to retrieve Catalog related data
module commands_docdown

nitc :: commands_docdown

Doc down related queries
module commands_graph

nitc :: commands_graph

Graph commands
module commands_http

nitc :: commands_http

Initialize commands from HTTP requests
module commands_parser

nitc :: commands_parser

A parser that create DocCommand from a string
module commands_usage

nitc :: commands_usage

Commands about how mentities are used
module html_commands

nitc :: html_commands

Render commands results as HTML
module json_commands

nitc :: json_commands

Translate command results to json
module md_commands

nitc :: md_commands

Render commands results as Markdown
module nitdoc

nitc :: nitdoc

Generator of static API documentation for the Nit language
module nitpackage

nitc :: nitpackage

Helpful features about packages
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
module static

nitc :: static

Nitdoc generation framework
module static_base

nitc :: static_base

Base entities shared by all the nitdoc code
module static_cards

nitc :: static_cards

Cards templates for the static documentation
module static_html

nitc :: static_html

Render documentation pages as HTML
module static_index

nitc :: static_index

Manage indexing of Nit model for Nitdoc QuickSearch.
module static_structure

nitc :: static_structure

Composes the pages of the static documentation
module term

nitc :: term

# Documentation commands
#
# A DocCommand returns data about a model, an entity or a piece of documentation.
#
# Each command assumes a different goal like getting the comment of an entity,
# getting a list of packages, getting an UML class diagram etc.
#
# Commands are used by documentation tools to build up documentation ressources
# like Nitweb, Nitx, Nitdoc or documentation cards within READMEs.
module commands_base

import model::model_index

# Documentation command
#
# An abstract command that works on a Model.
#
# Since they are used by a wide variety of clients, initialization of DocCommands
# works in two steps.
#
# First, you pass the data you already have to the command at init:
# ~~~nitish
# var c1 = new CmdEntity(view, mentity_name = "Array")
# var c2 = new CmdEntity(view, mentity = my_entity)
# ~~~
#
# Then, you call `init_command` to initialize the missing field from the stub data:
# ~~~nitish
# var r1 = c1.init_command
# assert c1.mentity != null
# assert r1 isa CmdSuccess
#
# var r2 = c2.init_command
# assert c2.mentity_name != null
# assert r2 isa CmdSuccess
# ~~~
#
# See `init_command` for more details about the returned statuses.
abstract class DocCommand

	# Model to retrieve data for
	var model: Model

	# ModelFilter to apply if any
	var filter: nullable ModelFilter

	# Initialize the command
	#
	# Returns a command message that gives the status of the command initialization.
	#
	# There is 3 categories of messages:
	# * `CmdSuccess`: when the command that initialized correctly;
	# * `CmdError`: when the command cannot be initialized;
	# * `CmdWarning`: when something is wrong with the command but a result still can be produced.
	#
	# Warnings are generally used to distinguish empty list or mdoc from no data at all.
	fun init_command: CmdMessage do return new CmdSuccess

	# Return a new filter for that command execution.
	fun cmd_filter: ModelFilter do
		var filter = self.filter
		if filter == null then return new ModelFilter
		return new ModelFilter.from(filter)
	end
end

# Command message
#
# A message returned by a command.
# Messages are used to inform the client of the command initialization status and results.
# Mostly, messages are used to check if a command is in an error state.
abstract class CmdMessage
end

# Command Success
#
# Returned when the command was performed without any error or warning.
class CmdSuccess
	super CmdMessage
end

# Command Error
#
# Command errors are returned when the command cannot provide results because
# of a problem on the user-end (i.e. Bad command name, MEntity not found etc.).
abstract class CmdError
	super CmdMessage
end

# Command Warning
#
# Command warnings are returned when the command cannot provide results because
# of a problem on the model-end (i.e. No documentation for a MEntity, no code etc.)
abstract class CmdWarning
	super CmdMessage
end

# Basic commands

# A command about a MEntity
class CmdEntity
	super DocCommand

	# MEntity this command is about
	#
	# Alternatively you can provide a `mentity_name`.
	var mentity: nullable MEntity = null is optional, writable

	# Name of the mentity this command is about
	#
	# Alternatively you can directly provide the `mentity`.
	var mentity_name: nullable String = null is optional, writable

	# Initialize the command mentity.
	#
	# If not already set, tries to find the `mentity` from the `mentity_name`.
	#
	# This function try to match `mentity_name` both as a `full_name` and
	# `name`.
	#
	# Return states:
	# * `CmdSuccess`: everything was ok;
	# * `ErrorMEntityNoName`: no `mentity` and no `mentity_name` provided;
	# * `ErrorMEntityNotFound`: no mentity for `mentity_name`;
	# * `ErrorMEntityConflict`: `mentity_name` was a non-qualified name that
	#   returns more than one MEntity.
	fun init_mentity: CmdMessage do
		if mentity != null then
			if mentity_name == null then mentity_name = mentity.as(not null).full_name
			return new CmdSuccess
		end

		var mentity_name = self.mentity_name
		if mentity_name == null or mentity_name.is_empty then return new ErrorMEntityNoName

		mentity = model.mentity_by_full_name(mentity_name)
		if mentity == null then
			var mentities = model.mentities_by_name(mentity_name)
			if mentities.is_empty then
				var suggest = model.find(mentity_name, 3)
				return new ErrorMEntityNotFound(mentity_name, suggest)
			else if mentities.length > 1 then
				return new ErrorMEntityConflict(mentity_name, mentities)
			end
			mentity = mentities.first
		end
		return new CmdSuccess
	end

	# See `init_mentity`.
	redef fun init_command do return init_mentity
end

# No MEntity name provided
class ErrorMEntityNoName
	super CmdError
	redef fun to_s do return "No entity name provided"
end

# No MEntity matching `mentity_name`
class ErrorMEntityNotFound
	super CmdError

	# MEntity name provided
	var mentity_name: String

	# Suggestions matching the `mentity_name`.
	var suggestions: Array[MEntity]

	redef fun to_s do
		var res = new Buffer
		res.append "No entity for `{mentity_name}`.\n"
		res.append "Did you mean: "
		for mentity in suggestions do
			res.append " `{mentity.full_name}`"
			if mentity != suggestions.last then res.append ","
		end
		return res.write_to_string
	end
end

# Multiple MEntities matching `mentity_name`
class ErrorMEntityConflict
	super CmdError

	# MEntity name provided
	var mentity_name: String

	# Conflicts for `mentity_name`
	var conflicts: Array[MEntity]

	redef fun to_s do
		var res = new Buffer
		res.append "Multiple entities for `{mentity_name}`:"
		for mentity in conflicts do
			res.append " `{mentity.full_name}`"
			if mentity != conflicts.last then res.append ","
		end
		return res.write_to_string
	end
end

# A command that returns a list of results
abstract class CmdList
	super DocCommand

	# Type of result
	type ITEM: Object

	# Limit the items in the list
	var limit: nullable Int = null is optional, writable

	# Page to display
	var page: nullable Int = null is optional, writable

	# Total number of ret
	var count: nullable Int = null is optional, writable

	# Total number of pages
	var max: nullable Int = null is optional, writable

	# Comparator used to sort the list
	var sorter: nullable Comparator = null is writable

	# Items in the list
	var results: nullable Array[ITEM] = null is writable

	# `init_command` is used to factorize the sorting and pagination of results
	#
	# See `init_results` for the result list initialization.
	redef fun init_command do
		var res = super
		if not res isa CmdSuccess then return res
		res = init_results
		if not res isa CmdSuccess then return res
		sort
		paginate
		return res
	end

	# Initialize the `results` list
	#
	# This method must be redefined by CmdList subclasses.
	fun init_results: CmdMessage do return new CmdSuccess

	# Sort `mentities` with `sorter`
	fun sort do
		var results = self.results
		if results == null then return
		var sorter = self.sorter
		if sorter == null then return
		sorter.sort(results)
	end

	# Paginate the results
	#
	# This methods keeps only a subset of `results` depending on the current `page` and the
	# number of elements to return set by `limit`.
	#
	# The `count` can be specified when `results` does not contain all the results.
	# For example when the results are already limited from a DB statement.
	fun paginate do
		var results = self.results
		if results == null then return

		var limit = self.limit
		if limit == null then return

		var page = self.page
		if page == null or page <= 0 then page = 1

		var count = self.count
		if count == null then count = results.length

		var max = count / limit
		if max == 0 then
			page = 1
			max = 1
		else if page > max then
			page = max
		end

		var lstart = (page - 1) * limit
		var lend = limit
		if lstart + lend > count then lend = count - lstart
		self.results = results.subarray(lstart, lend)
		self.max = max
		self.limit = limit
		self.page = page
		self.count = count
	end
end

# A list of mentities
abstract class CmdEntities
	super CmdList

	redef type ITEM: MEntity

	redef var sorter = new MEntityNameSorter
end

# A command about a MEntity that returns a list of mentities
abstract class CmdEntityList
	super CmdEntity
	super CmdEntities

	autoinit(model, filter, mentity, mentity_name, limit, page, count, max)

	redef fun init_command do
		var res = init_mentity
		if not res isa CmdSuccess then return res
		res = init_results
		if not res isa CmdSuccess then return res
		sort
		paginate
		return res
	end
end
src/doc/commands/commands_base.nit:15,1--333,3