comparison dwtx/jface/text/TabsToSpacesConverter.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
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
1 /*******************************************************************************
2 * Copyright (c) 2007 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.TabsToSpacesConverter;
14
15 import dwt.dwthelper.utils;
16
17
18 /**
19 * Auto edit strategy that converts tabs into spaces.
20 * <p>
21 * Clients usually instantiate and configure this class but
22 * can also extend it in their own subclass.
23 * </p>
24 *
25 * @since 3.3
26 */
27 public class TabsToSpacesConverter : IAutoEditStrategy {
28
29 private int fTabRatio;
30 private ILineTracker fLineTracker;
31
32
33 public void setNumberOfSpacesPerTab(int ratio) {
34 fTabRatio= ratio;
35 }
36
37 public void setLineTracker(ILineTracker lineTracker) {
38 fLineTracker= lineTracker;
39 }
40
41 private int insertTabString(StringBuffer buffer, int offsetInLine) {
42
43 if (fTabRatio is 0)
44 return 0;
45
46 int remainder= offsetInLine % fTabRatio;
47 remainder= fTabRatio - remainder;
48 for (int i= 0; i < remainder; i++)
49 buffer.append(' ');
50 return remainder;
51 }
52
53 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
54 String text= command.text;
55 if (text is null)
56 return;
57
58 int index= text.indexOf('\t');
59 if (index > -1) {
60
61 StringBuffer buffer= new StringBuffer();
62
63 fLineTracker.set(command.text);
64 int lines= fLineTracker.getNumberOfLines();
65
66 try {
67
68 for (int i= 0; i < lines; i++) {
69
70 int offset= fLineTracker.getLineOffset(i);
71 int endOffset= offset + fLineTracker.getLineLength(i);
72 String line= text.substring(offset, endOffset);
73
74 int position= 0;
75 if (i is 0) {
76 IRegion firstLine= document.getLineInformationOfOffset(command.offset);
77 position= command.offset - firstLine.getOffset();
78 }
79
80 int length= line.length();
81 for (int j= 0; j < length; j++) {
82 char c= line.charAt(j);
83 if (c is '\t') {
84 position += insertTabString(buffer, position);
85 } else {
86 buffer.append(c);
87 ++ position;
88 }
89 }
90
91 }
92
93 command.text= buffer.toString();
94
95 } catch (BadLocationException x) {
96 }
97 }
98 }
99 }