Redefined classes

redef class Sys

nitc :: nitpretty $ Sys

The main class of the program.
redef class ToolContext

nitc :: nitpretty $ ToolContext

Global context for tools

All class definitions

redef class Sys

nitc :: nitpretty $ Sys

The main class of the program.
redef class ToolContext

nitc :: nitpretty $ ToolContext

Global context for tools
package_diagram nitc::nitpretty nitpretty nitc::pretty pretty nitc::nitpretty->nitc::pretty nitc::modelbuilder modelbuilder nitc::pretty->nitc::modelbuilder nitc::astutil astutil nitc::pretty->nitc::astutil ...nitc::modelbuilder ... ...nitc::modelbuilder->nitc::modelbuilder ...nitc::astutil ... ...nitc::astutil->nitc::astutil a_star-m a_star-m a_star-m->nitc::nitpretty

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 array

core :: array

This module introduces the standard array structure.
module astutil

nitc :: astutil

Additional features on Nit AST
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 html

html :: html

HTML output facilities
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 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 model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
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 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 pretty

nitc :: pretty

Library used to pretty print Nit code.

Children

module a_star-m

a_star-m

import pretty

redef class ToolContext
	# The working directory used to store temp files.
	var opt_dir = new OptionString("Working directory (default is '.nitpretty')", "--dir")

	# Output pretty printed code with this filename.
	var opt_output = new OptionString("Output name (default is pretty.nit)", "-o",
	   "--output")

	# Show diff between source and pretty printed code.
	var opt_diff = new OptionBool("Show diff between source and output", "--diff")

	# Show diff between source and pretty printed code using meld.
	var opt_meld = new OptionBool("Show diff between source and output using meld",
	   "--meld")

	# --line-width
	var opt_line_width = new OptionInt("Maximum length of lines (use 0 to disable automatic line breaks)", 80, "--line-width")

	# --no-inline
	var opt_no_inline = new OptionBool("Disable automatic one-liners", "--no-inline")

	# Break too long string literals.
	var opt_break_str = new OptionBool("Break too long string literals", "--break-strings")

	# Force `do` on the same line as the method signature.
	var opt_inline_do = new OptionBool("Force do keyword on the same line as the method signature",
		"--inline-do")

	# Force formatting on empty lines.
	#
	# By default empty lines are kept as they were typed in the file.
	# When enabling this option, `nitpretty` will decide where to break lines
	# and will put empty lines to separate properties and code blocks.
	var opt_skip_empty = new OptionBool("Force formatting of empty lines", "--skip-empty")

	# Check formatting instead of pretty printing.
	#
	# This option creates a temporary pretty printed file then checks if the
	# output of the diff command on the source file and the pretty printed one is
	# empty.
	var opt_check = new OptionBool("Check format of Nit source files", "--check")
end

# Return result from diff between `file1` and `file2`.
private fun diff(file1, file2: String): String do
	var p = new ProcessReader("diff", "-u", file1, file2)
	var res = p.read_all
	p.wait
	p.close
	return res
end

# process options
var toolcontext = new ToolContext

var opts = toolcontext.option_context
opts.add_option(toolcontext.opt_dir, toolcontext.opt_output)
opts.add_option(toolcontext.opt_diff, toolcontext.opt_meld, toolcontext.opt_check)
opts.add_option(toolcontext.opt_line_width, toolcontext.opt_break_str, toolcontext.opt_inline_do)
opts.add_option(toolcontext.opt_no_inline)
opts.add_option(toolcontext.opt_skip_empty)

toolcontext.tooldescription = "Usage: nitpretty [OPTION]... <file.nit>\n" +
	"Pretty print Nit code from Nit source files."

toolcontext.process_options args
var arguments = toolcontext.option_context.rest
# build model
var model = new Model
var mbuilder = new ModelBuilder(model, toolcontext)
var mmodules = mbuilder.parse_full(arguments)
mbuilder.run_phases

if mmodules.is_empty then
	print "Error: no module to pretty print"
	return
end

if not toolcontext.opt_check.value and mmodules.length > 1 then
	print "Error: only --check option allow multiple modules"
	return
end

var dir = toolcontext.opt_dir.value or else ".nitpretty"
if not dir.file_exists then dir.mkdir
var v = new PrettyPrinterVisitor

v.max_size = toolcontext.opt_line_width.value
if toolcontext.opt_break_str.value then
	v.break_strings = true
end
if toolcontext.opt_inline_do.value then
	v.inline_do = true
end
if toolcontext.opt_skip_empty.value then
	v.skip_empty = true
end
if toolcontext.opt_no_inline.value then
	v.no_inline = true
end

for mmodule in mmodules do
	var nmodule = mbuilder.mmodule2node(mmodule)
	if nmodule == null then
		print " Error: no source file for module {mmodule}"
		return
	end
	var file = "{dir}/{mmodule.name}.nit"
	var tpl = v.pretty_nmodule(nmodule)
	tpl.write_to_file file

	if toolcontext.opt_check.value then
		var res = diff(nmodule.location.file.filename, file)

		if not res.is_empty then
			print "Wrong formating for module {nmodule.location.file.filename}"
			toolcontext.info(res, 1)

			if toolcontext.opt_meld.value then
				sys.system "meld {nmodule.location.file.filename} {file}"
			end
		else
			toolcontext.info("[OK] {nmodule.location.file.filename}", 1)
		end
	else
		# write to file
		var out = toolcontext.opt_output.value
		if out != null then sys.system "cp {file} {out}"

		# open in meld
		if toolcontext.opt_meld.value then
			sys.system "meld {arguments.first} {file}"
			return
		end

		# show diff
		if toolcontext.opt_diff.value then
			var res = diff(arguments.first, file)
			if not res.is_empty then print res
			return
		end

		# show pretty
		if not toolcontext.opt_quiet.value then tpl.write_to sys.stdout
	end
end
src/nitpretty.nit:18,1--165,3