Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kurtakov2017-05-25 05:14:32 -0400
committerAlexander Kurtakov2017-05-25 05:38:29 -0400
commitcad72c665066fb81457d7cee9ebea570c6d8790e (patch)
tree1c143e8194557973606921fe3bec063558a976a3
parentb9f24472776632b129b4c60c50eabac81bd86a0e (diff)
downloadorg.eclipse.dltk.sh-cad72c665066fb81457d7cee9ebea570c6d8790e.tar.gz
org.eclipse.dltk.sh-cad72c665066fb81457d7cee9ebea570c6d8790e.tar.xz
org.eclipse.dltk.sh-cad72c665066fb81457d7cee9ebea570c6d8790e.zip
Bug 493958 - ShellEd incorrectly auto-prepends&indents "#"
Do not autoindent whitespace chars and do not round to tab size indentations. Change-Id: I648c412d28b4c8d5eecd2e8b7cf85a18b66c57f9 Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
-rw-r--r--plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/ScriptAutoIndentStrategy.java67
1 files changed, 32 insertions, 35 deletions
diff --git a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/ScriptAutoIndentStrategy.java b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/ScriptAutoIndentStrategy.java
index b41a691..e6815fd 100644
--- a/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/ScriptAutoIndentStrategy.java
+++ b/plugins/org.eclipse.dltk.sh.ui/src/org/eclipse/dltk/sh/internal/ui/text/ScriptAutoIndentStrategy.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2016 Red Hat Inc. and others.
+ * Copyright (c) 2009, 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -48,8 +48,8 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
}
/**
- * This implementation attempts to auto-indent and auto-unindent after
- * keywords that require it.
+ * This implementation attempts to auto-indent and auto-unindent after keywords
+ * that require it.
*/
@Override
public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
@@ -64,8 +64,7 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
/**
* Set the indent of a new line when the user hits carriage return. The new
* indent will either be the same as the previous line or incremented if the
- * user has hit carriage return on a line that contains a incrementing
- * keyword.
+ * user has hit carriage return on a line that contains a incrementing keyword.
*
* @param document
* the document being parsed
@@ -77,7 +76,7 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
return;
}
try {
- StringBuffer buf = new StringBuffer(c.text);
+ StringBuilder buf = new StringBuilder(c.text);
int p = c.offset == document.getLength() ? c.offset - 1 : c.offset;
int line = document.getLineOfOffset(p);
int start = document.getLineOffset(line);
@@ -85,13 +84,13 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
buf.append(generateIndentation(getIndentOfLine(document, line), bracketCount <= 0 ? 0 : 1));
c.text = buf.toString();
} catch (BadLocationException x) {
- x.printStackTrace();
+ // ignore
}
}
/**
- * Set the indent of the current line when the user hits a key. The indent
- * will either be unchanged or decremented if the user types
+ * Set the indent of the current line when the user hits a key. The indent will
+ * either be unchanged or decremented if the user types
*
* @param document
* the document being parsed
@@ -99,11 +98,11 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
* the command being performed
*/
protected void smartIndentAfterKeypress(IDocument document, DocumentCommand c) {
- if ((c.offset == -1) || (document.getLength() == 0)) {
+ if ((c.offset == -1) || (document.getLength() == 0) || c.text.trim().isEmpty()) {
return;
}
try {
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
int p = c.offset == document.getLength() ? c.offset - 1 : c.offset;
int line = document.getLineOfOffset(p);
int start = document.getLineOffset(line);
@@ -118,14 +117,14 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
c.offset = start;
c.text = buf.toString();
} catch (BadLocationException x) {
- x.printStackTrace();
+ // ignore
}
}
/**
- * Returns the first offset greater than <code>offset</code> and smaller
- * than <code>end</code> whose character is not a space or tab character. If
- * no such offset is found, <code>end</code> is returned.
+ * Returns the first offset greater than <code>offset</code> and smaller than
+ * <code>end</code> whose character is not a space or tab character. If no such
+ * offset is found, <code>end</code> is returned.
*
* @param document
* the document to search in
@@ -133,8 +132,8 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
* the offset at which searching start
* @param end
* the offset at which searching stops
- * @return the offset in the specified range whose character is not a space
- * or tab
+ * @return the offset in the specified range whose character is not a space or
+ * tab
* @exception BadLocationException
* if offset is an invalid position in the given document
*/
@@ -170,23 +169,23 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
}
/**
- * Returns the bracket count of a section of text. The count is incremented
- * when an opening bracket is encountered and decremented when a closing
- * bracket is encountered.
+ * Returns the bracket count of a section of text. The count is incremented when
+ * an opening bracket is encountered and decremented when a closing bracket is
+ * encountered.
*
* @param document
* - the document being parsed
* @param command
- * - if not null, the inserted text specified by the command will
- * be taken into account as if it were part of the document
+ * - if not null, the inserted text specified by the command will be
+ * taken into account as if it were part of the document
* @param start
* - the start position for the search
* @param end
* - the end position for the search
* @param ignoreInflexions
* - whether or not to ignore inflexions in the count
- * @return the resulting bracket count, a positive value means we've
- * encountered more opening than closing brackets
+ * @return the resulting bracket count, a positive value means we've encountered
+ * more opening than closing brackets
*/
private int getBracketCount(IDocument document, DocumentCommand command, int start, int end,
boolean ignoreInflexions) {
@@ -220,8 +219,8 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
}
/**
- * Calculate the indentation needed for a new line based on the contents of
- * the previous line.
+ * Calculate the indentation needed for a new line based on the contents of the
+ * previous line.
*
* @param previous
* a string containing the indentation of the previous line
@@ -247,14 +246,13 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
// Trim previous indentation back to nearest tab stop
int minLength = Math.min(endLength, preLength);
- int maxCopyLength = tabSize > 0 ? minLength - (minLength % tabSize) : minLength; // maximum
// indent
// to
// copy
- String indent = stripExtraChars(previous, maxCopyLength, tabSize);
+ String indent = stripExtraChars(previous, minLength, tabSize);
// Add additional indentation
- int missing = endLength - maxCopyLength;
+ int missing = endLength - minLength;
final int tabs, spaces;
if (CodeFormatterConstants.SPACE.equals(tabChar)) {
// Each indent is a number of spaces equal to indent size
@@ -284,8 +282,8 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
}
/**
- * Computes the length of a an indentation, counting a tab character as the
- * size until the next tab stop and every other character as one.
+ * Computes the length of a an indentation, counting a tab character as the size
+ * until the next tab stop and every other character as one.
*
* @param indent
* the string containing the indentation to measure
@@ -300,8 +298,7 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
switch (ch) {
case '\t':
if (tabSize > 0) {
- int reminder = length % tabSize;
- length += tabSize - reminder;
+ length += tabSize;
}
break;
case ' ':
@@ -313,8 +310,8 @@ public class ScriptAutoIndentStrategy implements IAutoEditStrategy {
}
/**
- * Strips any characters off the end of an indentation that exceed a
- * specified maximum visual indentation length.
+ * Strips any characters off the end of an indentation that exceed a specified
+ * maximum visual indentation length.
*
* @param indent
* the string containing the indentation to measure

Back to the top