Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 4ba3a6c9a08b47fd0e1435ea0f0028d0a692b3c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *   IBM - Initial API and implementation
 *   Jens Lukowski/Innoopract - initial renaming/restructuring
 * 
 */
package org.eclipse.wst.html.ui.internal.autoedit;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ConfigurableLineTracker;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ILineTracker;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension3;
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
import org.eclipse.wst.html.ui.internal.Logger;

/**
 * AutoEditStrategy to handle characters inserted when Tab key is pressed
 */
public class AutoEditStrategyForTabs implements IAutoEditStrategy {
	private final String TAB_CHARACTER = "\t";	//$NON-NLS-1$
	
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
		// if not in smart insert mode just ignore
		if (!isSmartInsertMode())
			return;
		
		// spaces for tab character
		if (command.length == 0 && command.text != null && command.text.length() > 0 && command.text.indexOf(TAB_CHARACTER) != -1)
			smartInsertForTab(command, document);
	}

	/**
	 * Insert spaces for tabs
	 * 
	 * @param command
	 */
	private void smartInsertForTab(DocumentCommand command, IDocument document) {
		// tab key was pressed. now check preferences to see if need to insert
		// spaces instead of tab
		int indentationWidth = getIndentationWidth();
		if (indentationWidth > -1) {
			String originalText = command.text;
			StringBuffer newText = new StringBuffer(originalText);

			// determine where in line this command begins
			int lineOffset = -1;
			try {
				IRegion lineInfo = document.getLineInformationOfOffset(command.offset);
				lineOffset = command.offset - lineInfo.getOffset();
			} catch (BadLocationException e) {
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
			}

			ILineTracker lineTracker = getLineTracker(document, originalText);

			int startIndex = 0;
			int index = newText.indexOf(TAB_CHARACTER);
			while (index != -1) {
				String indent = getIndentString(indentationWidth, lineOffset, lineTracker, index);

				// replace \t character with spaces
				newText.replace(index, index + 1, indent);
				if (lineTracker != null) {
					try {
						lineTracker.replace(index, 1, indent);
					} catch (BadLocationException e) {
						// if something goes wrong with replacing text, just
						// reset to current string
						lineTracker.set(newText.toString());
						Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
					}
				}

				startIndex = index + indent.length();
				index = newText.indexOf(TAB_CHARACTER, startIndex);
			}
			command.text = newText.toString();
		}
	}

	/**
	 * Calculate number of spaces for next tab stop
	 */
	private String getIndentString(int indentationWidth, int lineOffset, ILineTracker lineTracker, int index) {
		int indentSize = indentationWidth;
		int offsetInLine = -1;
		if (lineTracker != null) {
			try {
				IRegion lineInfo = lineTracker.getLineInformationOfOffset(index);
				if (lineInfo.getOffset() == 0 && lineOffset > -1)
					offsetInLine = lineOffset + index;
				else
					offsetInLine = index - lineInfo.getOffset();
			} catch (BadLocationException e) {
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
			}
		} else {
			if (lineOffset > -1) {
				offsetInLine = lineOffset + index;
			}
		}
		if (offsetInLine > -1 && indentationWidth > 0) {
			int remainder = offsetInLine % indentationWidth;
			indentSize = indentationWidth - remainder;
		}

		StringBuffer indent = new StringBuffer();
		for (int i = 0; i < indentSize; i++)
			indent.append(' ');
		return indent.toString();
	}

	/**
	 * Set up a line tracker for text within command if text is multi-line
	 */
	private ILineTracker getLineTracker(IDocument document, String originalText) {
		ConfigurableLineTracker lineTracker = null;
		int[] delims = TextUtilities.indexOf(document.getLegalLineDelimiters(), originalText, 0);
		if (delims[0] != -1 || delims[1] != -1) {
			lineTracker = new ConfigurableLineTracker(document.getLegalLineDelimiters());
			lineTracker.set(originalText);
		}
		return lineTracker;
	}
	
	/**
	 * Return true if active editor is in smart insert mode, false otherwise
	 * 
	 * @return
	 */
	private boolean isSmartInsertMode() {
		boolean isSmartInsertMode = false;
		
		ITextEditor textEditor = null;
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IWorkbenchPage page = window.getActivePage();
			if (page != null) {
				IEditorPart editor = page.getActiveEditor();
				if (editor != null) {
					if (editor instanceof ITextEditor)
						textEditor = (ITextEditor)editor;
					else
						textEditor = (ITextEditor)editor.getAdapter(ITextEditor.class);
				}
			}
		}
		
		// check if smart insert mode
		if (textEditor instanceof ITextEditorExtension3 && ((ITextEditorExtension3) textEditor).getInsertMode() == ITextEditorExtension3.SMART_INSERT)
			isSmartInsertMode = true;
		return isSmartInsertMode;
	}
	
	/**
	 * Returns indentation width if using spaces for indentation, -1 otherwise
	 * 
	 * @return
	 */
	private int getIndentationWidth() {
		int width = -1;

		Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
		if (HTMLCorePreferenceNames.SPACE.equals(preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR)))
			width = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);

		return width;
	}
}

Back to the top