org.fife.ui.rsyntaxtextarea
Class AbstractTokenMaker

java.lang.Object
  extended by org.fife.ui.rsyntaxtextarea.AbstractTokenMaker
All Implemented Interfaces:
TokenMaker
Direct Known Subclasses:
UnixShellTokenMaker, WindowsBatchTokenMaker

public abstract class AbstractTokenMaker
extends java.lang.Object

An abstract implementation of the TokenMaker interface. It should be overridden for every language for which you want to provide syntax highlighting.

Version:
0.2
Author:
Robert Futrell
See Also:
Token

Field Summary
protected  TokenImpl currentToken
          Used in the creation of the linked list.
protected  TokenImpl firstToken
          The first token in the returned linked list.
protected  TokenImpl previousToken
          Used in the creation of the linked list.
protected  TokenMap wordsToHighlight
          Hash table of words to highlight and what token type they are.
 
Constructor Summary
AbstractTokenMaker()
          Constructor.
 
Method Summary
 void addNullToken()
          Adds a null token to the end of the current linked list of tokens.
 void addToken(char[] array, int start, int end, int tokenType, int startOffset)
          Adds the token specified to the current linked list of tokens.
 void addToken(char[] array, int start, int end, int tokenType, int startOffset, boolean hyperlink)
          Adds the token specified to the current linked list of tokens.
 void addToken(javax.swing.text.Segment segment, int start, int end, int tokenType, int startOffset)
          Adds the token specified to the current linked list of tokens.
