Permalink
Browse files

Improve real-time linking performance

This patch tweaks the regular expression to reduce the chance of
creating links that contain the cursor, and it adds code to avoid
reapplying links that already exist.
  • Loading branch information...
1 parent dce03ea commit 5f2a7c885a1b4df4817b8c01671273372730d1e4 @emk committed Aug 29, 2009
Showing with 24 additions and 3 deletions.
  1. +24 −3 src/buglinky/BugLinkyServlet.java
@@ -9,10 +9,19 @@
@SuppressWarnings("serial")
public class BugLinkyServlet extends AbstractRobotServlet {
private static final Logger log = Logger.getLogger(BugLinkyServlet.class.getName());
- private static final Pattern REGEX = Pattern.compile("bug #(\\d+)");
private static final String BUG_URL =
"http://code.google.com/p/google-wave-resources/issues/detail?id=";
+ /**
+ * Regex used to find bug numbers in the text. Note that we require at least
+ * one non-numeric character after the bug number (and not a newline). This ensures
+ * that when the user is adding text at the end of a paragraph, we won't add any links
+ * until the user is safely outside the area that we need to modify. Users making
+ * modifications inside of paragraphs will have to live with minor glitches.
+ */
+ private static final Pattern REGEX =
+ Pattern.compile("(?:bug|issue) #(\\d+)(?!\\d|\\r|\\n)");
+
/** Called when we receive events from the Wave server. */
@Override
public void processEvents(RobotMessageBundle bundle) {
@@ -55,8 +64,20 @@ private void addLinksToBlip(Blip blip) {
while (matcher.find()) {
log.fine("Found a link: " + matcher.group());
Range range = new Range(matcher.start(), matcher.end());
- String bugNumber = matcher.group(1);
- doc.setAnnotation(range, "link/manual", BUG_URL.concat(bugNumber));
+ String url = BUG_URL.concat(matcher.group(1));
+ if (!isAnnotationAlreadyPresent(doc, range, url)) {
+ log.fine("Making new link to " + url);
+ doc.setAnnotation(range, "link/manual", url);
+ }
+ }
+ }
+
+ /** Have we already added this annotation? */
+ private boolean isAnnotationAlreadyPresent(TextView doc, Range range, String url) {
+ for (Annotation annotation : doc.getAnnotations(range, "link/manual")) {
+ if (annotation.getRange().equals(range) && annotation.getValue().equals(url))
+ return true;
}
+ return false;
}
}

0 comments on commit 5f2a7c8

Please sign in to comment.