The indent module

This module provides Indenter to indent a Document.

To use the Indenter: instantiate one and call its indent() method with a Cursor describing the text range to indent. For example:

>>> from parce import Document, Cursor
>>> from parce.indent import Indenter
>>> from parce.lang.css import Css
>>> i = Indenter()
>>> i.indent_string = "    " # use four spaces by default
>>> d = Document(Css.root, "h1 {\ncolor: red;\n     }\n")
>>> c = Cursor(d, 0, None)  # select all
>>> i.indent(c)
>>> d.text()
'h1 {\n    color: red;\n}\n'

Indenter uses per-language Indent classes which define the indenting behaviour. You can add them manually to Indenter, but it can also find Indent classes automatically by looking in the language’s module and finding there an Indent subclass with the same name, with “Indent” appended.

To further adapt the indenting behaviour, you can implement the indent_events() method of the Indenter. Or the Indent.indent_events() method of the language-specific indenter.

The following events can be yielded (simply module constants):


this is a blank line, the current indent level is not changed.


the current indent string this line has.


next line should be indented a level.


the indent of this line may not be changed at all, e.g. because it is part of a multiline string.


next line should be dedented a level. (If this event occurs before INDENT or NO_DEDENT, the current line can be dedented.)


further DEDENT events will not dedent the current line anymore, but rather affect the indentation of the next line.


use this indent for the current line, but do not change the indent level or the current indent for the next line.


trailing whitespace should not be stripped off this line.

ALIGN, string:

The last INDENT event should use the specified string for alignment instead of the default indent (relatively to the start of the text in the current line excluding the current indent).

class IndentInfo(block)[source]

Bases: object

Contains information about how to indent a block.

Created by AbstractIndenter.indent_info() and used within AbstractIndenter.indent().


the Block


the indent events (None or string)


the number of dedents at the start of the line


the number of dedents later in the line


the current indent


a preferred, special case indent

property allow_indent

Whether the indent of this line may be changed.

property allow_strip

Whether trailing whitespace may be stripped of this line.

property is_blank
class AbstractIndenter[source]

Bases: object

Indents (part of) a Document.

The indenting preferences can be set using some instance attributes.

indent_string = '  '

the string to indent each level with, defaulting to two spaces.

indent_blank_lines = True

whether to also indent blank lines


Indent all the lines in the cursor’s range.

This method scans the document always from the beginning, although it doesn’t change lines before the start of the cursor’s range. To re-indent a full document, select all text in the cursor (i.e. pos is 0, end is None).


Adjust the indent of the single block at the Cursor’s pos.


Increase the indent in the Cursor’s lines.


Decrease the indent in the Cursor’s lines.

strip_trailing_blanks(cursor, chars=None)[source]

Strip trailing blanks off the selected lines.

Lines that don’t allow changing the indent are skipped. The chars argument is passed on to the Python strip() method.

indent_info(block, prev_indents=())[source]

Return an IndentInfo object for the specified block.

indent_events(block, prev_indents=())[source]

Implement this method to yield indenting events for the block.

class Indenter[source]

Bases: parce.indent.AbstractIndenter

Indenter that uses Language-specific indenters if available.

This can only be used on documents that have TreeDocument mixed in, i.e. have a tree available.

indent_events(block, prev_indents=())[source]

Reimplemented to use Indent subclasses for the specified language.


Return a Indent class instance for the specified language.

add_indent(language, indent)[source]

Add a Indent instance for the specified language.


If no Indent was added, try to find a predefined one.

This is done by looking for a Indent subclass in the language’s module, with the same name as the language with “Indent” appended. So for a language class named “Css”, this method tries to find a Indent in the same module with the name “CssIndent”.

If no Indent is found, for the language, the language’s base classes are also tried.

class Indent[source]

Bases: object

The base class for language-specific indenters.

indent_events(block, tokens, is_first, prev_indents)[source]

Implement this to yield indent events for the tokens.