Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 105912 | Differences between
and this patch

Collapse All | Expand All

(-)AutoEditStrategyForTabs.java (-20 / +138 lines)
Lines 14-23 Link Here
14
14
15
import org.eclipse.core.runtime.Preferences;
15
import org.eclipse.core.runtime.Preferences;
16
import org.eclipse.jface.text.BadLocationException;
16
import org.eclipse.jface.text.BadLocationException;
17
import org.eclipse.jface.text.ConfigurableLineTracker;
17
import org.eclipse.jface.text.DocumentCommand;
18
import org.eclipse.jface.text.DocumentCommand;
18
import org.eclipse.jface.text.IAutoEditStrategy;
19
import org.eclipse.jface.text.IAutoEditStrategy;
19
import org.eclipse.jface.text.IDocument;
20
import org.eclipse.jface.text.IDocument;
21
import org.eclipse.jface.text.ILineTracker;
20
import org.eclipse.jface.text.IRegion;
22
import org.eclipse.jface.text.IRegion;
23
import org.eclipse.jface.text.TextUtilities;
24
import org.eclipse.ui.IEditorPart;
25
import org.eclipse.ui.IWorkbenchPage;
26
import org.eclipse.ui.IWorkbenchWindow;
27
import org.eclipse.ui.PlatformUI;
28
import org.eclipse.ui.texteditor.ITextEditor;
29
import org.eclipse.ui.texteditor.ITextEditorExtension3;
21
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
30
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
22
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
31
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
23
import org.eclipse.wst.html.ui.internal.Logger;
32
import org.eclipse.wst.html.ui.internal.Logger;
Lines 26-35 Link Here
26
 * AutoEditStrategy to handle characters inserted when Tab key is pressed
35
 * AutoEditStrategy to handle characters inserted when Tab key is pressed
27
 */
36
 */
28
public class AutoEditStrategyForTabs implements IAutoEditStrategy {
37
public class AutoEditStrategyForTabs implements IAutoEditStrategy {
29
38
	private final String TAB_CHARACTER = "\t";	//$NON-NLS-1$
39
	
30
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
40
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
41
		// if not in smart insert mode just ignore
42
		if (!isSmartInsertMode())
43
			return;
44
		
31
		// spaces for tab character
45
		// spaces for tab character
32
		if (command.text != null && command.text.length() > 0 && command.text.charAt(0) == '\t')
46
		if (command.length == 0 && command.text != null && command.text.length() > 0 && command.text.indexOf(TAB_CHARACTER) != -1)
33
			smartInsertForTab(command, document);
47
			smartInsertForTab(command, document);
34
	}
48
	}
35
49
Lines 41-69 Link Here
41
	private void smartInsertForTab(DocumentCommand command, IDocument document) {
55
	private void smartInsertForTab(DocumentCommand command, IDocument document) {
42
		// tab key was pressed. now check preferences to see if need to insert
56
		// tab key was pressed. now check preferences to see if need to insert
43
		// spaces instead of tab
57
		// spaces instead of tab
44
		Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
58
		int indentationWidth = getIndentationWidth();
45
		if (HTMLCorePreferenceNames.SPACE.equals(preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR))) {
59
		if (indentationWidth > -1) {
46
			int indentationWidth = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);
60
			String originalText = command.text;
61
			StringBuffer newText = new StringBuffer(originalText);
62
63
			// determine where in line this command begins
64
			int lineOffset = -1;
65
			try {
66
				IRegion lineInfo = document.getLineInformationOfOffset(command.offset);
67
				lineOffset = command.offset - lineInfo.getOffset();
68
			} catch (BadLocationException e) {
69
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
70
			}
71
72
			ILineTracker lineTracker = getLineTracker(document, originalText);
47
73
48
			StringBuffer indent = new StringBuffer();
74
			int startIndex = 0;
49
			if (indentationWidth != 0) {
75
			int index = newText.indexOf(TAB_CHARACTER);
50
				int indentSize = indentationWidth;
76
			while (index != -1) {
51
				try {
77
				String indent = getIndentString(indentationWidth, lineOffset, lineTracker, index);
52
					IRegion firstLine = document.getLineInformationOfOffset(command.offset);
78
53
					int offsetInLine = command.offset - firstLine.getOffset();
79
				// replace \t character with spaces
54
					int remainder = offsetInLine % indentationWidth;
80
				newText.replace(index, index + 1, indent);
55
81
				if (lineTracker != null) {
56
					indentSize = indentationWidth - remainder;
82
					try {
57
				} catch (BadLocationException e) {
83
						lineTracker.replace(index, 1, indent);
58
					Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
84
					} catch (BadLocationException e) {
85
						// if something goes wrong with replacing text, just
86
						// reset to current string
87
						lineTracker.set(newText.toString());
88
						Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
89
					}
59
				}
90
				}
60
91
61
				for (int i = 0; i < indentSize; i++)
92
				startIndex = index + indent.length();
62
					indent.append(' ');
93
				index = newText.indexOf(TAB_CHARACTER, startIndex);
63
			}
94
			}
95
			command.text = newText.toString();
96
		}
97
	}
