|
Lines 11-17
Link Here
|
| 11 |
import org.eclipse.core.runtime.Preferences; |
11 |
import org.eclipse.core.runtime.Preferences; |
| 12 |
import org.eclipse.jface.text.BadLocationException; |
12 |
import org.eclipse.jface.text.BadLocationException; |
| 13 |
import org.eclipse.jface.text.DocumentCommand; |
13 |
import org.eclipse.jface.text.DocumentCommand; |
|
|
14 |
import org.eclipse.jface.text.IAutoEditStrategy; |
| 14 |
import org.eclipse.jface.text.IDocument; |
15 |
import org.eclipse.jface.text.IDocument; |
|
|
16 |
import org.eclipse.jface.text.IRegion; |
| 17 |
import org.eclipse.ui.IEditorPart; |
| 18 |
import org.eclipse.ui.IWorkbenchPage; |
| 19 |
import org.eclipse.ui.IWorkbenchWindow; |
| 20 |
import org.eclipse.ui.PlatformUI; |
| 21 |
import org.eclipse.ui.texteditor.ITextEditor; |
| 15 |
import org.eclipse.ui.texteditor.ITextEditorExtension3; |
22 |
import org.eclipse.ui.texteditor.ITextEditorExtension3; |
| 16 |
import org.eclipse.wst.css.core.internal.CSSCorePlugin; |
23 |
import org.eclipse.wst.css.core.internal.CSSCorePlugin; |
| 17 |
import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts; |
24 |
import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts; |
|
Lines 21-29
Link Here
|
| 21 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
28 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 22 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
29 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 23 |
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
30 |
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
| 24 |
import org.eclipse.wst.sse.ui.internal.autoedit.BasicAutoEditStrategy; |
|
|
| 25 |
|
31 |
|
| 26 |
public class StructuredAutoEditStrategyCSS extends BasicAutoEditStrategy { |
32 |
public class StructuredAutoEditStrategyCSS implements IAutoEditStrategy { |
| 27 |
protected IStructuredDocument structuredDocument = null; |
33 |
protected IStructuredDocument structuredDocument = null; |
| 28 |
|
34 |
|
| 29 |
class CompoundRegion { |
35 |
class CompoundRegion { |
|
Lines 153-158
Link Here
|
| 153 |
} |
159 |
} |
| 154 |
} |
160 |
} |
| 155 |
// */ |
161 |
// */ |
|
|
162 |
|
| 163 |
// spaces for tab character |
| 164 |
if (command.text != null && command.text.length() > 0 && command.text.charAt(0) == '\t') |
| 165 |
smartInsertForTab(command, document); |
| 156 |
} |
166 |
} |
| 157 |
|
167 |
|
| 158 |
/** |
168 |
/** |
|
Lines 501-521
Link Here
|
| 501 |
|
511 |
|
| 502 |
|
512 |
|
| 503 |
private String getIndentString() { |
513 |
private String getIndentString() { |
| 504 |
String indent = ""; //$NON-NLS-1$ |
514 |
StringBuffer indent = new StringBuffer(); |
| 505 |
|
515 |
|
| 506 |
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); |
516 |
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); |
| 507 |
if (preferences != null) { |
517 |
if (preferences != null) { |
| 508 |
String indentChar = " "; //$NON-NLS-1$ |
518 |
char indentChar = ' '; |
| 509 |
String indentCharPref = preferences.getString(CSSCorePreferenceNames.INDENTATION_CHAR); |
519 |
String indentCharPref = preferences.getString(CSSCorePreferenceNames.INDENTATION_CHAR); |
| 510 |
if (CSSCorePreferenceNames.TAB.equals(indentCharPref)) { |
520 |
if (CSSCorePreferenceNames.TAB.equals(indentCharPref)) { |
| 511 |
indentChar = "\t"; //$NON-NLS-1$ |
521 |
indentChar = '\t'; |
| 512 |
} |
522 |
} |
| 513 |
int indentationWidth = preferences.getInt(CSSCorePreferenceNames.INDENTATION_SIZE); |
523 |
int indentationWidth = preferences.getInt(CSSCorePreferenceNames.INDENTATION_SIZE); |
| 514 |
|
524 |
|
| 515 |
for (int i = 0; i < indentationWidth; i++) { |
525 |
for (int i = 0; i < indentationWidth; i++) { |
| 516 |
indent += indentChar; |
526 |
indent.append(indentChar); |
| 517 |
} |
527 |
} |
| 518 |
} |
528 |
} |
| 519 |
return indent; |
529 |
return indent.toString(); |
|
|
530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Return the active text editor if possible, otherwise the active editor |
| 534 |
* part. |
| 535 |
* |
| 536 |
* @return |
| 537 |
*/ |
| 538 |
private Object getActiveTextEditor() { |
| 539 |
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 540 |
if (window != null) { |
| 541 |
IWorkbenchPage page = window.getActivePage(); |
| 542 |
if (page != null) { |
| 543 |
IEditorPart editor = page.getActiveEditor(); |
| 544 |
if (editor != null) { |
| 545 |
if (editor instanceof ITextEditor) |
| 546 |
return editor; |
| 547 |
ITextEditor textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class); |
| 548 |
if (textEditor != null) |
| 549 |
return textEditor; |
| 550 |
return editor; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
return null; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Insert spaces for tabs |
| 559 |
* |
| 560 |
* @param command |
| 561 |
*/ |
| 562 |
private void smartInsertForTab(DocumentCommand command, IDocument document) { |
| 563 |
// tab key was pressed. now check preferences to see if need to insert |
| 564 |
// spaces instead of tab |
| 565 |
Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); |
| 566 |
if (CSSCorePreferenceNames.SPACE.equals(preferences.getString(CSSCorePreferenceNames.INDENTATION_CHAR))) { |
| 567 |
int indentationWidth = preferences.getInt(CSSCorePreferenceNames.INDENTATION_SIZE); |
| 568 |
|
| 569 |
StringBuffer indent = new StringBuffer(); |
| 570 |
if (indentationWidth != 0) { |
| 571 |
int indentSize = indentationWidth; |
| 572 |
try { |
| 573 |
IRegion firstLine = document.getLineInformationOfOffset(command.offset); |
| 574 |
int offsetInLine = command.offset - firstLine.getOffset(); |
| 575 |
int remainder = offsetInLine % indentationWidth; |
| 576 |
|
| 577 |
indentSize = indentationWidth - remainder; |
| 578 |
} catch (BadLocationException e) { |
| 579 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 580 |
} |
| 581 |
|
| 582 |
for (int i = 0; i < indentSize; i++) |
| 583 |
indent.append(' '); |
| 584 |
} |
| 585 |
|
| 586 |
// replace \t characters with spaces |
| 587 |
command.text = indent.toString(); |
| 588 |
} |
| 520 |
} |
589 |
} |
| 521 |
} |
590 |
} |