Skip to content

Commit

Permalink
bobbylight#548 add methods to set TokenMaker with style key on RSynta…
Browse files Browse the repository at this point in the history
…xTextArea and RSyntaxDocument
  • Loading branch information
ptorngren committed Jun 13, 2024
1 parent 6aef51a commit 611cef8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,20 @@ public void setSyntaxStyle(TokenMaker tokenMaker) {
this.syntaxStyle = "text/unknown"; // TODO: Make me public?
}

/**
* 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.
* @param styleKey The new style to use, such as {@link SyntaxConstants#SYNTAX_STYLE_JAVA}.
* @see #setSyntaxStyle(TokenMaker)
*/
public void setSyntaxStyle(TokenMaker tokenMaker, String styleKey) {
this.tokenMaker = tokenMaker;
updateSyntaxHighlightingInformation();
this.syntaxStyle = styleKey;
}

/**
* Sets the token maker factory used by this document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,30 @@ public void setSyntaxEditingStyle(String styleKey) {

}

/**
* Sets what type of syntax highlighting this editor is doing using a specific {@link TokenMaker}.
* This method fires a property change of type {@link #SYNTAX_STYLE_PROPERTY}.
* 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.
* @param styleKey The new style to use, such as {@link SyntaxConstants#SYNTAX_STYLE_JAVA}.
* @see #setSyntaxEditingStyle(String)
* @see RSyntaxDocument#setSyntaxStyle(TokenMaker, String)
* @see SyntaxConstants
*/
public void setSyntaxEditingStyle(TokenMaker tokenMaker, String styleKey) {
((RSyntaxDocument)getDocument()).setSyntaxStyle(tokenMaker, styleKey);

if (styleKey==null) {
styleKey = SYNTAX_STYLE_NONE;
}
if (!styleKey.equals(syntaxStyleKey)) {
String oldStyle = syntaxStyleKey;
syntaxStyleKey = styleKey;
firePropertyChange(SYNTAX_STYLE_PROPERTY, oldStyle, styleKey);
}
}

/**
* Sets all the colors used in syntax highlighting to the colors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,18 @@ void testSetSyntaxStyle_CustomTokenMaker() {

}

@Test
void testSetSyntaxStyle_CustomTokenMakerStyle() {

String noStyle = SyntaxConstants.SYNTAX_STYLE_NONE;
doc = new RSyntaxDocument(noStyle);
Assertions.assertEquals(noStyle, doc.getSyntaxStyle());

String htmlStyle = SyntaxConstants.SYNTAX_STYLE_HTML;
TokenMaker tokenMaker = new HTMLTokenMaker();
doc.setSyntaxStyle(tokenMaker, htmlStyle);
Assertions.assertEquals(htmlStyle, doc.getSyntaxStyle());
}

@Test
void testSetTokenMakerFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Scanner;

import org.fife.ui.SwingRunnerExtension;
import org.fife.ui.rsyntaxtextarea.modes.HTMLTokenMaker;
import org.fife.ui.rsyntaxtextarea.modes.JavaTokenMaker;
import org.fife.ui.rsyntaxtextarea.parser.AbstractParser;
import org.fife.ui.rsyntaxtextarea.parser.ParseResult;
Expand Down Expand Up @@ -710,6 +711,20 @@ void testSyntaxEditingStyle() {
Assertions.assertEquals(SyntaxConstants.SYNTAX_STYLE_JAVA, textArea.getSyntaxEditingStyle());
}

@Test
void testSyntaxEditingStyleTokenMaker() {
RSyntaxTextArea textArea = new RSyntaxTextArea();

String noStyle = SyntaxConstants.SYNTAX_STYLE_NONE;
Assertions.assertEquals(noStyle, textArea.getSyntaxEditingStyle());
Assertions.assertEquals(noStyle, ((RSyntaxDocument)textArea.getDocument()).getSyntaxStyle());

String htmlStyle = SyntaxConstants.SYNTAX_STYLE_HTML;
TokenMaker tokenMaker = new HTMLTokenMaker();
textArea.setSyntaxEditingStyle(tokenMaker, htmlStyle);
Assertions.assertEquals(htmlStyle, textArea.getSyntaxEditingStyle());
Assertions.assertEquals(htmlStyle, ((RSyntaxDocument)textArea.getDocument()).getSyntaxStyle());
}

/**
* In {@code RSyntaxTextArea}, the code path {@code setDocument()} ->
Expand Down

0 comments on commit 611cef8

Please sign in to comment.