Skip to content

XUICEWrenFormatter

Inherits XUICEAbstractFormatter

Implements XUICEFormatter

Description

A XUICodeEditor formatter for the Wren programming language.

Properties

Name Type Read-Only
Delimiters() XUICELineToken
Keywords Dictionary
MatchedDelimiters Dictionary
MatchedOpeningDelimiters() XUICELineToken

Methods

Name Parameters Returns
AddComment Boolean
AddHexNumberToken
AddIdentifierOrKeywordToken
AddMatchingDelimiters openingDelimiter As XUICELineToken, closingDelimiter As XUICELineToken
AddMultilineTokens type As String, startLine As Integer, startLocal As Integer, endLine As Integer, endPosLocal As Integer, fallbackType As String XUICELineToken
AddNumberToken
AddRawStringToken
AddStringToken
AllowsLeadingWhitespace Boolean
FirstNonCommentToken line As XUICELine XUICELineToken
HandleStringEscapeSequence stringStartLine As Integer Boolean
HandleStringInterpolation stringStartLine As Integer Boolean
IndentLines
InitialiseKeywordsDictionary Dictionary
IsClosingDelimiter t As XUICELineToken Boolean
IsCommentLine line As XUICELine Boolean
IsCommentToken token As XUICELineToken Boolean
IsOpeningDelimiter t As XUICELineToken Boolean
LastNonCommentToken line As XUICELine XUICELineToken
Name String
NearestDelimitersForCaretPos caretPos As Integer XUICEDelimiter
NextToken
Parse lines() As XUICELine
ProcessDelimiters
SetContinuationStatus line As XUICELine, previousLineLastToken As XUICELineToken
SkipWhitespace
SupportsDelimiterHighlighting Boolean
SupportsUnmatchedBlockHighlighting Boolean
TokenIsComment token As XUICELineToken Boolean
Tokenise lines() As XUICELine, firstVisibleLineNumber As Integer, lastVisibleLineNumber As Integer
TokeniseAll lines() As XUICELine
TokenTypes String()

Constants

Name Type
TOKEN_ESCAPE String
TOKEN_INTERPOLATION String

TOKEN_ESCAPE As String Used for escape sequences.


TOKEN_INTERPOLATION As String Used for string interpolations (e.g: %(name)).


Enumerations

CommentTypes

Used internally. The different types of supported comments.

Name
Block
SingleLine

Property Descriptions

Delimiters() As XUICELineToken

A sorted array of all delimiter tokens in the source code.


Keywords As Dictionary

Case sensitive dictionary of keywords. Key = Keyword, Value = Nil.


MatchedDelimiters As Dictionary

All matched delimiters in the source code. Key = Delimiter A (XUICELineToken), Value = Delimiter matching Delimiter A (XUICELineToken).


MatchedOpeningDelimiters() As XUICELineToken

An array of all opening delimiters that have a matching closing delimiter sorted by their position in the source code (index 0 = first matched delimiter in the source code).


Method Descriptions

AddComment() As Boolean

Attempts to add a comment beginning from the current position. Returns True if successful.

Assumes the pointer is yet to consume the opening delimiter.

Single line comments start with // and end at the end of the line:

// This is comment.
var age = 40 // This is also a comment.

Block comments start with \* and end with */. They can span multiple lines:

  /* This
     a
  multi-line
  comment. */

They can also be nested:

  /* This is /* a nested */ comment. */

AddHexNumberToken()

Consumes and adds a hex number token starting at mCurrent.

Assumes that mCurrent points at the first hex digit (which has been verified to exist).

0xFF
  ^

AddIdentifierOrKeywordToken()

Adds either an identifier or keyword beginning at mTokenStartLocal.

Assumes that mTokenStartLocal is a valid identifier or keyword starting character and that mCurrent is pointing to the character immediately following the starting character.


AddMatchingDelimiters(openingDelimiter As XUICELineToken, closingDelimiter As XUICELineToken)

Adds the passed matching delimiters the MatchedDelimiters dictionary and the SortedMatchedDelimiters array.

We add both delimiters as keys so we can find either.


AddMultilineTokens(type As String, startLine As Integer, startLocal As Integer, endLine As Integer, endPosLocal As Integer, fallbackType As String) As XUICELineToken

Adds contiguous tokens of type beginning at startLine and startLocal all the way to endLine endPosLocal. Returns the last token added. the last token.

Assumes that all arguments are valid. fallbackType is the generic fallback token style to use if the editor's current theme doesn't define a style named type. Assumes fallbackType is a valid fallback type (i.e. one of the constants in XUICELineToken).


