[hg] releases: fixed Bug #222396 C/C++ does not format code (end...

  • From: Alexander Simon < >
  • To:
  • Subject: [hg] releases: fixed Bug #222396 C/C++ does not format code (end...
  • Date: Mon, 19 Nov 2012 11:08:01 -0800

changeset 9d526f6c9d86 in releases ((none))
details: http://hg.netbeans.org/releases/rev/9d526f6c9d86
description:
        fixed Bug #222396 C/C++ does not format code (end of line comment + 
else)

diffstat:

 cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/Reformatter.java     
             |  18 +++++----
 cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/ReformatterImpl.java 
             |  15 +++++---
 
cnd.editor/test/unit/src/org/netbeans/modules/cnd/editor/cplusplus/FormatterTestCase.java
 |  17 +++++++++
 3 files changed, 36 insertions(+), 14 deletions(-)

diffs (154 lines):

diff --git 
a/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/Reformatter.java 
b/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/Reformatter.java
--- a/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/Reformatter.java
+++ b/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/Reformatter.java
@@ -43,6 +43,7 @@
 package org.netbeans.modules.cnd.editor.reformat;
 
 import java.util.LinkedList;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.text.BadLocationException;
 import javax.swing.text.Document;
@@ -71,6 +72,7 @@
     private JTextComponent currentComponent;
     private boolean expandTabToSpaces = true;
     private int tabSize = 8;
