remove duplication of split() in SplitBlockElementHandler

The SplitBlockElementHandler uses the split() method instead of
reimplementing split().

Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SimpleEditingTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SimpleEditingTest.java
index daee403..1907c93 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SimpleEditingTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/widget/L2SimpleEditingTest.java
@@ -464,6 +464,23 @@
 	}

 

 	@Test

+	public void givenElementWithMultipleOccurrence_whenCaretRightAfterStartIndex_shouldSplit() throws Exception {

+		final IElement para = widget.insertElement(PARA);

+		widget.insertText("12345");

+		widget.moveTo(para.getStartOffset() + 1);

+		widget.split();

+

+		final List<? extends INode> children = rootElement.children().asList();

+		assertEquals("two para elements", 2, children.size());

+		assertTrue("original para element", children.get(0) instanceof IParent);

+		assertTrue("splitted para element", children.get(1) instanceof IParent);

+		assertEquals("first line element", PARA, ((IElement) children.get(0)).getQualifiedName());

+		assertEquals("second line element", PARA, ((IElement) children.get(0)).getQualifiedName());

+		assertEquals("first line", "", children.get(0).getText());

+		assertEquals("second line", "12345", children.get(1).getText());

+	}

+

+	@Test

 	public void givenElementWithMultipleOccurrenceAndInlineElement_whenCaretAtInlineStartOffset_shouldSplit() throws Exception {

 		widget.insertElement(PARA);

 		widget.insertText("12");

diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/undo/DeleteEdit.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/undo/DeleteEdit.java
index 9b7e890..35345a6 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/undo/DeleteEdit.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/undo/DeleteEdit.java
@@ -34,7 +34,7 @@
 			document.insertFragment(range.getStartOffset(), fragment);

 			fragment = null;

 		} catch (final DocumentValidationException ex) {

-			throw new CannotUndoException();

+			throw new CannotUndoException(ex);

 		}

 	}

 

@@ -44,7 +44,7 @@
 			fragment = document.getFragment(range);

 			document.delete(range);

 		} catch (final DocumentValidationException ex) {

-			throw new CannotRedoException();

+			throw new CannotRedoException(ex);

 		}

 	}

 

diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/BaseVexWidget.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/BaseVexWidget.java
index c64ff1c..5b970ee 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/BaseVexWidget.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/BaseVexWidget.java
@@ -1486,12 +1486,11 @@
 			return true;
 		}
 
-		final INode node = document.getNodeForInsertionAt(getCaretOffset());
-		if (!Filters.elements().matches(node)) {
+		if (!Filters.elements().matches(currentNode)) {
 			return false;
 		}
 
-		final IElement element = (IElement) node;
+		final IElement element = (IElement) currentNode;
 		final IElement parent = element.getParentElement();
 		if (parent == null) {
 			return false;
@@ -1512,11 +1511,10 @@
 			throw new ReadOnlyException("Cannot split, because the editor is read-only.");
 		}
 
-		final INode node = document.getNodeForInsertionAt(getCaretOffset());
-		if (!Filters.elements().matches(node)) {
+		if (!Filters.elements().matches(currentNode)) {
 			throw new DocumentValidationException("Can only split elements.");
 		}
-		final IElement element = (IElement) node;
+		final IElement element = (IElement) currentNode;
 
 		final long start = System.currentTimeMillis();
 		boolean success = false;
diff --git a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/handlers/SplitBlockElementHandler.java b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/handlers/SplitBlockElementHandler.java
index b641b61..246f4a9 100644
--- a/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/handlers/SplitBlockElementHandler.java
+++ b/org.eclipse.vex.ui/src/org/eclipse/vex/ui/internal/handlers/SplitBlockElementHandler.java
@@ -11,18 +11,9 @@
  *******************************************************************************/
 package org.eclipse.vex.ui.internal.handlers;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.vex.core.internal.VEXCorePlugin;
-import org.eclipse.vex.core.internal.css.Styles;
-import org.eclipse.vex.core.internal.dom.CopyOfElement;
 import org.eclipse.vex.core.internal.widget.IVexWidget;
 import org.eclipse.vex.core.internal.widget.swt.VexWidget;
-import org.eclipse.vex.core.provisional.dom.IDocument;
-import org.eclipse.vex.core.provisional.dom.IDocumentFragment;
-import org.eclipse.vex.core.provisional.dom.IElement;
 import org.eclipse.vex.core.provisional.dom.INode;
 
 /**
@@ -35,19 +26,11 @@
 
 	@Override
 	public void execute(final VexWidget widget) throws ExecutionException {
-		IElement element = widget.getCurrentElement();
-		if (element == null) {
-			return; // we are not in an element, so we bail out here
+		if (!widget.canSplit()) {
+			return;
 		}
-		Styles styles = widget.getStyleSheet().getStyles(element);
-		while (!styles.isBlock()) {
-			element = element.getParentElement();
-			if (element == null || element.getParent() instanceof IDocument) {
-				return; // we reached the root element which cannot be split 
-			}
-			styles = widget.getStyleSheet().getStyles(element);
-		}
-		splitElement(widget, element);
+
+		splitElement(widget, widget.getCurrentNode());
 	}
 
 	/**
@@ -59,55 +42,13 @@
 	 *            Node to be split.
 	 */
 	protected void splitElement(final IVexWidget vexWidget, final INode node) {
-
 		vexWidget.doWork(new Runnable() {
 			public void run() {
-
-				long start = 0;
-				if (VEXCorePlugin.getInstance().isDebugging()) {
-					start = System.currentTimeMillis();
-				}
-
 				final boolean isPreformatted = vexWidget.getWhitespacePolicy().isPre(node);
 				if (isPreformatted) {
 					vexWidget.insertText("\n");
 				} else {
-					// There may be a number of child elements below the given
-					// element. We cut out the tails of each of these elements
-					// and put them in a list of fragments to be reconstructed
-					// when
-					// we clone the element.
-					final List<IElement> children = new ArrayList<IElement>();
-					final List<IDocumentFragment> fragments = new ArrayList<IDocumentFragment>();
-					IElement child = vexWidget.getCurrentElement();
-					while (true) {
-						children.add(child);
-						vexWidget.moveTo(child.getEndOffset(), true);
-						fragments.add(vexWidget.getSelectedFragment());
-						vexWidget.deleteSelection();
-						vexWidget.moveTo(child.getEndOffset() + 1);
-						if (child == node) {
-							break;
-						}
-						child = child.getParentElement();
-					}
-
-					for (int i = children.size() - 1; i >= 0; i--) {
-						child = children.get(i);
-						final IDocumentFragment fragment = fragments.get(i);
-						final IElement newChild = vexWidget.insertElement(child.getQualifiedName());
-						newChild.accept(new CopyOfElement(child));
-						final int offset = vexWidget.getCaretOffset();
-						if (fragment != null) {
-							vexWidget.insertFragment(fragment);
-						}
-						vexWidget.moveTo(offset);
-					}
-				}
-
-				if (VEXCorePlugin.getInstance().isDebugging()) {
-					final long end = System.currentTimeMillis();
-					System.out.println("split() took " + (end - start) + "ms");
+					vexWidget.split();
 				}
 			}
 		});