Permalink
Browse files

add test and fix to not push dedenting past matching indent level

  • Loading branch information...
1 parent 1883bc4 commit 0708668c20960f947f870a2c9c28323a7af7cacd sgtcoolguy committed with ashebanow Apr 5, 2010
@@ -44,7 +44,7 @@ public void customizeDocumentCommand(IDocument document, DocumentCommand command
}
else if (shouldAutoDedent() && !isLineDelimiter(document, command.text))
{
- autoDedent(document, command);
+ autoDedent(document, command);
}
}
}
@@ -81,17 +81,21 @@ private void autoDedent(IDocument d, DocumentCommand c)
{
return;
}
- // Textmate just assumes we subtract one indent level
+ // Textmate just assumes we subtract one indent level, unless the matching level it should be at is >=
+ // what we're at now!
String decreasedIndent = ""; //$NON-NLS-1$
+ // if we subtract one indent level and it is shorter than matching indent, then don't subtract!
+ String matchingIndent = findCorrectIndentString(d, lineNumber, currentLineIndent);
+
String indentString = getIndentString();
if (currentLineIndent.length() > indentString.length())
{
decreasedIndent = currentLineIndent
.substring(0, currentLineIndent.length() - indentString.length());
}
- if (decreasedIndent.equals(currentLineIndent)) // indent level hasn't changed, just pass newline and
- // same indent level along
+ // if indent level hasn't changed, or shouldn't be moved back, return early!
+ if (decreasedIndent.equals(currentLineIndent) || decreasedIndent.length() < matchingIndent.length())
{
return;
}
@@ -44,6 +44,33 @@ protected String getIndentString()
assertEquals(19, command.offset);
}
+ public void testDedentWontPushPastMatchingIndentLevel()
+ {
+ RubyRegexpAutoIndentStrategy strategy = new AlwaysMatchRubyRegexpAutoIndentStrategy()
+ {
+ @Override
+ protected String getIndentString()
+ {
+ return " ";
+ }
+
+ @Override
+ protected String findCorrectIndentString(IDocument d, int lineNumber, String currentLineIndent)
+ throws BadLocationException
+ {
+ return " ";
+ }
+ };
+ IDocument document = new Document(" if a.nil?\n a=1\n els");
+ DocumentCommand command = createTextCommand(23, "e");
+ strategy.customizeDocumentCommand(document, command);
+
+ assertEquals(" if a.nil?\n a=1\n els", document.get());
+ assertTrue(command.doit);
+ assertEquals("e", command.text);
+ assertEquals(23, command.offset);
+ }
+
public void testDoesntDedentWhenMultipleCharsArePasted()
{
RubyRegexpAutoIndentStrategy strategy = new AlwaysMatchRubyRegexpAutoIndentStrategy()

0 comments on commit 0708668

Please sign in to comment.