Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Maetzel2004-06-18 18:54:44 +0000
committerKai Maetzel2004-06-18 18:54:44 +0000
commit396e1a550da9397beab7d153e0c3c5d53e2f50ec (patch)
treeddf8d4a580ecf46b8d82bcc1547d3aff8d983926
parent344b112de4adfc25510996b989626e0731d9f8bf (diff)
downloadeclipse.platform.text-396e1a550da9397beab7d153e0c3c5d53e2f50ec.tar.gz
eclipse.platform.text-396e1a550da9397beab7d153e0c3c5d53e2f50ec.tar.xz
eclipse.platform.text-396e1a550da9397beab7d153e0c3c5d53e2f50ec.zip
#66157
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java4
-rw-r--r--org.eclipse.text.tests/projection/org/eclipse/text/tests/ProjectionDocumentTest.java51
-rw-r--r--org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java58
-rw-r--r--org.eclipse.text/projection/org/eclipse/jface/text/projection/SegmentUpdater.java2
4 files changed, 96 insertions, 19 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
index 70b2844c4..a3c40a260 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
@@ -1876,7 +1876,9 @@ public class TextViewer extends Viewer implements
if (fTextWidget != null) {
Point p= fTextWidget.getSelectionRange();
- return widgetSelection2ModelSelection(p);
+ p= widgetSelection2ModelSelection(p);
+ if (p != null)
+ return p;
}
return new Point(-1, -1);
diff --git a/org.eclipse.text.tests/projection/org/eclipse/text/tests/ProjectionDocumentTest.java b/org.eclipse.text.tests/projection/org/eclipse/text/tests/ProjectionDocumentTest.java
index 19fb7a047..3944f8f93 100644
--- a/org.eclipse.text.tests/projection/org/eclipse/text/tests/ProjectionDocumentTest.java
+++ b/org.eclipse.text.tests/projection/org/eclipse/text/tests/ProjectionDocumentTest.java
@@ -202,7 +202,7 @@ public class ProjectionDocumentTest extends TestCase {
Position previous= null;
for (int i= 0; i < segmentation.length; i++) {
- assertFalse(segmentation[i].getLength() == 0);
+ assertFalse(segmentation.length > 1 && segmentation[i].getLength() == 0);
if (previous != null)
assertTrue(previous.getOffset() + previous.getLength() == segmentation[i].getOffset());
previous= segmentation[i];
@@ -223,7 +223,7 @@ public class ProjectionDocumentTest extends TestCase {
Fragment fragment= (Fragment) fragmention[i];
assertTrue(fragment == segment.fragment);
assertTrue(segment == fragment.segment);
- assertFalse(fragment.getLength() == 0);
+ assertFalse(segmentation.length > 1 && fragment.getLength() == 0);
assertTrue(fragment.length == segment.length);
if (previous != null)
assertFalse(previous.getOffset() + previous.getLength() == fragment.getOffset());
@@ -1844,4 +1844,51 @@ public class ProjectionDocumentTest extends TestCase {
};
assertFragmentation(expected);
}
+
+ public void test28_a() {
+ // delete slave content and check fragmentation, need to keep a single fragment as anchor
+ createProjectionB();
+ try {
+ fSlaveDocument.replace(0, fSlaveDocument.getLength(), null);
+ } catch (BadLocationException e) {
+ assertTrue(false);
+ }
+ assertSlaveContents("");
+ StringBuffer buffer= new StringBuffer(getOriginalMasterContents());
+ buffer.delete(20, 160);
+ assertMasterContents(buffer.toString());
+
+ Position[] expected= new Position[] {
+ new Position(20, 0)
+ };
+ assertFragmentation(expected);
+ }
+
+ public void test28_b() {
+ // test step wise version of the complete replace
+ // delete whole content of slave, followed by inserting text
+
+ createProjectionB();
+ try {
+ fSlaveDocument.replace(0, fSlaveDocument.getLength(), null);
+ } catch (BadLocationException e) {
+ assertTrue(false);
+ }
+
+ try {
+ fSlaveDocument.replace(0, 0, "~~~~~");
+ } catch (BadLocationException e) {
+ assertTrue(false);
+ }
+
+ Position[] expected= new Position[] {
+ new Position(20, 5)
+ };
+ assertFragmentation(expected);
+
+ assertSlaveContents("~~~~~");
+ StringBuffer buffer= new StringBuffer(getOriginalMasterContents());
+ buffer.replace(20, 160, "~~~~~");
+ assertMasterContents(buffer.toString());
+ }
}
diff --git a/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java b/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java
index 2a38c4d17..c69e66ed5 100644
--- a/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java
+++ b/org.eclipse.text/projection/org/eclipse/jface/text/projection/ProjectionDocument.java
@@ -648,15 +648,17 @@ public class ProjectionDocument extends AbstractDocument {
} else if (fMapping.getImageLength() == 0 && masterEvent.getLength() == 0) {
- // there is no segment in this projection document, thus one must be created
- // need to bypass the usual infrastructure as the new segment/fragment would be of length 0 and thus the segmentation be not well formed
-
- try {
- Fragment fragment= new Fragment(0, 0);
- fMasterDocument.addPosition(fFragmentsCategory, fragment);
- createSegmentFor(fragment, 0);
- } catch (BadPositionCategoryException x) {
- internalError();
+ Position[] fragments= getFragments();
+ if (fragments.length == 0) {
+ // there is no segment in this projection document, thus one must be created
+ // need to bypass the usual infrastructure as the new segment/fragment would be of length 0 and thus the segmentation be not well formed
+ try {
+ Fragment fragment= new Fragment(0, 0);
+ fMasterDocument.addPosition(fFragmentsCategory, fragment);
+ createSegmentFor(fragment, 0);
+ } catch (BadPositionCategoryException x) {
+ internalError();
+ }
}
}
@@ -704,7 +706,7 @@ public class ProjectionDocument extends AbstractDocument {
} catch (BadLocationException e) {
internalError();
}
- } else if (ensureWellFormedSegmentation())
+ } else if (ensureWellFormedSegmentation(masterEvent.getOffset()))
fMapping.projectionChanged();
}
}
@@ -739,16 +741,28 @@ public class ProjectionDocument extends AbstractDocument {
*/
protected void updateDocumentStructures(DocumentEvent event) {
super.updateDocumentStructures(event);
- ensureWellFormedSegmentation();
+ ensureWellFormedSegmentation(computeAnchor(event));
fMapping.projectionChanged();
}
- private boolean ensureWellFormedSegmentation() {
+ private int computeAnchor(DocumentEvent event) {
+ if (event instanceof ProjectionDocumentEvent) {
+ ProjectionDocumentEvent slave= (ProjectionDocumentEvent) event;
+ if (ProjectionDocumentEvent.CONTENT_CHANGE == slave.getChangeType()) {
+ DocumentEvent master= slave.getMasterEvent();
+ if (master != null)
+ return master.getOffset();
+ }
+ }
+ return -1;
+ }
+
+ private boolean ensureWellFormedSegmentation(int anchorOffset) {
boolean changed= false;
Position[] segments= getSegments();
for (int i= 0; i < segments.length; i++) {
Segment segment= (Segment) segments[i];
- if (segment.isDeleted()) {
+ if (segment.isDeleted() || segment.getLength() == 0) {
try {
removePosition(fSegmentsCategory, segment);
fMasterDocument.removePosition(fFragmentsCategory, segment.fragment);
@@ -758,7 +772,7 @@ public class ProjectionDocument extends AbstractDocument {
}
} else if (i < segments.length - 1) {
Segment next= (Segment) segments[i + 1];
- if (next.isDeleted())
+ if (next.isDeleted() || next.getLength() == 0)
continue;
Fragment fragment= segment.fragment;
if (fragment.getOffset() + fragment.getLength() == next.fragment.getOffset()) {
@@ -769,6 +783,22 @@ public class ProjectionDocument extends AbstractDocument {
}
}
}
+
+ if (changed && anchorOffset != -1) {
+ Position[] changedSegments= getSegments();
+ if (changedSegments == null || changedSegments.length == 0) {
+ Fragment fragment= new Fragment(anchorOffset, 0);
+ try {
+ fMasterDocument.addPosition(fFragmentsCategory, fragment);
+ createSegmentFor(fragment, 0);
+ } catch (BadLocationException e) {
+ internalError();
+ } catch (BadPositionCategoryException e) {
+ internalError();
+ }
+ }
+ }
+
return changed;
}
diff --git a/org.eclipse.text/projection/org/eclipse/jface/text/projection/SegmentUpdater.java b/org.eclipse.text/projection/org/eclipse/jface/text/projection/SegmentUpdater.java
index 1142a7d69..e73f778b7 100644
--- a/org.eclipse.text/projection/org/eclipse/jface/text/projection/SegmentUpdater.java
+++ b/org.eclipse.text/projection/org/eclipse/jface/text/projection/SegmentUpdater.java
@@ -74,8 +74,6 @@ class SegmentUpdater extends DefaultPositionUpdater {
if (notDeleted())
adaptToReplace();
- if (fPosition.getLength() == 0)
- fPosition.isDeleted= true;
}
} catch (BadPositionCategoryException x) {

Back to the top