Home Reference Source
public class | source

JavaFormatter

Extends:

AFormatter → JavaFormatter

JavaFormatter is the auto-formatter implementation for Java.

Constructor Summary

Public Constructor
public

constructor(formatUnit: *)

Create a new JavaFormatter

Member Summary

Private Members
private

Method Summary

Public Methods
public

format(codeString: *): Array

Format a string of code.

public

formatSnippet(code: *, startRow: *, endRow: *, offset: *): Array

A slight variation of format(codeString).

Private Methods
private

_calculateScopeIndex(string: *, token: *): *

Calculate the scope index and make sure it is valid (look at _checkScopeIndex for more info)

private

Checks if a line is a method signature.

private

Checks if a line contains a special statement.

private

_checkScopeIndex(string: *, scopeIndex: *, token: *): *

Calculate the scope index and make sure it is valid, that means: 1.

private

_expressionIdentifier(codeArray: *, index: *): Boolean

Checks if a line identified by an index in an array qualifies as an expression.

private

_formatJavadoc(codeArray: *): *

Adds a space before Javadoc comments if they are a body or end comment.

private

_identifyScope(codeArray: *, index: *, token: *): Number

Helper method for _scopeEnterFunc and _scopeExitFunc.

private

_scopeEnterFunc(codeArray: *, index: *): Number

Checks if a line identified by an index in an array starts a new scope.

private

_scopeExitFunc(codeArray: *, index: *): Number

Checks if a line identified by an index in an array ends an existing scope.

Inherited Summary

From class AFormatter
public
private
private
private
private
private
private
private
private
private
private
private
private
public

format(code: *, expressionIdentifier: *, scopeEnterFunc: *, scopeExitFunc: *, formatCommentsFunc: *): Array

Format a string of code.

public

formatSnippet(code: *, startRow: *, endRow: *, offset: *, expressionIdentifier: *, scopeEnterFunc: *, scopeExitFunc: *, formatCommentsFunc: *, identifyMethodSigFunc: *, identifySpecialStatement: *, bodyCommentToken: *, simpleCommentToken: *): Array

A slight variation of format(codeString).

private

_fillBucketRange(array: *, start: *, end: *)

Adds indentations to the array from the start to the end index, inclusive.

private

_formatPrefix(scopeTree: *, array: *, oldStart: *): Array

Format the prefix of a snippet.

private

_formatSuffix(codeArray: *, oldEnd: *): Array

Format the suffix of a snippet.

private

_handleOpenComments(codeArray: *, startLine: *): Array

If an open comment is met, take all lines necessary to close it from the original file and append them to the snippet.

private

_preFormatArray(array: *, node: *)

Pre-formats an empty array by adding the indentations (the specified format unit).

private

_preFormatSnippet(snippet: *, selection: *): Array

Performs the following preliminary tasks:

  • Fills open comments
  • Removes extra method signatures (===> scope break so it will be removed later)
private

Remove method signatures in the prefix that do not belong to the selection.

private

Remove method signatures in the suffix that do not belong to the selection.

private

_splitSelection(codeArray: *, selection: *): Array

Splits the code array into three parts: prefix, selection, suffix.

private

_trimBeginning(codeArray: *): Array

Removes empty lines at beginning of snippet.

private

_trimEnd(codeArray: *): Array

Removes empty lines at end of snippet.

Public Constructors

public constructor(formatUnit: *) source

Create a new JavaFormatter

Override:

AFormatter#constructor

Params:

NameTypeAttributeDescription
formatUnit *

The token to be used for line indentations.

Private Members

private methodSigRegex: * source

Public Methods

public format(codeString: *): Array source

Format a string of code. The string will be cut into lines and lines will be indented accordingly to their scope.

Override:

AFormatter#format

Params:

NameTypeAttributeDescription
codeString *

The string of code to format.

Return:

Array

An array of formatted lines.

public formatSnippet(code: *, startRow: *, endRow: *, offset: *): Array source

A slight variation of format(codeString). Useful if you want to display a code snippet around a selection of lines.

In addition to indenting lines, formatSnippet takes a selection as a start and end row in a large slab of code and cuts out a snippet of code around this selection. The start and end of the snippet is based on an offset that is provided as a parameter. The offset with the start and end of the selection create a sort of range from which the snippet is taken.

In the example below, the selection is identified to belong to test2() and thus only test2() is returned. If the method is longer than the offset, than only the part within the offset will be returned. No code is added to the range with the exception of comment lines above the selection, to close unfinished comments.

Override:

AFormatter#formatSnippet

Params:

