Skip to content

Commit

Permalink
Fix for issue #239: enter key after braceless if should cause indent
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 6, 2016
1 parent db2aafe commit 32d4076
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,61 +215,82 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {
'''.stripIndent().replaceAll('\\|', ''))
}

void testIndentAfterIfStatement() {
makeEditor("""\
class Bagaga {
def foo(def a, def b) {
if (a == b)${CARET}
}
}
""".stripIndent())

send('\n')

assertEditorContents("""\
class Bagaga {
def foo(def a, def b) {
if (a == b)
${CARET}
}
}
""".stripIndent())
}

void testGRE757() {
makeEditor('''\
makeEditor("""\
class Bagaga {
def foo(def a, def b) {<***>
def foo(def a, def b) {${CARET}
}
}
'''.stripIndent())
""".stripIndent())

send('\nif (a < b)\n\t')
send('\nif (a < b)\n')

assertEditorContents('''\
assertEditorContents("""\
class Bagaga {
def foo(def a, def b) {
if (a < b)
<***>
${CARET}
}
}
'''.stripIndent())
""".stripIndent())

send('foo()\n')

assertEditorContents('''\
assertEditorContents("""\
class Bagaga {
def foo(def a, def b) {
if (a < b)
foo()
<***>
${CARET}
}
}
'''.stripIndent())
""".stripIndent())

sendBackTab()

assertEditorContents('''\
assertEditorContents("""\
class Bagaga {
def foo(def a, def b) {
if (a < b)
foo()
<***>
${CARET}
}
}
'''.stripIndent())
""".stripIndent())

send('else\n')

assertEditorContents('''\
assertEditorContents("""\
class Bagaga {
def foo(def a, def b) {
if (a < b)
foo()
else
<***>
${CARET}
}
}
'''.stripIndent())
""".stripIndent())
}

void testGRE620() {
Expand Down Expand Up @@ -701,13 +722,13 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1262
void testAutoCloseAfterClosureArgs1() {
makeEditor('''\
makeEditor('''\
def x = { yyy -><***>
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def x = { yyy ->
<***>
}
Expand All @@ -716,15 +737,15 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1262
void testAutoCloseAfterClosureArgs2() {
makeEditor('''\
makeEditor('''\
def xxx() {
def x = { yyy -><***>
}
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def xxx() {
def x = { yyy ->
<***>
Expand All @@ -735,15 +756,15 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1475
void testAutoIndentCurly1() {
makeEditor('''\
makeEditor('''\
def xxx() {
def x = { yyy -><***>}
}
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def xxx() {
def x = { yyy ->
<***>
Expand All @@ -754,15 +775,15 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1475
void testAutoIndentCurly2() {
makeEditor('''\
makeEditor('''\
def xxx() {
def x = { yyy -><***> }
}
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def xxx() {
def x = { yyy ->
<***>
Expand All @@ -773,15 +794,15 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1475
void testAutoIndentCurly3() {
makeEditor('''\
makeEditor('''\
def xxx() {
def x = { yyy -><***> } def foo
}
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def xxx() {
def x = { yyy ->
<***>
Expand All @@ -792,15 +813,15 @@ final class GroovyAutoIndenterTests2 extends GroovyEditorTest {

// GRECLIPSE-1475
void testAutoIndentCurly4() {
makeEditor('''\
makeEditor('''\
def xxx() {
def x = { yyy -><***> )
}
'''.stripIndent())

send('\n')
send('\n')

assertEditorContents('''\
assertEditorContents('''\
def xxx() {
def x = { yyy ->
<***> )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
*/
package org.codehaus.groovy.eclipse.refactoring.formatter;

import static org.codehaus.greclipse.GroovyTokenTypeBridge.EOF;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.LBRACK;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.LCURLY;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.LPAREN;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.NLS;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.RBRACK;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.RCURLY;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.RPAREN;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.STRING_CTOR_END;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.STRING_CTOR_START;
import static org.codehaus.greclipse.GroovyTokenTypeBridge.*;
import groovyjarjarantlr.Token;

import java.util.HashMap;
Expand Down Expand Up @@ -211,15 +202,27 @@ public int computeIndentAfterNewline(IDocument d, int offset) throws BadLocation
List<Token> tokens = getTokens(d, d.getLineOffset(line), offset);

int indentLevel = simpleComputeNextLineIndentLevel(orgIndentLevel, tokens);
if (indentLevel < orgIndentLevel) {
// Jumping back from indentation is more complex.
Token lastToken = tokens.get(tokens.size() - 1);
if (lastToken.getType() == GroovyTokenTypeBridge.NLS)
lastToken = getTokenBefore(d, lastToken);
if (isCloserOfPair(lastToken)) {

Token lastToken = tokens.get(tokens.size() - 1);
while (lastToken.getType() == GroovyTokenTypeBridge.NLS) {
lastToken = getTokenBefore(d, lastToken);
}
if (isCloserOfPair(lastToken)) {
if (indentLevel < orgIndentLevel) {
// Jumping back from indentation is more complex.
// A somewhat better strategy for newline after closing
// brackets, parens or braces.
indentLevel = getIndentLevelForCloserPair(d, lastToken);
} else if (indentLevel == orgIndentLevel) {
// if balanced line starts with a conditional keyword, indent the new line
switch (tokens.get(0).getType()) {
case LITERAL_if:
case LITERAL_else:
case LITERAL_while:
case LITERAL_switch:
case LITERAL_for:
indentLevel = getIndentLevelForCloserPair(d, lastToken) + getPrefs().getIndentationSize();
}
}
}
return indentLevel;
Expand Down Expand Up @@ -345,14 +348,13 @@ private int getIndentLevelForCloserPair(IDocument d, Token closer) {
try {
while (closeCount != 0 && (token = scanner.getLastTokenBefore(token)) != null) {
if (token.getType() == openerType)
closeCount--;
closeCount -= 1;
if (token.getType() == closerType)
closeCount++;
closeCount += 1;
}
return getIndentLevel(d, scanner.getOffset(token));
} catch (BadLocationException e) {
// Something went wrong. Just use indent level of the line itself as
// a "sensible" default.
// Something went wrong. Just use indent level of the line itself as a "sensible" default.
try {
return getIndentLevel(d, scanner.getOffset(closer));
} catch (BadLocationException e1) {
Expand Down Expand Up @@ -519,15 +521,15 @@ public void refreshPrefs() {
/**
* Compute an adjusted indentation level for the next line, based on
* some simple heuristics about the types of tokens seen only in the
* previous
* line.
* previous line.
*/
private int simpleComputeNextLineIndentLevel(int indentLevel, List<Token> tokens) {
int adjust = getOpenVersusCloseBalance(tokens);
if (adjust > 0)
if (adjust > 0) {
indentLevel += getPrefs().getIndentationSize();
else if (adjust < 0)
indentLevel = indentLevel - getPrefs().getIndentationSize();
} else if (adjust < 0) {
indentLevel -= getPrefs().getIndentationSize();
}
return indentLevel;
}

Expand Down

0 comments on commit 32d4076

Please sign in to comment.