Bug 520842 - As soon as the 'Indentation - Tab policy' is changed to
'Spaces' no more Tabs are possible

Takes into account the current offset when generating indentation.

Change-Id: I5c9a8b1fb837bf2014bfa1caf048c3d6da508473
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
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 19cb4fb..16ea5b7 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
@@ -85,7 +85,9 @@
 			int line = document.getLineOfOffset(p);
 			int start = document.getLineOffset(line);
 			int bracketCount = getBracketCount(document, null, start, c.offset, true);
-			buf.append(generateIndentation(getIndentOfLine(document, line), bracketCount <= 0 ? 0 : 1));
+			String indentOfLine = getIndentOfLine(document, line, p);
+			String indentation = generateIndentation(indentOfLine, bracketCount <= 0 ? 0 : 1);
+			buf.append(indentation);
 			c.text = buf.toString();
 		} catch (BadLocationException x) {
 			// ignore
@@ -107,13 +109,13 @@
 		}
 		try {
 			StringBuilder buf = new StringBuilder();
-			int p = c.offset == document.getLength() ? c.offset - 1 : c.offset;
+			int p = c.offset;
 			int line = document.getLineOfOffset(p);
 			int start = document.getLineOffset(line);
 			int whiteEnd = findEndOfWhiteSpace(document, start, c.offset);
 
 			int bracketCount = getBracketCount(document, c, start, c.offset, false);
-			buf.append(generateIndentation(getIndentOfLine(document, line), bracketCount >= 0 ? 0 : -1));
+			buf.append(generateIndentation(getIndentOfLine(document, line, p), bracketCount >= 0 ? 0 : -1));
 			buf.append(document.get(whiteEnd, c.offset - whiteEnd));
 			buf.append(c.text);
 			// Alter the command
@@ -161,11 +163,10 @@
 	 *            - the line number being searched
 	 * @return the string containing the indentation from the specified line
 	 */
-	private static String getIndentOfLine(IDocument document, int line) throws BadLocationException {
+	private static String getIndentOfLine(IDocument document, int line, int offset) throws BadLocationException {
 		if (line > -1) {
 			int start = document.getLineOffset(line);
-			int end = start + document.getLineLength(line);
-			int whiteend = findEndOfWhiteSpace(document, start, end);
+			int whiteend = findEndOfWhiteSpace(document, start, offset);
 			return document.get(start, whiteend - start);
 		} else {
 			return "";