NameTypeAttributeDescription
code *

The original code base in which the selection is.

startRow *

The start row of the selection in the code base.

endRow *

The end row of the selection in the code base.

offset *

The offset the defines the range on which to base the snippet.

Return:

Array

An array of formatted lines that form the snippet, separated into prefix selection and suffix, as well as the start and end lines of the snippet in the original code base.

Example:

Selection start row: 11 ---- Selection end row: 11 ---- Offset: 6 ----> Snippet range: [11 - 6, 11 + 6] = [5, 17]

 START:
1.  @Test
2.  public void test1() {
3.      System.out.println("Test 1");
4.  }
5.
6.  // ------------------
7.  // Perform test 2.
8.  // ------------------
9. @Test
10. public void test2() {
11.     System.out.println("Test 2");
12. }
13.
14. @Test
15. public void test3() {
16.     System.out.println("Test 3");
17. }
18. ...

RESULT:
6.  // ------------------
7.  // Perform test 2.
8.  // ------------------
9. @Test
10. public void test2() {
11.     System.out.println("Test 1");
12. }

Private Methods

private _calculateScopeIndex(string: *, token: *): * source

Calculate the scope index and make sure it is valid (look at _checkScopeIndex for more info)

Params:

NameTypeAttributeDescription
string *

The string that maybe contains the scope enter / exit token.

token *

The scope enter / exit token to look for in the string.

Return:

*

The position in the line where the scope starts or ends or -1 if this line does neither.

private _checkForFunction(line: *): Boolean source

Checks if a line is a method signature.

Params:

NameTypeAttributeDescription
line *

The line to check for.

Return:

Boolean

True if the line is a method signature, else false.

private _checkForSpecialStatement(line: *): Boolean source

Checks if a line contains a special statement.

A special statement is defined in Java by:

  • An empty line (e.g. '')
  • A line that starts with an annotation (e.g. '@')
  • A line that starts with a comment (e.g. '//')

Params:

NameTypeAttributeDescription
line *

The line to check for a special statement.

Return:

Boolean

True if the line contains a special statement, else false.

private _checkScopeIndex(string: *, scopeIndex: *, token: *): * source

Calculate the scope index and make sure it is valid, that means:

  1. It is not inside quotation marks

    If any of the above criteria is not met, the piece of code in question is cut off from the string, and we start over with the remaining string.

Params:

NameTypeAttributeDescription
string *

The string that maybe contains the scope enter / exit token.

scopeIndex *

The current "potential" scope index

token *

The scope enter / exit token to look for in the string.

Return:

*

The position in the line where the scope starts or ends or -1 if this line does neither.

private _expressionIdentifier(codeArray: *, index: *): Boolean source

Checks if a line identified by an index in an array qualifies as an expression.

An expression is defined as:

  • A line that ends with a termination token (e.g. ';')
  • A line that defines a scope start (e.g. '{')
  • A line that defines a scope end (e.g. '}')
  • A line that starts with a special character (e.g. '@')
  • A line that starts with a comment (e.g. '//')
  • An empty line (e.g. '')

Params:

NameTypeAttributeDescription
codeArray *

An array of lines of code.

index *

The index of the relevant line in the code array.

Return:

Boolean

True if the line qualifies as an expression, else false.

private _formatJavadoc(codeArray: *): * source

Adds a space before Javadoc comments if they are a body or end comment.

Params:

NameTypeAttributeDescription
codeArray *

The already formatted code array.

Return:

*

The formatted code array with spaces for Javadoc.

private _identifyScope(codeArray: *, index: *, token: *): Number source

Helper method for _scopeEnterFunc and _scopeExitFunc.

Params:

NameTypeAttributeDescription
codeArray *

An array of lines of code.

index *

The index of the relevant line in the code array.

token *

The token to find in the line.

Return:

Number

The position in the line where the scope starts or ends or null if this line does neither.

private _scopeEnterFunc(codeArray: *, index: *): Number source

Checks if a line identified by an index in an array starts a new scope. Example: 'if (foo) {'

Params:

NameTypeAttributeDescription
codeArray *

An array of lines of code.

index *

The index of the relevant line in the code array.

Return:

Number

The position in the line where the new scope starts or null if this line does not start a new scope.

private _scopeExitFunc(codeArray: *, index: *): Number source

Checks if a line identified by an index in an array ends an existing scope. Example: '}'

Params:

NameTypeAttributeDescription
codeArray *

An array of lines of code.

index *

The index of the relevant line in the code array.

Return:

Number

The position in the line where the scope ends or null if this line does not end a scope.