Skip to content

Commit ea82109

Browse files
committed
Extract state from TokenMarker
TokenMarker now serves as a holder for keyword list and to provide marking logic. I extracted the rest into TokenMarkerState. Each tab now has its own state, preventing bugs by leaking TokenMarker state between tabs amd Editor instances. There is no need to recompile any Modes, however TokenMarker should be redesigned in the next major version and Modes updated accordingly. For more details read TokenMarkerState header.
1 parent 0c363ae commit ea82109

6 files changed

Lines changed: 347 additions & 332 deletions

File tree

app/src/processing/app/syntax/JEditTextArea.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public final int offsetToX(int line, int offset) {
620620
* @param offset The offset, from the start of the line
621621
*/
622622
public int _offsetToX(int line, int offset) {
623-
TokenMarker tokenMarker = getTokenMarker();
623+
TokenMarkerState tokenMarker = getTokenMarker();
624624

625625
// Use painter's cached info for speed
626626
FontMetrics fm = painter.getFontMetrics();
@@ -682,7 +682,7 @@ public int _offsetToX(int line, int offset) {
682682
* @param x The x co-ordinate
683683
*/
684684
public int xToOffset(int line, int x) {
685-
TokenMarker tokenMarker = getTokenMarker();
685+
TokenMarkerState tokenMarker = getTokenMarker();
686686

687687
/* Use painter's cached info for speed */
688688
FontMetrics fm = painter.getFontMetrics();
@@ -855,7 +855,7 @@ public void setDocument(SyntaxDocument document,
855855
* Returns the document's token marker. Equivalent to calling
856856
* <code>getDocument().getTokenMarker()</code>.
857857
*/
858-
public final TokenMarker getTokenMarker() {
858+
public final TokenMarkerState getTokenMarker() {
859859
return document.getTokenMarker();
860860
}
861861

@@ -1680,7 +1680,7 @@ private void emitAsHTML(StringBuilder cf, int line, SyntaxDocument doc) {
16801680
int segmentOffset = segment.offset;
16811681
int segmentCount = segment.count;
16821682

1683-
TokenMarker tokenMarker = doc.getTokenMarker();
1683+
TokenMarkerState tokenMarker = doc.getTokenMarker();
16841684
// If syntax coloring is disabled, do simple translation
16851685
if (tokenMarker == null) {
16861686
for (int j = 0; j < segmentCount; j++) {

app/src/processing/app/syntax/PdeTextAreaPainter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void setMode(Mode mode) {
113113
* @param x horizontal position
114114
*/
115115
@Override
116-
protected void paintLine(Graphics gfx, int line, int x, TokenMarker marker) {
116+
protected void paintLine(Graphics gfx, int line, int x, TokenMarkerState marker) {
117117
try {
118118
// TODO This line is causing NPEs randomly ever since I added the
119119
// toggle for Java Mode/Debugger toolbar. [Manindra]
@@ -346,4 +346,4 @@ public Editor getEditor() {
346346
public PdeTextArea getPdeTextArea() {
347347
return (PdeTextArea) textArea;
348348
}
349-
}
349+
}

app/src/processing/app/syntax/SyntaxDocument.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SyntaxDocument extends PlainDocument
2727
* of this document up into tokens. May return null if this
2828
* document is not to be colorized.
2929
*/
30-
public TokenMarker getTokenMarker()
30+
public TokenMarkerState getTokenMarker()
3131
{
3232
return tokenMarker;
3333
}
@@ -40,9 +40,11 @@ public TokenMarker getTokenMarker()
4040
*/
4141
public void setTokenMarker(TokenMarker tm)
4242
{
43-
tokenMarker = tm;
44-
if(tm == null)
43+
if (tm == null) {
44+
tokenMarker = null;
4545
return;
46+
}
47+
tokenMarker = tm.createStateInstance();
4648
tokenMarker.insertLines(0,getDefaultRootElement()
4749
.getElementCount());
4850
tokenizeLines();
@@ -67,7 +69,7 @@ public void tokenizeLines()
6769
*/
6870
public void tokenizeLines(int start, int len)
6971
{
70-
if(tokenMarker == null || !tokenMarker.supportsMultilineTokens())
72+
if(tokenMarker == null || !tokenMarker.marker.supportsMultilineTokens())
7173
return;
7274

7375
Segment lineSegment = new Segment();
@@ -118,7 +120,7 @@ public void endCompoundEdit() {}
118120
public void addUndoableEdit(UndoableEdit edit) {}
119121

120122
// protected members
121-
protected TokenMarker tokenMarker;
123+
protected TokenMarkerState tokenMarker;
122124

123125
/**
124126
* We overwrite this method to update the token marker

app/src/processing/app/syntax/TextAreaPainter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public void paint(Graphics gfx) {
488488
int lastInvalid = firstLine + (clipRect.y + clipRect.height - 1) / height;
489489

490490
try {
491-
TokenMarker tokenMarker = textArea.getDocument().getTokenMarker();
491+
TokenMarkerState tokenMarker = textArea.getDocument().getTokenMarker();
492492
int x = textArea.getHorizontalOffset();
493493

494494
for (int line = firstInvalid; line <= lastInvalid; line++) {
@@ -611,7 +611,7 @@ public Segment getCurrentLine() {
611611
// Font defaultFont = getFont();
612612
// Color defaultColor = getForeground();
613613
protected void paintLine(Graphics gfx, int line, int x,
614-
TokenMarker tokenMarker) {
614+
TokenMarkerState tokenMarker) {
615615
currentLineIndex = line;
616616
int y = textArea.lineToY(line);
617617

@@ -671,7 +671,7 @@ protected void paintPlainLine(Graphics gfx, int line, int x, int y) {
671671
// int line, Font defaultFont,
672672
// Color defaultColor, int x, int y) {
673673
protected void paintSyntaxLine(Graphics gfx, int line, int x, int y,
674-
TokenMarker tokenMarker) {
674+
TokenMarkerState tokenMarker) {
675675
textArea.getLineText(currentLineIndex, currentLine);
676676
currentLineTokens = tokenMarker.markTokens(currentLine, currentLineIndex);
677677

0 commit comments

Comments
 (0)