Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move regex kludge from BugLinkAnnotator -> Annotator

This kludge really should be part of the abstract class, so that other
subclasses will eventually be able to reuse it.
  • Loading branch information...
commit a45fba9b67bb807ebc9b06f8b28e2c9ea8790279 1 parent 30c13d6
@emk authored
Showing with 49 additions and 18 deletions.
  1. +47 −9 src/buglinky/Annotator.java
  2. +2 −9 src/buglinky/BugLinkAnnotator.java
View
56 src/buglinky/Annotator.java
@@ -31,20 +31,15 @@
private static final Logger LOG =
Logger.getLogger(Annotator.class.getName());
- /** Return a regular expression matching the text we want to process. */
- protected abstract Pattern getPattern();
-
- /** Process a regular expression match. */
- protected abstract void processMatch(TextView doc, Range range,
- Matcher match);
-
- /** Add links to the specified blip. */
+ /**
+ * Annotate the specified blip.
+ */
public void processBlip(Blip blip) {
LOG.fine("Annotating blip " + blip.getBlipId());
// Adapted from http://senikk.com/min-f%C3%B8rste-google-wave-robot,
// a robot which links to @names on Twitter.
TextView doc = blip.getDocument();
- Matcher matcher = getPattern().matcher(doc.getText());
+ Matcher matcher = getCompiledPattern().matcher(doc.getText());
while (matcher.find()) {
LOG.fine("Found text to annotate: " + matcher.group());
Range range = new Range(matcher.start(), matcher.end());
@@ -53,6 +48,49 @@ public void processBlip(Blip blip) {
}
/**
+ * Return a regular expression matching the text we want to process.
+ */
+ protected abstract String getPattern();
+
+ /**
+ * Take our simple pattern, add some kludges, and compile it.
+ */
+ private Pattern getCompiledPattern() {
+ // KLUDGE - Try to avoid annotating text while the user's caret is
+ // still inside the annotation. For example, imagine that the user
+ // types:
+ //
+ // bug #12|
+ //
+ // ...where "|" represents the cursor. We could immediately annotate
+ // this with a link:
+ //
+ // [bug #12|]
+ //
+ // ...but this will tend to make a mess when the user keeps typing:
+ //
+ // [bug #12 is very annoying|]
+ //
+ // Instead, we require at least one non-newline character to appear
+ // after the match before we try to annotate it. (Note that this hack
+ // won't work anywhere but at the end of a paragraph. Users making
+ // modifications inside of paragraphs will have to live with minor
+ // glitches until the Wave API improves.)
+ //
+ // To do this, we use a zero-width negative lookahead pattern. But we
+ // don't want to use the last character that would normally be matched
+ // by getPattern as our negative lookahead, so we use a possessive
+ // qualifier to avoid backtracking.
+ return Pattern.compile("(?:" + getPattern() + "){1}+(?!\\r|\\n)");
+ }
+
+ /**
+ * Process a regular expression match.
+ */
+ protected abstract void processMatch(TextView doc, Range range,
+ Matcher match);
+
+ /**
* Add an annotation if it isn't already present.
*
* The Wave Robot API does not currently filter out duplicate annotation
View
11 src/buglinky/BugLinkAnnotator.java
@@ -16,7 +16,6 @@
package buglinky;
import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import com.google.wave.api.Range;
import com.google.wave.api.TextView;
@@ -32,14 +31,8 @@ public BugLinkAnnotator(String bugUrl) {
}
/** Return a regular expression matching the text we want to process. */
- protected Pattern getPattern() {
- // 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.
- return Pattern.compile("(?:bug|issue) #(\\d+)(?!\\d|\\r|\\n)");
+ protected String getPattern() {
+ return "(?:bug|issue) #(\\d+)";
}
/** Process a regular expression match. */
Please sign in to comment.
Something went wrong with that request. Please try again.