This is an automated email from the ASF dual-hosted git repository.

sdedic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 9cac272  [NetBeans-778] Formatting issue with var declaration 
statement (#568)
9cac272 is described below

commit 9cac2724c8610d556d8c01681543201f76df7103
Author: Vikas Prabhakar <vikas.prabha...@oracle.com>
AuthorDate: Thu Jun 14 14:44:14 2018 +0530

    [NetBeans-778] Formatting issue with var declaration statement (#568)
    
    [NetBeans-778] Formatting issue with var declaration statement
---
 .../modules/java/source/save/Reformatter.java      | 10 +++-
 .../modules/java/source/save/FormatingTest.java    | 58 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git 
a/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java 
b/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java
index 20af393..5b359c5 100644
--- 
a/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java
+++ 
b/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java
@@ -1196,9 +1196,15 @@ public class Reformatter implements ReformatTask {
                 } else {
                     if (!insideForTryOrCatch)
                         continuationIndent = true;
-                    if (node.getType() == null || scan(node.getType(), p)) {
-                        if (node.getType() != null) {
+                    if (node.getType() == null || tokens.token().id() == 
JavaTokenId.VAR || scan(node.getType(), p)) {
+                        if (node.getType() != null && tokens.token().id() != 
JavaTokenId.VAR) {
                             spaces(1, fieldGroup);
+                        } else {
+                            if (tokens.token().id() == JavaTokenId.VAR) {
+                                //Add space after 'var' token
+                                addDiff(new Diff(tokens.offset() + 3, 
tokens.offset() + 3, " "));
+                                tokens.moveNext();
+                            }
                         }
                         if (!ERROR.contentEquals(node.getName()))
                             accept(IDENTIFIER, UNDERSCORE);
diff --git 
a/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java
 
b/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java
index 59e8969..cda1c9f 100644
--- 
a/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java
+++ 
b/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java
@@ -4457,6 +4457,64 @@ public class FormatingTest extends NbTestCase {
         reformat(doc, content, golden);
     }
 
+    public void testForVar1() throws Exception {
+        testFile = new File(getWorkDir(), "Test.java");
+        TestUtilities.copyStringToFile(testFile, "");
+        FileObject testSourceFO = FileUtil.toFileObject(testFile);
+        DataObject testSourceDO = DataObject.find(testSourceFO);
+        EditorCookie ec = (EditorCookie) 
testSourceDO.getCookie(EditorCookie.class);
+        String oldLevel = JavaSourceTest.SourceLevelQueryImpl.sourceLevel;
+        JavaSourceTest.SourceLevelQueryImpl.sourceLevel = "1.10";
+        final Document doc = ec.openDocument();
+        doc.putProperty(Language.class, JavaTokenId.language());
+        String content
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public static void main(String[] args) {\n"
+                + "            var    v    =   10; \n"
+                + "    }\n"
+                + "}\n";
+
+        String golden
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public static void main(String[] args) {\n"
+                + "        var v = 10;\n"
+                + "    }\n"
+                + "}\n";
+        reformat(doc, content, golden);
+        JavaSourceTest.SourceLevelQueryImpl.sourceLevel = oldLevel;
+    }
+
+    public void testForVar2() throws Exception {
+        testFile = new File(getWorkDir(), "Test.java");
+        TestUtilities.copyStringToFile(testFile, "");
+        FileObject testSourceFO = FileUtil.toFileObject(testFile);
+        DataObject testSourceDO = DataObject.find(testSourceFO);
+        EditorCookie ec = (EditorCookie) 
testSourceDO.getCookie(EditorCookie.class);
+        String oldLevel = JavaSourceTest.SourceLevelQueryImpl.sourceLevel;
+        JavaSourceTest.SourceLevelQueryImpl.sourceLevel = "1.10";
+        final Document doc = ec.openDocument();
+        doc.putProperty(Language.class, JavaTokenId.language());
+        String content
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public static void main(String[] args) {\n"
+                + "           final   var    v    =   10; \n"
+                + "    }\n"
+                + "}\n";
+
+        String golden
+                = "package hierbas.del.litoral;\n\n"
+                + "public class Test {\n\n"
+                + "    public static void main(String[] args) {\n"
+                + "        final var v = 10;\n"
+                + "    }\n"
+                + "}\n";
+        reformat(doc, content, golden);
+        JavaSourceTest.SourceLevelQueryImpl.sourceLevel = oldLevel;
+    }
+
     private void reformat(Document doc, String content, String golden) throws 
Exception {
         reformat(doc, content, golden, 0, content.length());
     }

-- 
To stop receiving notification emails like this one, please contact
sde...@apache.org.

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to