[287326] XML editor hangs the Eclipse UI
diff --git a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/text/BasicStructuredDocumentRegion.java b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/text/BasicStructuredDocumentRegion.java
index b85593d..8174a5e 100644
--- a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/text/BasicStructuredDocumentRegion.java
+++ b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/text/BasicStructuredDocumentRegion.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2001, 2008 IBM Corporation and others.
+ * Copyright (c) 2001, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -202,16 +202,27 @@
* The parameter offset refers to the overall offset in the document.
*/
public ITextRegion getRegionAtCharacterOffset(int offset) {
- ITextRegion result = null;
if (_getRegions() != null) {
+ int thisStartOffset = getStartOffset();
+ if (offset < thisStartOffset)
+ return null;
+ int thisEndOffset = getStartOffset() + getLength();
+ if (offset > thisEndOffset)
+ return null;
// transform the requested offset to the "scale" that
// regions are stored in, which are all relative to the
// start point.
// int transformedOffset = offset - getStartOffset();
//
- int length = getRegions().size();
- for (int i = 0; i < length; i++) {
- ITextRegion region = getRegions().get(i);
+ ITextRegionList regions = getRegions();
+ int length = regions.size();
+ int low = 0;
+ int high = length;
+ int mid = 0;
+ // Binary search for the region
+ while (low < high) {
+ mid = low + ((high - low) >> 1);
+ ITextRegion region = regions.get(mid);
if (Debug.debugStructuredDocument) {
System.out.println("region(s) in IStructuredDocumentRegion::getRegionAtCharacterOffset: " + region); //$NON-NLS-1$
System.out.println(" requested offset: " + offset); //$NON-NLS-1$
@@ -223,13 +234,16 @@
System.out.println(" region class: " + region.getClass()); //$NON-NLS-1$
}
- if ((getStartOffset(region) <= offset) && (offset < getEndOffset(region))) {
- result = region;
- break;
- }
+ // Region is before this one
+ if (offset < region.getStart() + thisStartOffset)
+ high = mid;
+ else if (offset > (region.getEnd() + thisStartOffset - 1))
+ low = mid + 1;
+ else
+ return region;
}
}
- return result;
+ return null;
}
public ITextRegionList getRegions() {