Skip to content

Commit 00fe52d

Browse files
committed
add getTokenMarker(code) for better file type flexibility; also clean up formatting
1 parent 3d0497c commit 00fe52d

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

app/src/processing/app/Mode.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,14 +734,22 @@ public String lookupReference(String keyword) {
734734
}
735735

736736

737-
//public TokenMarker getTokenMarker() throws IOException {
738-
// File keywordsFile = new File(folder, "keywords.txt");
739-
// return new PdeKeywords(keywordsFile);
740-
//}
737+
/**
738+
* Specialized version of getTokenMarker() that can be overridden to
739+
* provide different TokenMarker objects for different file types.
740+
* @since 3.2
741+
* @param code the code for which we need a TokenMarker
742+
*/
743+
public TokenMarker getTokenMarker(SketchCode code) {
744+
return getTokenMarker();
745+
}
746+
747+
741748
public TokenMarker getTokenMarker() {
742749
return tokenMarker;
743750
}
744751

752+
745753
protected TokenMarker createTokenMarker() {
746754
return new PdeKeywords();
747755
}

app/src/processing/app/syntax/KeywordMap.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class KeywordMap {
3232

3333
// A value of 52 will give good performance for most maps.
3434
static private int MAP_LENGTH = 52;
35-
35+
3636
/**
3737
* Creates a new <code>KeywordMap</code>.
3838
* @param ignoreCase True if keys are case insensitive
@@ -84,8 +84,8 @@ public byte lookup(Segment text, int offset, int length, boolean paren) {
8484
}
8585
return Token.NULL;
8686
}
87-
88-
87+
88+
8989
/**
9090
* Checks if a subregion of a <code>Segment</code> is equal to a
9191
* character array.
@@ -98,20 +98,20 @@ static public boolean regionMatches(boolean ignoreCase, Segment text,
9898
int offset, char[] match) {
9999
int length = offset + match.length;
100100
char[] textArray = text.array;
101-
if(length > text.offset + text.count)
101+
if (length > text.offset + text.count) {
102102
return false;
103-
for(int i = offset, j = 0; i < length; i++, j++)
104-
{
105-
char c1 = textArray[i];
106-
char c2 = match[j];
107-
if(ignoreCase)
108-
{
109-
c1 = Character.toUpperCase(c1);
110-
c2 = Character.toUpperCase(c2);
111-
}
112-
if(c1 != c2)
113-
return false;
103+
}
104+
for (int i = offset, j = 0; i < length; i++, j++) {
105+
char c1 = textArray[i];
106+
char c2 = match[j];
107+
if (ignoreCase) {
108+
c1 = Character.toUpperCase(c1);
109+
c2 = Character.toUpperCase(c2);
110+
}
111+
if (c1 != c2) {
112+
return false;
114113
}
114+
}
115115
return true;
116116
}
117117

@@ -127,7 +127,7 @@ public void add(String keyword, byte id, boolean paren) {
127127
map[key] = new Keyword(keyword.toCharArray(), id, map[key]);
128128
}
129129

130-
130+
131131
/**
132132
* Returns true if the keyword map is set to be case insensitive,
133133
* false otherwise.
@@ -136,7 +136,7 @@ public boolean getIgnoreCase() {
136136
return ignoreCase;
137137
}
138138

139-
139+
140140
/**
141141
* Sets if the keyword map should be case insensitive.
142142
* @param ignoreCase True if the keyword map should be case
@@ -146,21 +146,21 @@ public void setIgnoreCase(boolean ignoreCase) {
146146
this.ignoreCase = ignoreCase;
147147
}
148148

149-
149+
150150
protected int getStringMapKey(String s) {
151151
return (Character.toUpperCase(s.charAt(0)) +
152152
Character.toUpperCase(s.charAt(s.length()-1)))
153153
% MAP_LENGTH;
154154
}
155155

156-
156+
157157
protected int getSegmentMapKey(Segment s, int off, int len) {
158158
return (Character.toUpperCase(s.array[off]) +
159159
Character.toUpperCase(s.array[off + len - 1]))
160160
% MAP_LENGTH;
161161
}
162162

163-
163+
164164
// private members
165165
private static class Keyword {
166166
public final char[] keyword;

app/src/processing/app/syntax/TextAreaDefaults.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@
1818
* creating the text area is that this method is faster.
1919
*/
2020
public class TextAreaDefaults {
21-
// For 3.0a6, the InputHandler object was broken out because it has little
22-
// to do with this code and created circuitous connections between classes.
23-
//public InputHandler inputHandler;
24-
2521
public SyntaxDocument document;
2622

2723
public boolean caretVisible;
2824
public boolean caretBlinks;
2925
public boolean blockCaret;
3026
public int electricScroll;
3127

32-
// default/preferred number of rows/cols
28+
// default/preferred number of rows/cols
3329
public int cols;
3430
public int rows;
35-
31+
3632
public SyntaxStyle[] styles;
3733
public Color caretColor;
3834
public Color selectionColor;

app/src/processing/app/ui/Editor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ public void endCompoundEdit() {
18051805
code.setDocument(document);
18061806

18071807
// turn on syntax highlighting
1808-
document.setTokenMarker(mode.getTokenMarker());
1808+
document.setTokenMarker(mode.getTokenMarker(code));
18091809

18101810
// insert the program text into the document object
18111811
try {

todo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ _ allow modes to specify their own base file name
2727
_ need to move "is this a sketch?" handling into Mode
2828
_ favicon handling for p5jsMode
2929
_ move template subst code to Util
30+
_ in Mode: TokenMarker getTokenMarker(SketchCode code)
31+
_ passes through to no args version if not overridden
3032

3133
_ modify line number color when no lines extend that far?
3234
_ https://github.com/processing/processing/pull/4560

0 commit comments

Comments
 (0)