Clickable url are now searched with a good regexp and highlighted.

This commit is contained in:
Cristian Maglie 2011-08-19 22:32:34 +02:00
parent a6c8753a80
commit d60c42e5ed
4 changed files with 35 additions and 9 deletions

View File

@ -169,6 +169,9 @@ public class PdeTextAreaDefaults extends TextAreaDefaults {
// ?? // ??
styles[Token.LABEL] = Theme.getStyle("label"); styles[Token.LABEL] = Theme.getStyle("label");
// http://arduino.cc/
styles[Token.URL] = Theme.getStyle("url");
// + - = / // + - = /
styles[Token.OPERATOR] = Theme.getStyle("operator"); styles[Token.OPERATOR] = Theme.getStyle("operator");

View File

@ -104,6 +104,7 @@ public class SyntaxUtilities
styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true); styles[Token.LITERAL2] = new SyntaxStyle(new Color(0x650099),false,true);
styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true); styles[Token.LABEL] = new SyntaxStyle(new Color(0x990033),false,true);
styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true); styles[Token.OPERATOR] = new SyntaxStyle(Color.black,false,true);
styles[Token.URL] = new SyntaxStyle(Color.blue,true,false);
styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true); styles[Token.INVALID] = new SyntaxStyle(Color.red,false,true);
return styles; return styles;
@ -151,7 +152,7 @@ public class SyntaxUtilities
line.count = length; line.count = length;
if (id == Token.COMMENT1 || id == Token.COMMENT2) if (id == Token.COMMENT1 || id == Token.COMMENT2)
x = drawTabbedCommentsText(line, x, y, gfx, expander); x = drawTabbedCommentsText(line, x, y, gfx, expander, styles, styles[id]);
else else
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0); x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
line.offset += length; line.offset += length;
@ -174,9 +175,7 @@ public class SyntaxUtilities
* the remaining part of the string. * the remaining part of the string.
*/ */
public static String[] parseCommentUrls(String line) { public static String[] parseCommentUrls(String line) {
// Try to find pattern Matcher m = urlPattern.matcher(line.toString());
Pattern schematics = Pattern.compile("@schematics\\s+([^\\s]+)");
Matcher m = schematics.matcher(line.toString());
if (!m.find()) if (!m.find())
return null; return null;
@ -188,12 +187,21 @@ public class SyntaxUtilities
return res; return res;
} }
static private Pattern urlPattern = Pattern.compile(
"((?:https?|ftp)://" + // ( Protocol
"(?:(?:[\\w_\\-]+:)?[\\w_\\-]+@)?" + // Username and password
"(?:[\\w_\\-]+\\.)+[\\w_\\-]+" + // Domain name
"(?::[0-9]{1,5})?" + // Port
"(?:/[\\w_\\-./?%&=+]*)?)" + // Path )
"(?:\\s|$)"); // whitespace or EOL
public static Segment stringToSegment(String v) { public static Segment stringToSegment(String v) {
return new Segment(v.toCharArray(), 0, v.length()); return new Segment(v.toCharArray(), 0, v.length());
} }
private static int drawTabbedCommentsText(Segment line, int x, int y, private static int drawTabbedCommentsText(Segment line, int x, int y,
Graphics gfx, TabExpander expander) { Graphics gfx, TabExpander expander, SyntaxStyle[] styles,
SyntaxStyle commentStyle) {
String parse[] = parseCommentUrls(line.toString()); String parse[] = parseCommentUrls(line.toString());
if (parse == null) if (parse == null)
@ -203,14 +211,21 @@ public class SyntaxUtilities
Segment tag = stringToSegment(parse[1]); Segment tag = stringToSegment(parse[1]);
Segment post = stringToSegment(parse[2]); Segment post = stringToSegment(parse[2]);
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0); if (pre.length()>0)
x = Utilities.drawTabbedText(pre, x, y, gfx, expander, 0);
Font f = gfx.getFont();
styles[Token.URL].setGraphicsFlags(gfx, f);
x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0); x = Utilities.drawTabbedText(tag, x, y, gfx, expander, 0);
// Draw arrow. // Draw arrow.
FontMetrics metrics = gfx.getFontMetrics(); FontMetrics metrics = gfx.getFontMetrics();
int h = metrics.getHeight() - 2; int h = metrics.getHeight() - 2;
drawArrow(gfx, x, y - h + metrics.getDescent() - 1, h, h); drawArrow(gfx, x, y - h + metrics.getDescent() - 1, h, h);
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
commentStyle.setGraphicsFlags(gfx, f);
if (post.length()>0)
x = Utilities.drawTabbedText(post, x, y, gfx, expander, 0);
return x; return x;
} }

View File

@ -83,17 +83,22 @@ public class Token
*/ */
public static final byte OPERATOR = 9; public static final byte OPERATOR = 9;
/**
* URL token id.
*/
public static final byte URL = 10;
/** /**
* Invalid token id. This can be used to mark invalid * Invalid token id. This can be used to mark invalid
* or incomplete tokens, so the user can easily spot * or incomplete tokens, so the user can easily spot
* syntax errors. * syntax errors.
*/ */
public static final byte INVALID = 10; public static final byte INVALID = 11;
/** /**
* The total number of defined token ids. * The total number of defined token ids.
*/ */
public static final byte ID_COUNT = 11; public static final byte ID_COUNT = 12;
/** /**
* The first id that can be used for internal state * The first id that can be used for internal state

View File

@ -83,6 +83,9 @@ editor.literal1.style = #006699,plain
# p5 built in variables: e.g. mouseX, width, pixels # p5 built in variables: e.g. mouseX, width, pixels
editor.literal2.style = #006699,plain editor.literal2.style = #006699,plain
# http://arduino.cc/
editor.url.style = #0000ff,italic
# e.g. + - = / # e.g. + - = /
editor.operator.style = #000000,plain editor.operator.style = #000000,plain