📄 rsyntaxdocument.java
字号:
getText(startOffset,endOffset-startOffset, s);
} catch (BadLocationException ble) {
ble.printStackTrace();
return null;
}
int initialTokenType = Token.NULL;
if (line>0)
initialTokenType = getLastTokenTypeOnLine(line-1);
return tokenMaker.getTokenList(s, initialTokenType, startOffset);
/*
Token tokenList = tokenMaker.getTokenList(s, initialTokenType, startOffset);
if (parser!=null)
parser.parseTokenList(tokenList);
return tokenList;
*/
}
/*****************************************************************************/
/**
* Makes our private <code>Segment s</code> point to the text in our
* document referenced by the specified element. Note that
* <code>line</code> MUST be a valid line number in the document.
*
* @param line The line number you want to get.
*/
private final void setSharedSegment(int line) {
Element map = getDefaultRootElement();
int numLines = map.getElementCount();
Element element = map.getElement(line);
if (element==null)
throw new InternalError("Invalid line number: " + line);
int startOffset = element.getStartOffset();
int endOffset = (line==numLines-1 ?
element.getEndOffset()-1 : element.getEndOffset() - 1);
try {
getText(startOffset, endOffset-startOffset, s);
} catch (BadLocationException ble) {
throw new InternalError("Text range not in document: " +
startOffset + "-" + endOffset);
}
}
/*****************************************************************************/
/**
* Sets the syntax style being used for syntax highlighting in this
* document.
*
* @param syntaxStyle The new style to use, such as
* <code>JAVA_SYNTAX_STYLE</code>. If this value is not one of
* the syntax constants, then <code>PLAIN_SYNTAX_STYLE</code> is
* used.
*/
public void setSyntaxStyle(int syntaxStyle) {
// Get the right token maker for syntax highlighting.
switch (syntaxStyle) {
case ASSEMBLER_X86_SYNTAX_STYLE:
tokenMaker = new AssemblerX86TokenMaker();
break;
case C_SYNTAX_STYLE:
tokenMaker = new CTokenMaker();
break;
case CPLUSPLUS_SYNTAX_STYLE:
tokenMaker = new CPlusPlusTokenMaker();
break;
case CSHARP_SYNTAX_STYLE:
tokenMaker = new CSharpTokenMaker();
break;
case CSS_SYNTAX_STYLE:
tokenMaker = new CSSTokenMaker();
break;
case FORTRAN_SYNTAX_STYLE:
tokenMaker = new FortranTokenMaker();
break;
case HTML_SYNTAX_STYLE:
tokenMaker = new HTMLTokenMaker();
break;
case JAVA_SYNTAX_STYLE:
tokenMaker = new JavaTokenMaker();
break;
case JAVASCRIPT_SYNTAX_STYLE:
tokenMaker = new JavaScriptTokenMaker();
break;
case PERL_SYNTAX_STYLE:
tokenMaker = new PerlTokenMaker();
break;
case PYTHON_SYNTAX_STYLE:
tokenMaker = new PythonTokenMaker();
break;
case PROPERTIES_FILE_SYNTAX_STYLE:
tokenMaker = new PropertiesFileTokenMaker();
break;
case SAS_SYNTAX_STYLE:
tokenMaker = new SASTokenMaker();
break;
case SQL_SYNTAX_STYLE:
tokenMaker = new SQLTokenMaker();
break;
case UNIX_SHELL_SYNTAX_STYLE:
tokenMaker = new UnixShellTokenMaker();
break;
case WINDOWS_BATCH_SYNTAX_STYLE:
tokenMaker = new WindowsBatchTokenMaker();
break;
case XML_SYNTAX_STYLE:
tokenMaker = new XMLTokenMaker();
break;
case NO_SYNTAX_STYLE:
default:
tokenMaker = new PlainTextTokenMaker();
break;
} // End of switch (syntaxStyle).
updateSyntaxHighlightingInformation();
}
/*****************************************************************************/
/**
* 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 by
* <code>RSyntaxTextArea</code>.
*
* @param tokenMaker The new token maker to use.
*/
public void setSyntaxStyle(TokenMaker tokenMaker) {
this.tokenMaker = tokenMaker;
updateSyntaxHighlightingInformation();
}
/*****************************************************************************/
/**
* Sets whether whitespace is visible. This property is actually setting
* whether the tokens generated from this document "paint" something when
* they represent whitespace.
*
* @param visible Whether whitespace should be visible.
*/
// public void setWhitespaceVisible(boolean visible) {
// tokenMaker.setWhitespaceVisible(visible);
// }
public void setWhitespaceVisible(boolean visible, RSyntaxTextArea textArea) {
tokenMaker.setWhitespaceVisible(visible, textArea);
}
/*****************************************************************************/
/**
* 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.
*
* @param line The first line to check for a change in last-token value.
* @param numLines The number of lines in the document.
* @param previousTokenType The last-token value of the line just before
* <code>line</code>.
* @return The last line that needs repainting.
*/
private int updateLastTokensBelow(int line, int numLines, int previousTokenType) {
int firstLine = line;
// Loop through all lines past our starting point. We don't need to check the
// last line (numLines-1) because if line numLines-2's end-token value changed,
// line numLines-1 certainly needs repainting, and there are no lines past
// numLines-1 to worry about.
int end = numLines - 1;
//System.err.println("--- end==" + end + " (numLines==" + numLines + ")");
while (line<end) {
setSharedSegment(line); // Sets s's text to that of line 'line' in the document.
int oldTokenType = lastTokensOnLines.get(line);
int newTokenType = tokenMaker.getLastTokenTypeOnLine(s, previousTokenType);
//System.err.println("---------------- line " + line + "; oldTokenType==" + oldTokenType + ", newTokenType==" + newTokenType + ", s=='" + s + "'");
// If this line's end-token value didn't change, stop here. Note
// that we're saying this line needs repainting; this is because
// the beginning of this line did indeed change color, but the
// end didn't.
if (oldTokenType==newTokenType) {
//System.err.println("... ... ... repainting lines " + firstLine + "-" + line);
fireChangedUpdate(new DefaultDocumentEvent(firstLine, line, DocumentEvent.EventType.CHANGE));
return line;
}
// If the line's end-token value did change, update it and
// keep going.
lastTokensOnLines.set(line, newTokenType);
previousTokenType = newTokenType;
line++;
} // End of while (line<numLines).
// If any lines had their token types changed, fire a changed update
// for them. The view will repaint the area covered by the lines.
// FIXME: We currently cheat and send the line range that needs to be
// repainted as the "offset and length" of the change, since this is
// what the view needs. We really should send the actual offset and
// length.
if (line>firstLine) {
//System.err.println("... ... ... repainting lines " + firstLine + "-" + line);
fireChangedUpdate(new DefaultDocumentEvent(firstLine, line,
DocumentEvent.EventType.CHANGE));
}
return line;
}
/*****************************************************************************/
/**
* 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.<p>
*
* This is called internally whenever the syntax style changes.
*/
protected void updateSyntaxHighlightingInformation() {
// Reinitialize the "last token on each line" array. Note that since
// the actual text in the document isn't changing, the number of lines
// is the same.
Element map = getDefaultRootElement();
int numLines = map.getElementCount();
int lastTokenType = Token.NULL;
for (int i=0; i<numLines; i++) {
setSharedSegment(i);
lastTokenType = tokenMaker.getLastTokenTypeOnLine(
s, lastTokenType);
lastTokensOnLines.set(i, lastTokenType);
}
// Let everybody know that syntax styles have (probably) changed.
fireChangedUpdate(new DefaultDocumentEvent(
0, numLines-1, DocumentEvent.EventType.CHANGE));
}
/*****************************************************************************/
/************************** PRIVATE INNER CLASSES ****************************/
/*****************************************************************************/
/**
* Element representing a token in a line in the document.
*
* This class is not currently used.
*
* Using these to represent tokens in a line represent two problems:
* 1. Much, much higher memory usage.
* 2. I'm not sure how to update them when their parent element gets
* updated (i.e., their p0 and p1).
* BUT, using them would provide these benefits:
* 1. No more getTokenList() call for every caret blink, line displayed
* via scrolling, etc. It'd all be cached.
* 2. As an extension of (1) above, scrolling via the page-down key would
* be sped up a lot, as it redraws the screen twice (and hence calls
* getTokenList() twice for each line on the screen).
*/
protected class TokenElement extends AbstractDocument.LeafElement {
/**
*
*/
private static final long serialVersionUID = -8282312801308650643L;
private int type;
public TokenElement(Element parent, int p0,int p1, int type) {
super(parent, null, p0,p1);
this.type = type;
}
public int getType() {
return type;
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -