|
Lines 14-35
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.jst.jsp.ui.internal.Logger; |
| 25 |
import org.eclipse.ui.IEditorPart; |
| 26 |
import org.eclipse.ui.IWorkbenchPage; |
| 27 |
import org.eclipse.ui.IWorkbenchWindow; |
| 28 |
import org.eclipse.ui.PlatformUI; |
| 29 |
import org.eclipse.ui.texteditor.ITextEditor; |
| 30 |
import org.eclipse.ui.texteditor.ITextEditorExtension3; |
| 21 |
import org.eclipse.wst.html.core.internal.HTMLCorePlugin; |
31 |
import org.eclipse.wst.html.core.internal.HTMLCorePlugin; |
| 22 |
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames; |
32 |
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames; |
| 23 |
import org.eclipse.wst.html.ui.internal.Logger; |
|
|
| 24 |
|
33 |
|
| 25 |
/** |
34 |
/** |
| 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 { |
|
|
38 |
private final String TAB_CHARACTER = "\t"; //$NON-NLS-1$ |
| 29 |
|
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 |
} |
| 47 |
|
71 |
|
| 48 |
StringBuffer indent = new StringBuffer(); |
72 |
ILineTracker lineTracker = getLineTracker(document, originalText); |
| 49 |
if (indentationWidth != 0) { |
73 |
|
| 50 |
int indentSize = indentationWidth; |
74 |
int startIndex = 0; |
| 51 |
try { |
75 |
int index = newText.indexOf(TAB_CHARACTER); |
| 52 |
IRegion firstLine = document.getLineInformationOfOffset(command.offset); |
76 |
while (index != -1) { |
| 53 |
int offsetInLine = command.offset - firstLine.getOffset(); |
77 |
String indent = getIndentString(indentationWidth, lineOffset, lineTracker, index); |
| 54 |
int remainder = offsetInLine % indentationWidth; |
78 |
|
| 55 |
|
79 |
// replace \t character with spaces |
| 56 |
indentSize = indentationWidth - remainder; |
80 |
newText.replace(index, index + 1, indent); |
| 57 |
} catch (BadLocationException e) { |
81 |
if (lineTracker != null) { |
| 58 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
82 |
try { |
|
|
83 |
lineTracker.replace(index, 1, indent); |
| 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); |
|
|
94 |
} |
| 95 |
command.text = newText.toString(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 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; |
| 63 |
} |
118 |
} |
|
|
119 |
} |
| 120 |
if (offsetInLine > -1) { |
| 121 |
int remainder = offsetInLine % indentationWidth; |
| 122 |
indentSize = indentationWidth - remainder; |
| 123 |
} |
| 64 |
|
124 |
|
| 65 |
// replace \t characters with spaces |
125 |
StringBuffer indent = new StringBuffer(); |
| 66 |
command.text = indent.toString(); |
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); |
| 67 |
} |
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 |
} |