Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNoopur Gupta2013-02-15 13:36:02 +0000
committerDani Megert2013-02-15 13:36:02 +0000
commitcf0c4beeb2326627bec6196f67c096b928022cf8 (patch)
tree479503df20f44bc6934d29cc71d27437f9b0237e
parent5a3d67c973f51af4cd34e01331afe194ec6532ce (diff)
downloadeclipse.jdt.ui-cf0c4beeb2326627bec6196f67c096b928022cf8.tar.gz
eclipse.jdt.ui-cf0c4beeb2326627bec6196f67c096b928022cf8.tar.xz
eclipse.jdt.ui-cf0c4beeb2326627bec6196f67c096b928022cf8.zip
Fixed bug 395071: [typing] "Automatically close {Braces}" should not putv20130215-133602
the } after the rest of the line
-rw-r--r--org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java185
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java43
2 files changed, 212 insertions, 16 deletions
diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java
index 39d7f9f228..d152cc6fc7 100644
--- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java
+++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JavaAutoIndentStrategyTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2011 IBM Corporation and others.
+ * Copyright (c) 2009, 2013 IBM Corporation 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
@@ -50,6 +50,8 @@ public class JavaAutoIndentStrategyTest extends TestCase implements ILogListener
private Accessor fAccessor;
+ private Accessor fCommandAccessor;
+
private JavaAutoIndentStrategy fJavaAutoIndentStrategy;
public JavaAutoIndentStrategyTest() {
@@ -69,6 +71,7 @@ public class JavaAutoIndentStrategyTest extends TestCase implements ILogListener
fDocumentCommand= new DocumentCommand() {
};
fAccessor= new Accessor(fJavaAutoIndentStrategy, JavaAutoIndentStrategy.class);
+ fCommandAccessor= new Accessor(fDocumentCommand, DocumentCommand.class);
}
private void performPaste() {
@@ -210,6 +213,186 @@ public class JavaAutoIndentStrategyTest extends TestCase implements ILogListener
Assert.assertEquals(result, fDocumentCommand.text);
}
+ private void performSmartIndentAfterNewLine() {
+ fAccessor.invoke("clearCachedValues", null, null);
+ fAccessor.invoke("smartIndentAfterNewLine", new Class[] { IDocument.class, DocumentCommand.class }, new Object[] { fDocument, fDocumentCommand });
+ fCommandAccessor.invoke("execute", new Class[] { IDocument.class }, new Object[] { fDocument });
+ }
+
+ public void testSmartIndentAfterNewLine1() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=29379
+ fDocument.set("main (new String [] {);");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 21;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("main (new String [] {\r\n");
+ buf.append("\t\t\r\n");
+ buf.append("});");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+
+ fDocument.set("main (new String [] {\"a\");");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 24;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf1= new StringBuffer();
+ buf1.append("main (new String [] {\"a\"\r\n");
+ buf1.append("\t\t\r\n");
+ buf1.append("});");
+ Assert.assertEquals(buf1.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine2() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=395071
+ fDocument.set("main (new String [] {\"a\",);");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 25;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("main (new String [] {\"a\",\r\n");
+ buf.append("\t\t\r\n");
+ buf.append("});");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+
+ fDocument.set("main (new String [] {\"a\", );");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 26;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf1= new StringBuffer();
+ buf1.append("main (new String [] {\"a\", \r\n");
+ buf1.append("\t\t\r\n");
+ buf1.append("});");
+ Assert.assertEquals(buf1.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine3() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=395071
+ fDocument.set("main (new String [] {\"a\",\"b\",);");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 29;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("main (new String [] {\"a\",\"b\",\r\n");
+ buf.append("\t\t\r\n");
+ buf.append("});");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine4() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=254704
+ fDocument.set("@NamedQueries({);");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 15;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("@NamedQueries({\r\n");
+ buf.append("\t\r\n");
+ buf.append("});");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=394467
+ fDocument.set("@MesageDriven( activationConfig ={)");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 34;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf1= new StringBuffer();
+ buf1.append("@MesageDriven( activationConfig ={\r\n");
+ buf1.append("\t\t\r\n");
+ buf1.append("})");
+ Assert.assertEquals(buf1.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine5() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=256087
+ fDocument.set("if (false) {return false;");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 12;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("if (false) {\r\n");
+ buf.append("\treturn false;\r\n");
+ buf.append("}");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+
+ fDocument.set("if (false) { return false;");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 13;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf1= new StringBuffer();
+ buf1.append("if (false) { \r\n");
+ buf1.append("\treturn false;\r\n");
+ buf1.append("}");
+ Assert.assertEquals(buf1.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine6() {
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=200015
+ StringBuffer inBuf= new StringBuffer();
+ inBuf.append("enum ReviewResult {\n");
+ inBuf.append(" Good{, Bad\n");
+ inBuf.append("}");
+ fDocument.set(inBuf.toString());
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 29;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("enum ReviewResult {\n");
+ buf.append(" Good{\r\n");
+ buf.append(" \t\n");
+ buf.append(" }, Bad\n");
+ buf.append("}");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine7() {
+ fDocument.set("int[] a= new int[] { ;");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 21;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("int[] a= new int[] { \r\n");
+ buf.append("\t\t\r\n");
+ buf.append("};");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine8() {
+ fDocument.set("String[] strs = {\"a\",\"b\",");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 21;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("String[] strs = {\"a\",\r\n");
+ buf.append("\t\t\"b\",\r\n");
+ buf.append("}");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+ }
+
+ public void testSmartIndentAfterNewLine9() {
+ fDocument.set("{ int a;");
+ fDocumentCommand.doit= true;
+ fDocumentCommand.offset= 1;
+ fDocumentCommand.text= "\r\n";
+ performSmartIndentAfterNewLine();
+ StringBuffer buf= new StringBuffer();
+ buf.append("{\r\n");
+ buf.append("\tint a;\r\n");
+ buf.append("}");
+ Assert.assertEquals(buf.toString(), fDocument.get());
+ }
+
/*
* @see junit.framework.TestCase#setUp()
*/
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java
index c79aa355d8..5e2288f471 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaAutoIndentStrategy.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation 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
@@ -314,10 +314,16 @@ public class JavaAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
c.caretOffset= c.offset + buf.length();
c.shiftsCaret= false;
+ int pos= c.offset - 1;
+ char ch= d.getChar(pos);
+ while (ch == ' ' || ch == '\t') {
+ pos--;
+ ch= d.getChar(pos);
+ }
// copy old content of line behind insertion point to new line
// unless we think we are inserting an anonymous type definition
- if (c.offset == 0 || computeAnonymousPosition(d, c.offset - 1, fPartitioning, lineEnd) == -1) {
+ if (c.offset == 0 || copyContent(d, pos + 1, fPartitioning, lineEnd)) {
if (lineEnd - contentStart > 0) {
c.length= lineEnd - c.offset;
buf.append(d.get(contentStart, lineEnd - contentStart).toCharArray());
@@ -363,18 +369,19 @@ public class JavaAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
}
/**
- * Computes an insert position for an opening brace if <code>offset</code> maps to a position in
- * <code>document</code> with a expression in parenthesis that will take a block after the closing parenthesis.
+ * Checks if it is required to copy the old content of the line after caret position to new line
+ * before inserting the closing brace.
*
* @param document the document being modified
* @param offset the offset of the caret position, relative to the line start.
* @param partitioning the document partitioning
* @param max the max position
- * @return an insert position relative to the line start if <code>line</code> contains a parenthesized expression that can be followed by a block, -1 otherwise
+ * @return <code>true</code> if the old content of the line after caret position has to be
+ * copied to new line before inserting the closing brace, <code>false</code> otherwise
*/
- private static int computeAnonymousPosition(IDocument document, int offset, String partitioning, int max) {
+ private boolean copyContent(IDocument document, int offset, String partitioning, int max) {
// find the opening parenthesis for every closing parenthesis on the current line after offset
- // return the position behind the closing parenthesis if it looks like a method declaration
+ // return true if it looks like a method declaration
// or an expression for an if, while, for, catch statement
JavaHeuristicScanner scanner= new JavaHeuristicScanner(document);
@@ -385,14 +392,21 @@ public class JavaAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
scanTo= length;
int closingParen= findClosingParenToLeft(scanner, pos) - 1;
- boolean hasNewToken= looksLikeAnonymousClassDef(document, partitioning, scanner, pos);
int openingParen= -1;
while (true) {
int startScan= closingParen + 1;
closingParen= scanner.scanForward(startScan, scanTo, ')');
if (closingParen == -1) {
- if (hasNewToken && openingParen != -1)
- return openingParen + 1;
+ if (openingParen != -1)
+ return false;
+ try {
+ int p= findEndOfWhiteSpace(document, pos, scanTo);
+ char ch= document.getChar(p);
+ if (ch == ',' || ch == ';')
+ return false;
+ } catch (BadLocationException e) {
+ // ignore
+ }
break;
}
@@ -407,17 +421,16 @@ public class JavaAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
continue;
if (looksLikeAnonymousClassDef(document, partitioning, scanner, openingParen - 1))
- return closingParen + 1;
-
+ return false;
}
- return -1;
+ return true;
}
/**
* Finds a closing parenthesis to the left of <code>position</code> in document, where that parenthesis is only
* separated by whitespace from <code>position</code>. If no such parenthesis can be found, <code>position</code> is returned.
- *
+ *
* @param scanner the java heuristic scanner set up on the document
* @param position the first character position in <code>document</code> to be considered
* @return the position of a closing parenthesis left to <code>position</code> separated only by whitespace, or <code>position</code> if no parenthesis can be found
@@ -1326,7 +1339,7 @@ public class JavaAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
* Returns the block balance, i.e. zero if the blocks are balanced at <code>offset</code>, a
* negative number if there are more closing than opening braces, and a positive number if there
* are more opening than closing braces.
- *
+ *
* @param document the document
* @param offset the offset
* @param partitioning the partitioning

Back to the top