Home Reference Source
import ScopeTree from 'auto-format/src/core/scopeTree.js'
public class | source

ScopeTree

A scope tree is a tree that captures the scope of a piece of code. A node contains the start and end of the scope. If start or end is null then the start or end is not in the code. A parent node contains all scopes inside it as children nodes.

Constructor Summary

Public Constructor
public

constructor(parent: *, start: *)

Creates a new node.

Member Summary

Private Members
private

children: *[]

private

end: *

private

parent: *

private

start: *

Method Summary

Public Methods
public

addChild(child: *)

Add a child to this node.

public

Balance the tree.

public

build(lines: *, index: *, scopeEnterFunc: *, scopeExitFunc: *): ScopeTree

Builds the scope tree.

public

close(line: *)

Close this scope.

public

Get an array of all children of this node.

public

Get the end of the scope.

public

Get the parent of this node.

public

Get the start of the scope.

public

Returns true if the scope of this node is closed (meaning the scope has a start and an end point)

Public Constructors

public constructor(parent: *, start: *) source

Creates a new node.

Params:

NameTypeAttributeDescription
parent *

The parent node.

start *

The start of the scope of this node.

Private Members

private children: *[] source

private end: * source

private parent: * source

private start: * source

Public Methods

public addChild(child: *) source

Add a child to this node.

Params:

NameTypeAttributeDescription
child *

The child to add to this node, should also be of type ScopeTree.

public balance() source

Balance the tree. This enforces an ordering of the nodes in the tree such that parent nodes are always 1 scope above their child nodes.

By nature, the tree will be built in a balanced fashion. However because we might be dealing with snippets of code scope entries or exits (i.e. brackets) might be missing, which puts the tree out of balance. This method fixes that.

Particularly, this moves all left siblings of a node that only has an end but not a start under it, making them his children.

Example:

 <-> : scope is open and closed
 <-  : scope is only opened but not closed
  -> : scope is only closed but never opened

IMBALANCED TREE                BALANCED TREE
 -root-                         -root-
   |                              |
   |---------------               |-----
   |    |    |    |               |    |
   |    |    |    |               |    |
  <->  <->   ->  <-    =======>   ->  <-
                                  |
   ^    ^    ^    ^               |-----
   |    |    |    |               |    |
  ok   ok   not  ok               |    |
            ok                   <->  <->

public build(lines: *, index: *, scopeEnterFunc: *, scopeExitFunc: *): ScopeTree source

Builds the scope tree. This function is designed in such a way that it can build scope trees over a variety of different programming languages. That is why as a parameter, the function takes two functions, each which determine where the scope starts or ends in a current line, if it does at all. These functions NEED to have the following signatures:

Index:Integer scopeEnterFunc(arrayOfAllLines:Array, lineToCheckIndex:Integer) Index:Integer scopeExitFunc(arrayOfAllLines:Array, lineToCheckIndex:Integer)

Both functions take the full array of lines and the index of the line to examine, and return the column within the line at which the scope is entered or exited. If there is no scope change in the line, null must be returned.

Params:

NameTypeAttributeDescription
lines *

Array of lines to build the scope tree over.

index *

Current line number.

scopeEnterFunc *

Function with the above signature. It takes the full array of lines and the index of the line to examine, and returns the column within the line at which the scope is entered. If no scope is entered in the line, null must be returned.

scopeExitFunc *

Function with the above signature. It takes the full array of lines and the index of the line to examine, and returns the column within the line at which the scope is exited. If no scope is exited in the line, null must be returned.

Return:

ScopeTree

The root node of the scope tree.

public close(line: *) source

Close this scope. This means the end of the scope has been reached.

Params:

NameTypeAttributeDescription
line *

The line that the scope ends at.

public getChildren(): Array source

Get an array of all children of this node.

Return:

Array

An array of all children of this node.

public getEnd(): Number source

Get the end of the scope.

Return:

Number

The end of the scope, or null if not defined.

public getParent(): ScopeTree source

Get the parent of this node. Returns null if this is the root node.

Return:

ScopeTree

The parent of this node, or null if this is the root node.

public getStart(): Number source

Get the start of the scope.

Return:

Number

The start of the scope, or null if not defined.

public isClosed(): Boolean source

Returns true if the scope of this node is closed (meaning the scope has a start and an end point)

Return:

Boolean

True if the scope for this node is defined, else false.