Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 095c1a7

Browse files
committed
Fix bobbylight#193: Latex: EOL comment start % can be escaped
1 parent 91720e4 commit 095c1a7

File tree

3 files changed

+140
-65
lines changed

3 files changed

+140
-65
lines changed

src/main/java/org/fife/ui/rsyntaxtextarea/modes/LatexTokenMaker.flex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ import org.fife.ui.rsyntaxtextarea.*;
146146
* @return The first <code>Token</code> in a linked list representing
147147
* the syntax highlighted text.
148148
*/
149+
@Override
149150
public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
150151

151152
resetTokenList();
@@ -234,6 +235,10 @@ URL = (((https?|f(tp|ile))"://"|"www.")({URLCharacters}{URLEndCharacter})?)
234235
<YYINITIAL> {
235236

236237
([\\]{AnyChar}+) { addToken(Token.FUNCTION); }
238+
([\\]%) { int temp = zzStartRead;
239+
addToken(temp, temp, Token.SEPARATOR);
240+
addToken(temp + 1, temp + 1, Token.IDENTIFIER);
241+
}
237242
[\{\}] { addToken(Token.SEPARATOR); }
238243
("\\begin{"{AnyChar}+"}") { int temp = zzStartRead;
239244
addToken(temp, temp+5, Token.RESERVED_WORD);

src/main/java/org/fife/ui/rsyntaxtextarea/modes/LatexTokenMaker.java

Lines changed: 75 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* 09/20/2016
3+
*
4+
* This library is distributed under a modified BSD license. See the included
5+
* RSyntaxTextArea.License.txt file for details.
6+
*/
7+
package org.fife.ui.rsyntaxtextarea.modes;
8+
9+
import javax.swing.text.Segment;
10+
11+
import org.fife.ui.rsyntaxtextarea.Token;
12+
import org.fife.ui.rsyntaxtextarea.TokenMaker;
13+
import org.fife.ui.rsyntaxtextarea.TokenTypes;
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
18+
/**
19+
* Unit tests for the {@link LatexTokenMaker} class.
20+
*
21+
* @author Robert Futrell
22+
* @version 1.0
23+
*/
24+
public class LatexTokenMakerTest extends AbstractTokenMakerTest2 {
25+
26+
27+
@Override
28+
protected TokenMaker createTokenMaker() {
29+
return new LatexTokenMaker();
30+
}
31+
32+
33+
@Test
34+
public void testEolComments() {
35+
String[] eolCommentLiterals = {
36+
"% Hello world",
37+
};
38+
assertAllTokensOfType(eolCommentLiterals, TokenTypes.COMMENT_EOL);
39+
}
40+
41+
42+
@Test
43+
public void testEolComments_escapedPercentNotAComment() {
44+
String code = "\\% not-comment";
45+
Segment segment = createSegment(code);
46+
TokenMaker tm = createTokenMaker();
47+
Token token = tm.getTokenList(segment, TokenTypes.NULL, 0);
48+
Assert.assertTrue(token.isSingleChar(TokenTypes.SEPARATOR, '\\'));
49+
token = token.getNextToken();
50+
Assert.assertTrue(token.isSingleChar(TokenTypes.IDENTIFIER, '%'));
51+
}
52+
53+
54+
@Test
55+
public void testGetLineCommentStartAndEnd() {
56+
String[] startAndEnd = createTokenMaker().getLineCommentStartAndEnd(0);
57+
Assert.assertEquals("%", startAndEnd[0]);
58+
Assert.assertNull(startAndEnd[1]);
59+
}
60+
}

0 commit comments

Comments
 (0)