protected  OccurrenceMarker createOccurrenceMarker()
          Returns the occurrence marker to use for this token maker.
 int getClosestStandardTokenTypeForInternalType(int type)
          Returns the closest "standard" token type for a given "internal" token type (e.g. one whose value is < 0).
 boolean getCurlyBracesDenoteCodeBlocks()
          Returns whether this programming language uses curly braces ('{' and '}') to denote code blocks.
 javax.swing.Action getInsertBreakAction()
          Returns an action to handle "insert break" key presses (i.e.
 int getLastTokenTypeOnLine(javax.swing.text.Segment text, int initialTokenType)
          Returns the last token on this line's type if the token is "unfinished", or TokenTypes.NULL if it was finished.
 java.lang.String[] getLineCommentStartAndEnd()
          Returns the text to place at the beginning and end of a line to "comment" it in a this programming language.
 boolean getMarkOccurrencesOfTokenType(int type)
          Returns whether tokens of the specified type should have "mark occurrences" enabled for the current programming language.
 OccurrenceMarker getOccurrenceMarker()
          Returns the object in charge of marking all occurrences of the token at the current caret position, if it is a relevant token.
 boolean getShouldIndentNextLineAfter(Token token)
          The default implementation returns false always.
abstract  TokenMap getWordsToHighlight()
          Returns the words to highlight for this programming language.
 boolean isMarkupLanguage()
          The default implementation returns false always.
 void removeLastToken()
          Removes the token last added from the linked list of tokens.
protected  void resetTokenList()
          Deletes the linked list of tokens so we can begin anew.
 void setLanguageIndex(int languageIndex)
          Sets the language index to assign to tokens moving forward.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.fife.ui.rsyntaxtextarea.TokenMaker
getTokenList
 

Field Detail

wordsToHighlight

protected TokenMap wordsToHighlight
Hash table of words to highlight and what token type they are. The keys are the words to highlight, and their values are the token types, for example, Token.RESERVED_WORD or Token.FUNCTION.


firstToken

protected TokenImpl firstToken
The first token in the returned linked list.


currentToken

protected TokenImpl currentToken
Used in the creation of the linked list.


previousToken

protected TokenImpl previousToken
Used in the creation of the linked list.

Constructor Detail

AbstractTokenMaker

public AbstractTokenMaker()
Constructor.

Method Detail

getWordsToHighlight

public abstract TokenMap getWordsToHighlight()
Returns the words to highlight for this programming language.

Returns:
A TokenMap containing the words to highlight for this programming language.

removeLastToken

public void removeLastToken()
Removes the token last added from the linked list of tokens. The programmer should never have to call this directly; it can be called by subclasses of TokenMaker if necessary.


addNullToken

public void addNullToken()
Adds a null token to the end of the current linked list of tokens. This should be put at the end of the linked list whenever the last token on the current line is NOT a multi-line token.

Specified by:
addNullToken in interface TokenMaker

addToken

public void addToken(javax.swing.text.Segment segment,
                     int start,
                     int end,
                     int tokenType,
                     int startOffset)
Adds the token specified to the current linked list of tokens.

Parameters:
segment - Segment to get text from.
start - Start offset in segment of token.
end - End offset in segment of token.
tokenType - The token's type.
startOffset - The offset in the document at which this token occurs.

addToken

public void addToken(char[] array,
                     int start,
                     int end,
                     int tokenType,
                     int startOffset)
Adds the token specified to the current linked list of tokens.

Specified by:
addToken in interface TokenMaker
Parameters:
array - The character array from which to get the text.
start - Start offset in segment of token.
end - End offset in segment of token.
tokenType - The token's type.
startOffset - The offset in the document at which this token occurs.

addToken

public void addToken(char[] array,
                     int start,
                     int end,
                     int tokenType,
                     int startOffset,
                     boolean hyperlink)
Adds the token specified to the current linked list of tokens.

Parameters:
array - The character array.
start - The starting offset in the array.
end - The ending offset in the array.
tokenType - The token's type.
startOffset - The offset in the document at which this token occurs.
hyperlink - Whether this token is a hyperlink.

createOccurrenceMarker

protected OccurrenceMarker createOccurrenceMarker()
Returns the occurrence marker to use for this token maker. Subclasses can override to use different implementations.

Returns:
The occurrence marker to use.

getClosestStandardTokenTypeForInternalType

public int getClosestStandardTokenTypeForInternalType(int type)
Returns the closest "standard" token type for a given "internal" token type (e.g. one whose value is < 0).

The default implementation returns type always, which denotes that a mapping from internal token types to standard token types is not defined; subclasses can override.

Specified by:
getClosestStandardTokenTypeForInternalType in interface TokenMaker
Parameters:
type - The token type.
Returns:
The closest "standard" token type.

getCurlyBracesDenoteCodeBlocks

public boolean getCurlyBracesDenoteCodeBlocks()
Returns whether this programming language uses curly braces ('{' and '}') to denote code blocks.

The default implementation returns false; subclasses can override this method if necessary.

Specified by:
getCurlyBracesDenoteCodeBlocks in interface TokenMaker
Returns:
Whether curly braces denote code blocks.

getInsertBreakAction

public javax.swing.Action getInsertBreakAction()
Returns an action to handle "insert break" key presses (i.e. Enter). The default implementation returns null. Subclasses can override.

Specified by:
getInsertBreakAction in interface TokenMaker
Returns:
The default implementation always returns null.

getLastTokenTypeOnLine

public int getLastTokenTypeOnLine(javax.swing.text.Segment text,
                                  int initialTokenType)
Returns the last token on this line's type if the token is "unfinished", or TokenTypes.NULL if it was finished. For example, if C-style syntax highlighting is being implemented, and text contained a line of code that contained the beginning of a comment but no end-comment marker ("*\/"), then this method would return TokenTypes.COMMENT_MULTILINE for that line. This is useful for doing syntax highlighting.

Specified by:
getLastTokenTypeOnLine in interface TokenMaker
Parameters:
text - The line of tokens to examine.
initialTokenType - The token type to start with (i.e., the value of getLastTokenTypeOnLine for the line before text).
Returns:
The last token on this line's type, or TokenTypes.NULL if the line was completed.

getLineCommentStartAndEnd

public java.lang.String[] getLineCommentStartAndEnd()
Returns the text to place at the beginning and end of a line to "comment" it in a this programming language.

Specified by:
getLineCommentStartAndEnd in interface TokenMaker
Returns:
The start and end strings to add to a line to "comment" it out. A null value for either means there is no string to add for that part. A value of null for the array means this language does not support commenting/uncommenting lines.

getMarkOccurrencesOfTokenType

public boolean getMarkOccurrencesOfTokenType(int type)
Returns whether tokens of the specified type should have "mark occurrences" enabled for the current programming language. The default implementation returns true if type is TokenTypes.IDENTIFIER. Subclasses can override this method to support other token types, such as TokenTypes.VARIABLE.

Specified by:
getMarkOccurrencesOfTokenType in interface TokenMaker
Parameters:
type - The token type.
Returns:
Whether tokens of this type should have "mark occurrences" enabled.

getOccurrenceMarker

public OccurrenceMarker getOccurrenceMarker()
Returns the object in charge of marking all occurrences of the token at the current caret position, if it is a relevant token. If null is returned, a default OccurrenceMarker is used.

Specified by:
getOccurrenceMarker in interface TokenMaker
Returns:
The occurrence marker for this language, or null for none.

getShouldIndentNextLineAfter

public boolean getShouldIndentNextLineAfter(Token token)
The default implementation returns false always. Languages that wish to better support auto-indentation can override this method.

Specified by:
getShouldIndentNextLineAfter in interface TokenMaker
Parameters:
token - The token the previous line ends with.
Returns:
Whether the next line should be indented.

isMarkupLanguage

public boolean isMarkupLanguage()
The default implementation returns false always. Subclasses that are highlighting a markup language should override this method to return true.

Specified by:
isMarkupLanguage in interface TokenMaker
Returns:
false always.

resetTokenList

protected void resetTokenList()
Deletes the linked list of tokens so we can begin anew. This should never have to be called by the programmer, as it is automatically called whenever the user calls getLastTokenTypeOnLine(Segment, int) or TokenMaker.getTokenList(Segment, int, int).


setLanguageIndex

public void setLanguageIndex(int languageIndex)
Sets the language index to assign to tokens moving forward. This property is used to designate tokens as being in "secondary" languages (such as CSS or JavaScript in HTML).

Parameters:
languageIndex - The new language index. A value of 0 denotes the "main" language, any positive value denotes a specific secondary language. Negative values will be treated as 0.