Skip to content

Commit d60c42e

Browse files
committed
Clickable url are now searched with a good regexp and highlighted.
1 parent a6c8753 commit d60c42e

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

app/src/processing/app/syntax/PdeTextAreaDefaults.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public PdeTextAreaDefaults() {
169169
// ??
170170
styles[Token.LABEL] = Theme.getStyle("label");
171171

172+
// http://arduino.cc/
173+
styles[Token.URL] = Theme.getStyle("url");
174+
172175
// + - = /
173176
styles[Token.OPERATOR] = Theme.getStyle("operator");
174177

app/src/processing/app/syntax/SyntaxUtilities.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public static SyntaxStyle[] getDefaultSyntaxStyles()
104104
styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true);
105105
styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
106106
styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
107+
styles[Token.URL] = new SyntaxStyle(Color.blue,true,false);
107108
styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
108109

109110
return styles;
@@ -151,7 +152,7 @@ public static int paintSyntaxLine(Segment line, Token tokens,
151152

152153
line.count = length;
153154
if (id == Token.COMMENT1 || id == Token.COMMENT2)
154-
x = drawTabbedCommentsText(line, x, y, gfx, expander);
155+
x = drawTabbedCommentsText(line, x, y, gfx, expander, styles, styles[id]);
155156
else
156157
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
157158
line.offset += length;
@@ -174,9 +175,7 @@ public static int paintSyntaxLine(Segment line, Token tokens,
174175
* the remaining part of the string.
175176
*/
176177
public static String[] parseCommentUrls(String line) {
177-
// Try to find pattern
178-
Pattern schematics = Pattern.compile("@schematics\\s+([^\\s]+)");
179-
Matcher m = schematics.matcher(line.toString());
178+
Matcher m = urlPattern.matcher(line.toString());
180179
if (!m.find())
181180
return null;
182181

@@ -188,12 +187,21 @@ public static String[] parseCommentUrls(String line) {
188187
return res;
189188
}
190189

190+
static private Pattern urlPattern = Pattern.compile(
191+
"((?:https?|ftp)://" + // ( Protocol
192+
"(?:(?:[\\w_\\-]+:)?[\\w_\\-]+@)?" + // Username and password
193+
"(?:[\\w_\\-]+\\.)+[\\w_\\-]+" + // Domain name
194+
"(?::[0-9]{1,5})?" + // Port
195+
"(?:/[\\w_\\-./?%&=+]*)?)" + // Path )
196+
"(?:\\s|$)"); // whitespace or EOL
197+
191198
public static Segment stringToSegment(String v) {
192199
return new Segment(v.toCharArray(), 0, v.length());
193200
}
194201

195202
private static int drawTabbedCommentsText(Segment line, int x, int y,
196-
Graphics gfx, TabExpander expander) {
203+
Graphics gfx, TabExpander expander, SyntaxStyle[] styles,
204+
SyntaxStyle commentStyle) {
197205

198206
String parse[] = parseCommentUrls(line.toString());
199207
if (parse == null)
@@ -203,14 +211,21 @@ private static int drawTabbedCommentsText(Segment line, int x, int y,
203211
Segment tag = stringToSegment(parse[1]);
204212
Segment post = stringToSegment(parse[2]);
205213

206-
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
214+
if (pre.length()>0)
215+
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
216+
217+
Font f = gfx.getFont();
218+
styles[Token.URL].setGraphicsFlags(gfx, f);
207219
x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0);
208220

209221
// Draw arrow.
210222
FontMetrics metrics = gfx.getFontMetrics();
211223
int h = metrics.getHeight() - 2;
212224
drawArrow(gfx, x, y - h + metrics.getDescent() - 1, h, h);
213-
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
225+
226+
commentStyle.setGraphicsFlags(gfx, f);
227+
if (post.length()>0)
228+
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
214229
return x;
215230
}
216231

app/src/processing/app/syntax/Token.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,22 @@ public class Token
8383
*/
8484
public static final byte OPERATOR = 9;
8585

86+
/**
87+
* URL token id.
88+
*/
89+
public static final byte URL = 10;
90+
8691
/**
8792
* Invalid token id. This can be used to mark invalid
8893
* or incomplete tokens, so the user can easily spot
8994
* syntax errors.
9095
*/
91-
public static final byte INVALID = 10;
96+
public static final byte INVALID = 11;
9297

9398
/**
9499
* The total number of defined token ids.
95100
*/
96-
public static final byte ID_COUNT = 11;
101+
public static final byte ID_COUNT = 12;
97102

98103
/**
99104
* The first id that can be used for internal state

build/shared/lib/theme/theme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ editor.literal1.style = #006699,plain
8383
# p5 built in variables: e.g. mouseX, width, pixels
8484
editor.literal2.style = #006699,plain
8585

86+
# http://arduino.cc/
87+
editor.url.style = #0000ff,italic
88+
8689
# e.g. + - = /
8790
editor.operator.style = #000000,plain
8891

0 commit comments

Comments
 (0)