Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Caks2016-03-10 16:10:49 +0000
committerChristoph Caks2016-03-10 16:10:49 +0000
commit5ef4a6745e2107eede6e789a625cc108eec53607 (patch)
treec80237d83c566fab2f9cc329f1bcff3271323c89 /bundles
parent330cca3a6aa5df32cea4be1a1f4471e41aa61c6d (diff)
downloadorg.eclipse.efxclipse-5ef4a6745e2107eede6e789a625cc108eec53607.tar.gz
org.eclipse.efxclipse-5ef4a6745e2107eede6e789a625cc108eec53607.tar.xz
org.eclipse.efxclipse-5ef4a6745e2107eede6e789a625cc108eec53607.zip
Bug 489368 - StyledTextArea - Indentation
implemented simple smart indent for '{' '}'
Diffstat (limited to 'bundles')
-rw-r--r--bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/internal/SimpleSmartIndent.java84
-rw-r--r--bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/source/SourceViewer.java4
2 files changed, 88 insertions, 0 deletions
diff --git a/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/internal/SimpleSmartIndent.java b/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/internal/SimpleSmartIndent.java
new file mode 100644
index 000000000..d3b545ba4
--- /dev/null
+++ b/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/internal/SimpleSmartIndent.java
@@ -0,0 +1,84 @@
+package org.eclipse.fx.text.ui.internal;
+
+import org.eclipse.fx.text.ui.TextViewer;
+import org.eclipse.fx.ui.controls.styledtext.ActionEvent;
+import org.eclipse.fx.ui.controls.styledtext.ActionEvent.ActionType;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+
+public class SimpleSmartIndent {
+
+ private TextViewer viewer;
+
+ public SimpleSmartIndent(TextViewer viewer) {
+ this.viewer = viewer;
+ this.viewer.getTextWidget().addEventHandler(ActionEvent.ACTION, this::handle);
+ }
+
+ public void dispose() {
+ this.viewer.getTextWidget().removeEventHandler(ActionEvent.ACTION, this::handle);
+ }
+
+ private int findIndentAt(int index) {
+ String beforeEnter = viewer.getDocument().get().substring(0, index);
+
+ int count = 0;
+ for (int idx = 0; idx < beforeEnter.length(); idx++) {
+ char curChar = beforeEnter.charAt(idx);
+ if (curChar == '{') {
+ count ++;
+ }
+ if (curChar == '}') {
+ count --;
+ }
+ }
+
+ return Math.max(0, count);
+ }
+
+ private void handle(ActionEvent event) {
+
+ if (event.type == ActionType.NEW_LINE) {
+ try {
+ IDocument doc = viewer.getDocument();
+ int caret = viewer.getTextWidget().getCaretOffset();
+
+ int count = findIndentAt(caret);
+
+ int replaceAt = caret;
+ int replaceLen = 0;
+ String replace = "\n";
+ for (int i = 0; i < count; i++) {
+ replace += "\t";
+ }
+
+
+ // endfix
+ int indent = findIndentAt(caret);
+ int lineBegin = doc.getLineInformationOfOffset(caret).getOffset();
+ String before = doc.get().substring(lineBegin, caret);
+ if (before.matches("^\\s*}")) {
+ String tabs = "";
+ for (int i = 0; i < indent; i++) {
+ tabs += "\t";
+ }
+ replaceAt = lineBegin;
+ replaceLen = before.length();
+ replace = tabs + '}' + replace;
+ }
+
+
+ viewer.getDocument().replace(replaceAt, replaceLen, replace);
+ viewer.getTextWidget().setCaretOffset(caret - replaceLen + replace.length());
+ event.consume();
+
+
+ }
+ catch (BadLocationException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+}
diff --git a/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/source/SourceViewer.java b/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/source/SourceViewer.java
index df2604af5..38055949e 100644
--- a/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/source/SourceViewer.java
+++ b/bundles/code/org.eclipse.fx.text.ui/src/org/eclipse/fx/text/ui/source/SourceViewer.java
@@ -23,6 +23,7 @@ import org.eclipse.fx.text.ui.TextViewer;
import org.eclipse.fx.text.ui.contentassist.ContentAssistant;
import org.eclipse.fx.text.ui.contentassist.IContentAssistant;
import org.eclipse.fx.text.ui.internal.AnnotationModelSupport;
+import org.eclipse.fx.text.ui.internal.SimpleSmartIndent;
import org.eclipse.fx.text.ui.internal.WrappedLineRulerAnnotationPresenter;
import org.eclipse.fx.text.ui.internal.WrappedTextAnnotationPresenter;
import org.eclipse.fx.text.ui.presentation.IPresentationReconciler;
@@ -231,6 +232,9 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi
// });
}
+
+ new SimpleSmartIndent(this);
+
}
// private Node annotationFactory(StyledTextLine l) {

Back to the top