diff options
5 files changed, 96 insertions, 12 deletions
diff --git a/org.eclipse.ui.console/schema/consolePatternMatchListener.exsd b/org.eclipse.ui.console/schema/consolePatternMatchListener.exsd index f91eee840..cad32af12 100644 --- a/org.eclipse.ui.console/schema/consolePatternMatchListener.exsd +++ b/org.eclipse.ui.console/schema/consolePatternMatchListener.exsd @@ -74,6 +74,13 @@ </documentation> </annotation> </attribute> + <attribute name="matchContext" type="string"> + <annotation> + <documentation> + Allows extension to decide whether a pattern should be matched against the entire document of each line of the document individually. Acceptable values are "line" and "document". If not explicitly specified, "line" matching is assumed. + </documentation> + </annotation> + </attribute> </complexType> </element> diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/IOConsole.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/IOConsole.java index fca1b2662..55c484e1f 100644 --- a/org.eclipse.ui.console/src/org/eclipse/ui/console/IOConsole.java +++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/IOConsole.java @@ -18,6 +18,10 @@ import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadPositionCategoryException; @@ -450,27 +454,68 @@ public class IOConsole extends AbstractConsole implements IDocumentListener { synchronized(patterns) { for (Iterator iter = patterns.iterator(); iter.hasNext();) { CompiledPatternMatchListener pattern = (CompiledPatternMatchListener) iter.next(); - int start = Math.min(pattern.end, event.fOffset); try { - testForMatch(pattern, start); + testForMatch(pattern, event.fOffset); } catch (BadLocationException e) { } } } } - private void testForMatch(CompiledPatternMatchListener compiled, int documentOffset) throws BadLocationException { + private void testForMatch(CompiledPatternMatchListener compiled, int eventOffset) throws BadLocationException { + String matchContext = compiled.listener.getMatchContext(); + if (IPatternMatchListener.LINE_MATCH.equals(matchContext)) { + matchByLine(compiled, eventOffset); + } else { + matchByDocument(compiled, eventOffset); + } + } + private void matchByLine(final CompiledPatternMatchListener compiled, final int eventOffset) throws BadLocationException { IDocument document = getDocument(); - String contents = document.get(documentOffset, document.getLength()-documentOffset); - Matcher matcher = compiled.pattern.matcher(contents); - IPatternMatchListener notifier = compiled.listener; - while(matcher.find()) { - String group = matcher.group(); - int matchOffset = documentOffset + matcher.start(); - notifier.matchFound(new PatternMatchEvent(this, matchOffset, group.length())); - compiled.end = matcher.end() + documentOffset; + final IPatternMatchListener notifier = compiled.listener; + int curLine = document.getLineOfOffset(eventOffset); + int numLines = document.getNumberOfLines(); + for(; curLine<numLines; curLine++) { + final int start = document.getLineOffset(curLine); + final String line = document.get(start, document.getLineLength(curLine)); + Job job = new Job("Pattern Match Job") { //$NON-NLS-1$ + protected IStatus run(IProgressMonitor monitor) { + Matcher matcher = compiled.pattern.matcher(line); + while(matcher.find()) { + String group = matcher.group(); + int matchOffset = start + matcher.start(); + notifier.matchFound(new PatternMatchEvent(IOConsole.this, matchOffset, group.length())); + } + return Status.OK_STATUS; + } + + }; + job.setSystem(true); + job.schedule(); + } } + + private void matchByDocument(final CompiledPatternMatchListener compiled, final int eventOffset) throws BadLocationException { + final int start = Math.min(compiled.end, eventOffset); + IDocument document = getDocument(); + final String contents = document.get(start, document.getLength()-start); + Job job = new Job("Pattern Match Job") { //$NON-NLS-1$ + protected IStatus run(IProgressMonitor monitor) { + Matcher matcher = compiled.pattern.matcher(contents); + IPatternMatchListener notifier = compiled.listener; + while(matcher.find()) { + String group = matcher.group(); + int matchOffset = eventOffset + matcher.start(); + notifier.matchFound(new PatternMatchEvent(IOConsole.this, matchOffset, group.length())); + compiled.end = matcher.end() + start; + } + return Status.OK_STATUS; + } + }; + job.setSystem(true); + job.schedule(); + } private class CompiledPatternMatchListener { Pattern pattern; diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/console/IPatternMatchListener.java b/org.eclipse.ui.console/src/org/eclipse/ui/console/IPatternMatchListener.java index 421413b40..957316410 100644 --- a/org.eclipse.ui.console/src/org/eclipse/ui/console/IPatternMatchListener.java +++ b/org.eclipse.ui.console/src/org/eclipse/ui/console/IPatternMatchListener.java @@ -19,6 +19,8 @@ package org.eclipse.ui.console; * @since 3.1 */ public interface IPatternMatchListener { + public static final String LINE_MATCH = "line"; //$NON-NLS-1$ + public static final String DOCUMENT_MATCH = "document"; //$NON-NLS-1$ /** * Returns the pattern to be used for matching. The pattern is * a string representing a regular expression. @@ -34,6 +36,15 @@ public interface IPatternMatchListener { public int getCompilerFlags(); /** + * Returns the match context for this pattern match listener. Allowable values + * are <code>line</code> and <code>document</code>. + * @return <code>line</code> if pattern should be matched against individual lines + * or <code>document</code> if pattern should be matched against the console's entire + * document. + */ + public String getMatchContext(); + + /** * Notification that a match has been found. * * @param event event describing where the match was found diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListener.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListener.java index b927bc620..a0c2fa9f3 100644 --- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListener.java +++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListener.java @@ -39,7 +39,14 @@ public class PatternMatchListener implements IPatternMatchListener { public int getCompilerFlags() { return fExtension.getCompilerFlags(); } - + + /* (non-Javadoc) + * @see org.eclipse.ui.console.IPatternMatchListener#getMatchContext() + */ + public String getMatchContext() { + return fExtension.getMatchContext(); + } + /* (non-Javadoc) * @see org.eclipse.ui.console.IPatternMatchListener#matchFound(org.eclipse.ui.console.PatternMatchEvent) */ diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListenerExtension.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListenerExtension.java index e63e46dec..a1847ca10 100644 --- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListenerExtension.java +++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/PatternMatchListenerExtension.java @@ -30,6 +30,7 @@ public class PatternMatchListenerExtension implements IPluginContribution { private Expression fEnablementExpression; private String fPattern; private int fFlags = -1; + private String fMatchContext; public PatternMatchListenerExtension(IConfigurationElement extension) { fConfig = extension; @@ -108,6 +109,19 @@ public class PatternMatchListenerExtension implements IPluginContribution { return fFlags; } + /** + * @return + */ + public String getMatchContext() { + if (fMatchContext == null) { + fMatchContext = fConfig.getAttributeAsIs("matchContext"); //$NON-NLS-1$ + if (fMatchContext == null) { + fMatchContext = "line"; //$NON-NLS-1$ + } + } + return fMatchContext; + } + /* (non-Javadoc) * @see org.eclipse.ui.IPluginContribution#getLocalId() */ |