diff dwtx/jface/text/DefaultIndentLineAutoEditStrategy.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/DefaultIndentLineAutoEditStrategy.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.DefaultIndentLineAutoEditStrategy;
+
+import dwt.dwthelper.utils;
+
+
+/**
+ * This strategy always copies the indentation of the previous line.
+ * <p>
+ * This class is not intended to be subclassed.</p>
+ *
+ * @since 3.1
+ */
+public class DefaultIndentLineAutoEditStrategy : IAutoEditStrategy {
+
+    /**
+     * Creates a new indent line auto edit strategy which can be installed on
+     * text viewers.
+     */
+    public DefaultIndentLineAutoEditStrategy() {
+    }
+
+    /**
+     * Returns the first offset greater than <code>offset</code> and smaller than
+     * <code>end</code> whose character is not a space or tab character. If no such
+     * offset is found, <code>end</code> is returned.
+     *
+     * @param document the document to search in
+     * @param offset the offset at which searching start
+     * @param end the offset at which searching stops
+     * @return the offset in the specified range whose character is not a space or tab
+     * @exception BadLocationException if position is an invalid range in the given document
+     */
+    protected int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {
+        while (offset < end) {
+            char c= document.getChar(offset);
+            if (c !is ' ' && c !is '\t') {
+                return offset;
+            }
+            offset++;
+        }
+        return end;
+    }
+
+    /**
+     * Copies the indentation of the previous line.
+     *
+     * @param d the document to work on
+     * @param c the command to deal with
+     */
+    private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) {
+
+        if (c.offset is -1 || d.getLength() is 0)
+            return;
+
+        try {
+            // find start of line
+            int p= (c.offset is d.getLength() ? c.offset  - 1 : c.offset);
+            IRegion info= d.getLineInformationOfOffset(p);
+            int start= info.getOffset();
+
+            // find white spaces
+            int end= findEndOfWhiteSpace(d, start, c.offset);
+
+            StringBuffer buf= new StringBuffer(c.text);
+            if (end > start) {
+                // append to input
+                buf.append(d.get(start, end - start));
+            }
+
+            c.text= buf.toString();
+
+        } catch (BadLocationException excp) {
+            // stop work
+        }
+    }
+
+    /*
+     * @see dwtx.jface.text.IAutoEditStrategy#customizeDocumentCommand(dwtx.jface.text.IDocument, dwtx.jface.text.DocumentCommand)
+     */
+    public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
+        if (c.length is 0 && c.text !is null && TextUtilities.endsWith(d.getLegalLineDelimiters(), c.text) !is -1)
+            autoIndentAfterNewLine(d, c);
+    }
+}