+    private static final Logger LOG = 
Logger.getLogger("org.netbeans.modules.cnd.editor"); // NOI18N
 
 
     public Reformatter(Context context) {
@@ -108,6 +110,7 @@
             int endOffset = doc.getLength();
             TokenHierarchy<?> hierarchy = TokenHierarchy.get(doc);
             if (hierarchy == null) {
+                LOG.log(Level.SEVERE, "Token hierarchy is not found in the 
document {1}", new Object[]{doc});
                 return;
             }
             reformatImpl(hierarchy, 0, endOffset);
@@ -126,12 +129,15 @@
         Language<CppTokenId> language = 
CndLexerUtilities.getLanguage(context.mimePath());
         if (language != null) {
             reformatLanguage(language, startOffset, endOffset);
+        } else {
+            //LOG.log(Level.SEVERE, "Language of mime type {0} is not found 
in the document {1}", new Object[]{context.mimePath(), doc});
         }
     }
 
     private void reformatLanguage(Language<CppTokenId> language, int 
startOffset, int endOffset) throws BadLocationException {
         TokenHierarchy<?> hierarchy = TokenHierarchy.create(doc.getText(0, 
doc.getLength()), language);
         if (hierarchy == null) {
+            LOG.log(Level.SEVERE, "Token hierarchy {0} is not found in the 
document {1}", new Object[]{language, doc});
             return;
         }
         reformatImpl(hierarchy, startOffset, endOffset);
@@ -240,10 +246,8 @@
                 if (end - start > 0) {
                     if (!checkRemoved(what)){
                         // Reformat failed
-                        Logger log = 
Logger.getLogger("org.netbeans.modules.cnd.editor"); // NOI18N
-                        String error = 
NbBundle.getMessage(Reformatter.class, "REFORMATTING_FAILED", // NOI18N
-                                doc.getText(start, end - start), text);
-                        log.severe(error);
+                        LOG.log(Level.SEVERE, 
NbBundle.getMessage(Reformatter.class, "REFORMATTING_FAILED", // NOI18N
+                                doc.getText(start, end - start), text));
                         return;
                     }
                     buf.delete(start, end);
@@ -274,10 +278,8 @@
             }
             if (!checkRemoved(what)){
                 // Reformat failed
-                Logger log = 
Logger.getLogger("org.netbeans.modules.cnd.editor"); // NOI18N
-                String error = NbBundle.getMessage(Reformatter.class, 
"REFORMATTING_FAILED", // NOI18N
-                        doc.getText(start, end - start), text);
-                log.severe(error);
+                LOG.log(Level.SEVERE, NbBundle.getMessage(Reformatter.class, 
"REFORMATTING_FAILED", // NOI18N
+                        doc.getText(start, end - start), text));
                 return false;
             }
             doc.remove(start, end - start);
diff --git 
a/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/ReformatterImpl.java
 
b/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/ReformatterImpl.java
--- 
a/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/ReformatterImpl.java
+++ 
b/cnd.editor/src/org/netbeans/modules/cnd/editor/reformat/ReformatterImpl.java
@@ -1368,7 +1368,7 @@
 
         Token<CppTokenId> next = ts.lookNext();
         if (isClassDeclaration) {
-            if (next != null && !(next.id() == WHITESPACE || next.id() == 
ESCAPED_WHITESPACE || next.id() == NEW_LINE)) {
+            if (next != null && !isNextWitespace(next)) {
                 ts.addAfterCurrent(current, 0, 1, false);
             }
             return;
@@ -1388,7 +1388,7 @@
                     StackEntry top = braces.peek();
                     if (top != null && top.getKind() == DO) {
                         if (!codeStyle.newLineWhile()) {
-                            if (ts.isLastLineToken()) {
+                            if (ts.isLastLineToken() && 
isNextWitespace(next)) {
                                 Token<CppTokenId> n2 = ts.lookNext(2);
                                 if (n2 == null || n2.id() != 
PREPROCESSOR_DIRECTIVE) {
                                     ts.replaceNext(current, next, 0, 0, 
false);
@@ -1408,7 +1408,7 @@
                     if (statementEntry != null &&
                         (statementEntry.getKind() == TRY || 
statementEntry.getKind() == CATCH)) {
                         if (!codeStyle.newLineCatch()) {
-                            if (ts.isLastLineToken()) {
+                            if (ts.isLastLineToken() && 
isNextWitespace(next)) {
                                 Token<CppTokenId> n2 = ts.lookNext(2);
                                 if (n2 == null || n2.id() != 
PREPROCESSOR_DIRECTIVE) {
                                     ts.replaceNext(current, next, 0, 0, 
false);
@@ -1426,7 +1426,7 @@
                 case ELSE:
                 {
                     if (!codeStyle.newLineElse()) {
-                        if (ts.isLastLineToken()) {
+                        if (ts.isLastLineToken() && isNextWitespace(next)) {
                             Token<CppTokenId> n2 = ts.lookNext(2);
                             if (n2 == null || n2.id() != 
PREPROCESSOR_DIRECTIVE) {
                                 ts.replaceNext(current, next, 0, 0, false);
@@ -1447,6 +1447,10 @@
         }
     }
 
+    private boolean isNextWitespace(Token<CppTokenId> next) {
+        return next.id() == WHITESPACE || next.id() == ESCAPED_WHITESPACE || 
next.id() == NEW_LINE;
+    }
+    
     private void newLineFormat(Token<CppTokenId> previous, Token<CppTokenId> 
current, int parenDepth) {
         if (previous != null) {
             boolean done = false;
@@ -2050,8 +2054,7 @@
         Token<CppTokenId> next = ts.lookNext();
         if (next != null) {
             if (add) {
-                if (!(next.id() == WHITESPACE || next.id() == 
ESCAPED_WHITESPACE ||
-                      next.id() == NEW_LINE)) {
+                if (!isNextWitespace(next)) {
                     ts.addAfterCurrent(current, 0, 1, false);
                 }
             } else if (canRemoveSpaceAfter(current) && !keepExtra){
diff --git 
a/cnd.editor/test/unit/src/org/netbeans/modules/cnd/editor/cplusplus/FormatterTestCase.java
 
b/cnd.editor/test/unit/src/org/netbeans/modules/cnd/editor/cplusplus/FormatterTestCase.java
--- 
a/cnd.editor/test/unit/src/org/netbeans/modules/cnd/editor/cplusplus/FormatterTestCase.java
+++ 
b/cnd.editor/test/unit/src/org/netbeans/modules/cnd/editor/cplusplus/FormatterTestCase.java
@@ -5616,4 +5616,21 @@
               + "int && c;\n"
               + "\n");
     }
+    
+    public void test222396() {
+        setDefaultsOptions();
+        setLoadDocumentText(
+                "if(0){\n" 
+              + "        int a=0;\n" 
+              + "    }//sample comment\n"
+              + "else\n"
+              + "    int b = 1;");
+        reformat();
+        assertDocumentText("Incorrect rvalue reference",
+                "if (0) {\n" 
+              + "    int a = 0;\n" 
+              + "}//sample comment\n"
+              + "else\n"
+              + "    int b = 1;");
+    }
 }

[hg] releases: fixed Bug #222396 C/C++ does not format code (end...

Alexander Simon 11/19/2012

Project Features

About this Project

CND was started in November 2009, is owned by Leonid Lenyashin, and has 130 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close