129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.DefaultIndentLineAutoEditStrategy;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 /**
|
|
19 * This strategy always copies the indentation of the previous line.
|
|
20 * <p>
|
|
21 * This class is not intended to be subclassed.</p>
|
|
22 *
|
|
23 * @since 3.1
|
|
24 */
|
|
25 public class DefaultIndentLineAutoEditStrategy : IAutoEditStrategy {
|
|
26
|
|
27 /**
|
|
28 * Creates a new indent line auto edit strategy which can be installed on
|
|
29 * text viewers.
|
|
30 */
|
|
31 public DefaultIndentLineAutoEditStrategy() {
|
|
32 }
|
|
33
|
|
34 /**
|
|
35 * Returns the first offset greater than <code>offset</code> and smaller than
|
|
36 * <code>end</code> whose character is not a space or tab character. If no such
|
|
37 * offset is found, <code>end</code> is returned.
|
|
38 *
|
|
39 * @param document the document to search in
|
|
40 * @param offset the offset at which searching start
|
|
41 * @param end the offset at which searching stops
|
|
42 * @return the offset in the specified range whose character is not a space or tab
|
|
43 * @exception BadLocationException if position is an invalid range in the given document
|
|
44 */
|
|
45 protected int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {
|
|
46 while (offset < end) {
|
|
47 char c= document.getChar(offset);
|
|
48 if (c !is ' ' && c !is '\t') {
|
|
49 return offset;
|
|
50 }
|
|
51 offset++;
|
|
52 }
|
|
53 return end;
|
|
54 }
|
|
55
|
|
56 /**
|
|
57 * Copies the indentation of the previous line.
|
|
58 *
|
|
59 * @param d the document to work on
|
|
60 * @param c the command to deal with
|
|
61 */
|
|
62 private void autoIndentAfterNewLine(IDocument d, DocumentCommand c) {
|
|
63
|
|
64 if (c.offset is -1 || d.getLength() is 0)
|
|
65 return;
|
|
66
|
|
67 try {
|
|
68 // find start of line
|
|
69 int p= (c.offset is d.getLength() ? c.offset - 1 : c.offset);
|
|
70 IRegion info= d.getLineInformationOfOffset(p);
|
|
71 int start= info.getOffset();
|
|
72
|
|
73 // find white spaces
|
|
74 int end= findEndOfWhiteSpace(d, start, c.offset);
|
|
75
|
|
76 StringBuffer buf= new StringBuffer(c.text);
|
|
77 if (end > start) {
|
|
78 // append to input
|
|
79 buf.append(d.get(start, end - start));
|
|
80 }
|
|
81
|
|
82 c.text= buf.toString();
|
|
83
|
|
84 } catch (BadLocationException excp) {
|
|
85 // stop work
|
|
86 }
|
|
87 }
|
|
88
|
|
89 /*
|
|
90 * @see dwtx.jface.text.IAutoEditStrategy#customizeDocumentCommand(dwtx.jface.text.IDocument, dwtx.jface.text.DocumentCommand)
|
|
91 */
|
|
92 public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
|
|
93 if (c.length is 0 && c.text !is null && TextUtilities.endsWith(d.getLegalLineDelimiters(), c.text) !is -1)
|
|
94 autoIndentAfterNewLine(d, c);
|
|
95 }
|
|
96 }
|