Class RSyntaxDocument
- java.lang.Object
-
- javax.swing.text.AbstractDocument
-
- javax.swing.text.PlainDocument
-
- org.fife.ui.rtextarea.RDocument
-
- org.fife.ui.rsyntaxtextarea.RSyntaxDocument
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<Token>
,javax.swing.text.Document
,SyntaxConstants
public class RSyntaxDocument extends RDocument implements java.lang.Iterable<Token>, SyntaxConstants
The document used byRSyntaxTextArea
. This document is likejavax.swing.text.PlainDocument
except that it also keeps track of syntax highlighting in the document. It has a "style" attribute associated with it that determines how syntax highlighting is done (i.e., what language is being highlighted).Instances of
RSyntaxTextArea
will only accept instances ofRSyntaxDocument
, since it is this document that keeps track of syntax highlighting. All others will cause an exception to be thrown.To change the language being syntax highlighted at any time, you merely have to call
setSyntaxStyle(java.lang.String)
. Other than that, this document can be treated like any other save one caveat: allDocumentEvent
s of typeCHANGE
use their offset and length values to represent the first and last lines, respectively, that have had their syntax coloring change. This is really a hack to increase the speed of the painting code and should really be corrected, but oh well.- Version:
- 1.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
-
-
Field Summary
Fields Modifier and Type Field Description private Token
cachedTokenList
private static boolean
DEBUG_TOKEN_CACHING
If this is set totrue
, debug information about how much token caching is helping is printed to stdout.private int
lastLine
protected DynamicIntArray
lastTokensOnLines
Array of values representing the "last token type" on each line.private javax.swing.text.Segment
s
private java.lang.String
syntaxStyle
The current syntax style.private TokenMaker
tokenMaker
Splits text into tokens for the current programming language.private TokenMakerFactory
tokenMakerFactory
Creates aTokenMaker
appropriate for a given programming language.private int
tokenRetrievalCount
private int
useCacheCount
-
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
-
Fields inherited from interface org.fife.ui.rsyntaxtextarea.SyntaxConstants
SYNTAX_STYLE_ACTIONSCRIPT, SYNTAX_STYLE_ASSEMBLER_6502, SYNTAX_STYLE_ASSEMBLER_X86, SYNTAX_STYLE_BBCODE, SYNTAX_STYLE_C, SYNTAX_STYLE_CLOJURE, SYNTAX_STYLE_CPLUSPLUS, SYNTAX_STYLE_CSHARP, SYNTAX_STYLE_CSS, SYNTAX_STYLE_CSV, SYNTAX_STYLE_D, SYNTAX_STYLE_DART, SYNTAX_STYLE_DELPHI, SYNTAX_STYLE_DOCKERFILE, SYNTAX_STYLE_DTD, SYNTAX_STYLE_FORTRAN, SYNTAX_STYLE_GO, SYNTAX_STYLE_GROOVY, SYNTAX_STYLE_HANDLEBARS, SYNTAX_STYLE_HOSTS, SYNTAX_STYLE_HTACCESS, SYNTAX_STYLE_HTML, SYNTAX_STYLE_INI, SYNTAX_STYLE_JAVA, SYNTAX_STYLE_JAVASCRIPT, SYNTAX_STYLE_JSON, SYNTAX_STYLE_JSON_WITH_COMMENTS, SYNTAX_STYLE_JSP, SYNTAX_STYLE_KOTLIN, SYNTAX_STYLE_LATEX, SYNTAX_STYLE_LESS, SYNTAX_STYLE_LISP, SYNTAX_STYLE_LUA, SYNTAX_STYLE_MAKEFILE, SYNTAX_STYLE_MARKDOWN, SYNTAX_STYLE_MXML, SYNTAX_STYLE_NONE, SYNTAX_STYLE_NSIS, SYNTAX_STYLE_PERL, SYNTAX_STYLE_PHP, SYNTAX_STYLE_PROPERTIES_FILE, SYNTAX_STYLE_PROTO, SYNTAX_STYLE_PYTHON, SYNTAX_STYLE_RUBY, SYNTAX_STYLE_RUST, SYNTAX_STYLE_SAS, SYNTAX_STYLE_SCALA, SYNTAX_STYLE_SQL, SYNTAX_STYLE_TCL, SYNTAX_STYLE_TYPESCRIPT, SYNTAX_STYLE_UNIX_SHELL, SYNTAX_STYLE_VISUAL_BASIC, SYNTAX_STYLE_WINDOWS_BATCH, SYNTAX_STYLE_XML, SYNTAX_STYLE_YAML
-
-
Constructor Summary
Constructors Constructor Description RSyntaxDocument(java.lang.String syntaxStyle)
Constructs a plain text document.RSyntaxDocument(TokenMakerFactory tmf, java.lang.String syntaxStyle)
Constructs a plain text document.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
fireInsertUpdate(javax.swing.event.DocumentEvent e)
Alerts all listeners to this document of an insertion.protected void
fireRemoveUpdate(javax.swing.event.DocumentEvent chng)
This method is called AFTER the content has been inserted into the document and the element structure has been updated.int
getClosestStandardTokenTypeForInternalType(int type)
Returns the closest"standard" token type
for a given "internal" token type (e.g.boolean
getCompleteMarkupCloseTags()
Returns whether closing markup tags should be automatically completed.boolean
getCurlyBracesDenoteCodeBlocks(int languageIndex)
Returns whether the current programming language uses curly braces ('{
' and '}
') to denote code blocks.boolean
getLanguageIsMarkup()
Returns whether the current language is a markup language, such as HTML, XML or PHP.int
getLastTokenTypeOnLine(int line)
Returns the token type of the last token on the given line.java.lang.String[]
getLineCommentStartAndEnd(int languageIndex)
Returns the text to place at the beginning and end of a line to "comment" it in this programming language.(package private) boolean
getMarkOccurrencesOfTokenType(int type)
Returns whether tokens of the specified type should have "mark occurrences" enabled for the current programming language.(package private) OccurrenceMarker
getOccurrenceMarker()
Returns the occurrence marker for the current language.boolean
getShouldIndentNextLine(int line)
This method returns whether auto indentation should be done if Enter is pressed at the end of the specified line.java.lang.String
getSyntaxStyle()
Returns the syntax style being used.Token
getTokenListForLine(int line)
Returns a token list for the specified segment of text representing the specified line number.(package private) boolean
insertBreakSpecialHandling(java.awt.event.ActionEvent e)
boolean
isIdentifierChar(int languageIndex, char ch)
Returns whether a character could be part of an "identifier" token in a specific language.java.util.Iterator<Token>
iterator()
Returns an iterator over the paintable tokens in this document.private void
readObject(java.io.ObjectInputStream in)
Deserializes a document.private void
setSharedSegment(int line)
Makes our privateSegment s
point to the text in our document referenced by the specified element.void
setSyntaxStyle(java.lang.String styleKey)
Sets the syntax style being used for syntax highlighting in this document.void
setSyntaxStyle(TokenMaker tokenMaker)
Sets the syntax style being used for syntax highlighting in this document.void
setTokenMakerFactory(TokenMakerFactory tmf)
Sets the token maker factory used by this document.private int
updateLastTokensBelow(int line, int numLines, int previousTokenType)
Loops through the last-tokens-on-lines array from a specified point onward, updating last-token values until they stop changing.private void
updateSyntaxHighlightingInformation()
Updates internal state information; e.g.-
Methods inherited from class javax.swing.text.PlainDocument
createDefaultRoot, getDefaultRootElement, getParagraphElement, insertString, insertUpdate, removeUpdate
-
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
-
-
-
-
Field Detail
-
tokenMakerFactory
private transient TokenMakerFactory tokenMakerFactory
Creates aTokenMaker
appropriate for a given programming language.
-
tokenMaker
private transient TokenMaker tokenMaker
Splits text into tokens for the current programming language.
-
syntaxStyle
private java.lang.String syntaxStyle
The current syntax style. Only cached to keep this class serializable.
-
lastTokensOnLines
protected transient DynamicIntArray lastTokensOnLines
Array of values representing the "last token type" on each line. This is used in cases such as multi-line comments: if the previous line ended with an (unclosed) multi-line comment, we can use this knowledge and start the current line's syntax highlighting in multi-line comment state.
-
lastLine
private transient int lastLine
-
cachedTokenList
private transient Token cachedTokenList
-
useCacheCount
private transient int useCacheCount
-
tokenRetrievalCount
private transient int tokenRetrievalCount
-
s
private transient javax.swing.text.Segment s
-
DEBUG_TOKEN_CACHING
private static final boolean DEBUG_TOKEN_CACHING
If this is set totrue
, debug information about how much token caching is helping is printed to stdout.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RSyntaxDocument
public RSyntaxDocument(java.lang.String syntaxStyle)
Constructs a plain text document. A default root element is created, and the tab size set to 5.- Parameters:
syntaxStyle
- The syntax highlighting scheme to use.
-
RSyntaxDocument
public RSyntaxDocument(TokenMakerFactory tmf, java.lang.String syntaxStyle)
Constructs a plain text document. A default root element is created, and the tab size set to 5.- Parameters:
tmf
- TheTokenMakerFactory
for this document. If this isnull
, a default factory is used.syntaxStyle
- The syntax highlighting scheme to use.
-
-
Method Detail
-
fireInsertUpdate
protected void fireInsertUpdate(javax.swing.event.DocumentEvent e)
Alerts all listeners to this document of an insertion. This is overridden so we can update our syntax highlighting stuff.The syntax highlighting stuff has to be here instead of in
insertUpdate
becauseinsertUpdate
is not called by the undo/redo actions, but this method is.- Overrides:
fireInsertUpdate
in classjavax.swing.text.AbstractDocument
- Parameters:
e
- The change.
-
fireRemoveUpdate
protected void fireRemoveUpdate(javax.swing.event.DocumentEvent chng)
This method is called AFTER the content has been inserted into the document and the element structure has been updated.The syntax-highlighting updates need to be done here (as opposed to an override of
postRemoveUpdate
) as this method is called in response to undo/redo events, whereaspostRemoveUpdate
is not.Now that the text is actually inserted into the content and element structure, we can update our token elements and "last tokens on lines" structure.
- Overrides:
fireRemoveUpdate
in classjavax.swing.text.AbstractDocument
- Parameters:
chng
- The change that occurred.- See Also:
PlainDocument.removeUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent)
-
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
).- Parameters:
type
- The token type.- Returns:
- The closest "standard" token type. If a mapping is not defined
for this language, then
type
is returned.
-
getCompleteMarkupCloseTags
public boolean getCompleteMarkupCloseTags()
Returns whether closing markup tags should be automatically completed. This method only returnstrue
ifgetLanguageIsMarkup()
also returnstrue
.- Returns:
- Whether markup closing tags should be automatically completed.
- See Also:
getLanguageIsMarkup()
-
getCurlyBracesDenoteCodeBlocks
public boolean getCurlyBracesDenoteCodeBlocks(int languageIndex)
Returns whether the current programming language uses curly braces ('{
' and '}
') to denote code blocks.- Parameters:
languageIndex
- The language index at the offset in question. Since someTokenMaker
s effectively have nested languages (such as JavaScript in HTML), this parameter tells theTokenMaker
what sub-language to look at.- Returns:
- Whether curly braces denote code blocks.
-
getLanguageIsMarkup
public boolean getLanguageIsMarkup()
Returns whether the current language is a markup language, such as HTML, XML or PHP.- Returns:
- Whether the current language is a markup language.
-
getLastTokenTypeOnLine
public int getLastTokenTypeOnLine(int line)
Returns the token type of the last token on the given line.- Parameters:
line
- The line to inspect.- Returns:
- The token type of the last token on the specified line. If the line is invalid, an exception is thrown.
-
getLineCommentStartAndEnd
public java.lang.String[] getLineCommentStartAndEnd(int languageIndex)
Returns the text to place at the beginning and end of a line to "comment" it in this programming language.- Parameters:
languageIndex
- The language index to get comment information for.- 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 ofnull
for the array means this language does not support commenting/uncommenting lines.
-
getMarkOccurrencesOfTokenType
boolean getMarkOccurrencesOfTokenType(int type)
Returns whether tokens of the specified type should have "mark occurrences" enabled for the current programming language.- Parameters:
type
- The token type.- Returns:
- Whether tokens of this type should have "mark occurrences" enabled.
-
getOccurrenceMarker
OccurrenceMarker getOccurrenceMarker()
Returns the occurrence marker for the current language.- Returns:
- The occurrence marker.
-
getShouldIndentNextLine
public boolean getShouldIndentNextLine(int line)
This method returns whether auto indentation should be done if Enter is pressed at the end of the specified line.- Parameters:
line
- The line to check.- Returns:
- Whether an extra indentation should be done.
-
getSyntaxStyle
public java.lang.String getSyntaxStyle()
Returns the syntax style being used.- Returns:
- The syntax style.
- See Also:
setSyntaxStyle(String)
-
getTokenListForLine
public Token getTokenListForLine(int line)
Returns a token list for the specified segment of text representing the specified line number. This method is basically a wrapper fortokenMaker.getTokenList
that takes into account the last token on the previous line to assure token accuracy.- Parameters:
line
- The line number oftext
in the document, >= 0.- Returns:
- A token list representing the specified line.
-
insertBreakSpecialHandling
boolean insertBreakSpecialHandling(java.awt.event.ActionEvent e)
-
isIdentifierChar
public boolean isIdentifierChar(int languageIndex, char ch)
Returns whether a character could be part of an "identifier" token in a specific language. This is used to identify such things as the bounds of the "word" to select on double-clicking.- Parameters:
languageIndex
- The language index the character was found in.ch
- The character.- Returns:
- Whether the character could be part of an "identifier" token.
-
iterator
public java.util.Iterator<Token> iterator()
Returns an iterator over the paintable tokens in this document. Results are undefined if this document is modified while the iterator is being iterated through, so this should only be used on the EDT.The
remove()
method of the returned iterator will throw anUnsupportedOperationException
.- Specified by:
iterator
in interfacejava.lang.Iterable<Token>
- Returns:
- An iterator.
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.lang.ClassNotFoundException, java.io.IOException
Deserializes a document.- Parameters:
in
- The stream to read from.- Throws:
java.lang.ClassNotFoundException
- If an unexpected error occurs.java.io.IOException
- If an IO error occurs.
-
setSharedSegment
private void setSharedSegment(int line)
Makes our privateSegment s
point to the text in our document referenced by the specified element. Note thatline
MUST be a valid line number in the document.- Parameters:
line
- The line number you want to get.
-
setSyntaxStyle
public void setSyntaxStyle(java.lang.String styleKey)
Sets the syntax style being used for syntax highlighting in this document. What styles are supported by a document is determined by itsTokenMakerFactory
. By default, allRSyntaxDocument
s support all languages built intoRSyntaxTextArea
.- Parameters:
styleKey
- The new style to use, such asSyntaxConstants.SYNTAX_STYLE_JAVA
. If this style is not known or supported by this document, thenSyntaxConstants.SYNTAX_STYLE_NONE
is used.- See Also:
setSyntaxStyle(TokenMaker)
,getSyntaxStyle()
-
setSyntaxStyle
public void setSyntaxStyle(TokenMaker tokenMaker)
Sets the syntax style being used for syntax highlighting in this document. You should call this method if you've created a custom token maker for a language not normally supported byRSyntaxTextArea
.- Parameters:
tokenMaker
- The new token maker to use.- See Also:
setSyntaxStyle(String)
-
setTokenMakerFactory
public void setTokenMakerFactory(TokenMakerFactory tmf)
Sets the token maker factory used by this document.- Parameters:
tmf
- TheTokenMakerFactory
for this document. If this isnull
, a default factory is used.
-
updateLastTokensBelow
private int updateLastTokensBelow(int line, int numLines, int previousTokenType)
Loops through the last-tokens-on-lines array from a specified point onward, updating last-token values until they stop changing. This should be called when lines are updated/inserted/removed, as doing so may cause lines below to change color.- Parameters:
line
- The first line to check for a change in last-token value.numLines
- The number of lines in the document.previousTokenType
- The last-token value of the line just beforeline
.- Returns:
- The last line that needs repainting.
-
updateSyntaxHighlightingInformation
private void updateSyntaxHighlightingInformation()
Updates internal state information; e.g. the "last tokens on lines" data. After this, a changed update is fired to let listeners know that the document's structure has changed.This is called internally whenever the syntax style changes.
-
-