AddNumberToken()

Consumes and adds a number token starting at mCurrent.

Assumes that mCurrent points at a digit.


AddRawStringToken()

Attempts to consume and add a raw string token.

Assumes that mCurrent points here:

"""Hello """
   ^

"""
   ^
Multiline
"""

AddStringToken()

Attempts to consume and add a string token starting at mCurrent.

Assumes we have just consumed a double quote ("):

name = "Hello"
        ^

If the string is successfully added then the closing delimiter will be assigned a data key "isClosingDelimiter" with a value of True. This is used later when parsing.


AllowsLeadingWhitespace() As Boolean

True if this formatter allows whitespace at the beginning of a line. If False, the editor will strip it when pasting and prevent it from being typed.

Part of the XUICEFormatter interface.


FirstNonCommentToken(line As XUICELine) As XUICELineToken

Returns the first token on line that is not a comment or Nil if the line there are none.

Assumes line is not Nil.


HandleStringEscapeSequence(stringStartLine As Integer) As Boolean

Determines if there is a valid string escape sequence. Adds the relevant token(s) if so and returns True, otherwise returns False.

Assumes were are within a string and have peeked to see \:

System.write("age \tcool")
                  ^

stringStartLine is the number of the line that the preceding open string begins at. mTokenStartLocal points to the local position of the start of the preceding string.


HandleStringInterpolation(stringStartLine As Integer) As Boolean

Determines if there is a valid string interpolation. Adds the token(s) if so and returns True, otherwise returns False.

Assumes were are within a string and have peeked to see %(:

System.write("age %(25 + 15)"
                  ^

stringStartLine is the number of the line that the preceding open string begins at. mTokenStartLocal points to the local position of the start of the preceding string.


IndentLines()

Sets the indentation and continuation levels for each line.

Assumes ProcessDelimiters() has been called prior to this method.


InitialiseKeywordsDictionary() As Dictionary

Returns a case-sensitive dictionary of Wren's keywords / reserved words.


IsClosingDelimiter(t As XUICELineToken) As Boolean

True if t is a closing delimiter like }, ) or ].

Assumes t is not Nil.


IsCommentLine(line As XUICELine) As Boolean

True if this entire line is a comment.

Part of the XUICEFormatter interface.


IsCommentToken(token As XUICELineToken) As Boolean

True if token is a comment.

Assumes token is not Nil.


IsOpeningDelimiter(t As XUICELineToken) As Boolean

True if t is an opening delimiter like {, ( or [.

Assumes t is not Nil.


LastNonCommentToken(line As XUICELine) As XUICELineToken

Returns the last token on line that is not a comment or Nil if the line there are none.

Assumes line is not Nil.


Name() As String

The name of this formatter.

Part of the XUICEFormatter interface.


NearestDelimitersForCaretPos(caretPos As Integer) As XUICEDelimiter

Returns the nearest delimiters at the given caretPos. May be Nil.

Part of the XUICEFormatter interface.


NextToken()

Generates the next token and appends it to mLine.Tokens.


Parse(lines() As XUICELine)

Called periodically by the editor. An opportunity to parse the tokenised lines. Will always be called after the lines have been tokenised.

Part of the XUICEFormatter interface.


ProcessDelimiters()

Finds the locations of matching delimiters (e.g: braces, parentheses, etc) and adds them to MatchedDelimiters.


SetContinuationStatus(line As XUICELine, previousLineLastToken As XUICELineToken)

Sets line.IsContinuation based on the last token of the previous line.

Assumes line is not Nil. previousLineLastToken may be Nil.


SkipWhitespace()

Advances past whitespace.


SupportsDelimiterHighlighting() As Boolean

Returns True as this formatter supports highlighting the delimiters around the caret.

Part of the XUICEFormatter interface.


SupportsUnmatchedBlockHighlighting() As Boolean

True if this formatter highlights unmatched blocks.

Part of the XUICEFormatter interface.


TokenIsComment(token As XUICELineToken) As Boolean

True if token is considered to be a comment.

Part of the XUICEFormatter interface.


Tokenise(lines() As XUICELine, firstVisibleLineNumber As Integer, lastVisibleLineNumber As Integer)

Tokenises a portion of lines.

Note that we tokenise all lines, even though this method is passed the visible line numbers.

Part of the XUICEFormatter interface.


TokeniseAll(lines() As XUICELine)

Tokenises an array of lines.

Part of the XUICEFormatter interface.


TokenTypes() As String()

Returns an array of all token types used by this formatter.

Part of the XUICEFormatter interface.