64
98
65
			// replace \t characters with spaces
99
	/**
66
			command.text = indent.toString();
100
	 * Calculate number of spaces for next tab stop
101
	 */
102
	private String getIndentString(int indentationWidth, int lineOffset, ILineTracker lineTracker, int index) {
103
		int indentSize = indentationWidth;
104
		int offsetInLine = -1;
105
		if (lineTracker != null) {
106
			try {
107
				IRegion lineInfo = lineTracker.getLineInformationOfOffset(index);
108
				if (lineInfo.getOffset() == 0 && lineOffset > -1)
109
					offsetInLine = lineOffset + index;
110
				else
111
					offsetInLine = index - lineInfo.getOffset();
112
			} catch (BadLocationException e) {
113
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
114
			}
115
		} else {
116
			if (lineOffset > -1) {
117
				offsetInLine = lineOffset + index;
118
			}
67
		}
119
		}
120
		if (offsetInLine > -1) {
121
			int remainder = offsetInLine % indentationWidth;
122
			indentSize = indentationWidth - remainder;
123
		}
124
125
		StringBuffer indent = new StringBuffer();
126
		for (int i = 0; i < indentSize; i++)
127
			indent.append(' ');
128
		return indent.toString();
129
	}
130
131
	/**
132
	 * Set up a line tracker for text within command if text is multi-line
133
	 */
134
	private ILineTracker getLineTracker(IDocument document, String originalText) {
135
		ConfigurableLineTracker lineTracker = null;
136
		int[] delims = TextUtilities.indexOf(document.getLegalLineDelimiters(), originalText, 0);
137
		if (delims[0] != -1 || delims[1] != -1) {
138
			lineTracker = new ConfigurableLineTracker(document.getLegalLineDelimiters());
139
			lineTracker.set(originalText);
140
		}
141
		return lineTracker;
142
	}
143
	
144
	/**
145
	 * Return true if active editor is in smart insert mode, false otherwise
146
	 * 
147
	 * @return
148
	 */
149
	private boolean isSmartInsertMode() {
150
		boolean isSmartInsertMode = false;
151
		
152
		ITextEditor textEditor = null;
153
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
154
		if (window != null) {
155
			IWorkbenchPage page = window.getActivePage();
156
			if (page != null) {
157
				IEditorPart editor = page.getActiveEditor();
158
				if (editor != null) {
159
					if (editor instanceof ITextEditor)
160
						textEditor = (ITextEditor)editor;
161
					else
162
						textEditor = (ITextEditor)editor.getAdapter(ITextEditor.class);
163
				}
164
			}
165
		}
166
		
167
		// check if smart insert mode
168
		if (textEditor instanceof ITextEditorExtension3 && ((ITextEditorExtension3) textEditor).getInsertMode() == ITextEditorExtension3.SMART_INSERT)
169
			isSmartInsertMode = true;
170
		return isSmartInsertMode;
171
	}
172
	
173
	/**
174
	 * Returns indentation width if using spaces for indentation, -1 otherwise
175
	 * 
176
	 * @return
177
	 */
178
	private int getIndentationWidth() {
179
		int width = -1;
180
181
		Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
182
		if (HTMLCorePreferenceNames.SPACE.equals(preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR)))
183
			width = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);
184
185
		return width;
68
	}
186
	}
69
}
187
}

Return to bug 105912