Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  * 
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  * 
  * Contributor(s):
  * 
  * Portions Copyrighted 2008 Sun Microsystems, Inc.
  */
 
 package org.netbeans.modules.cnd.editor.fortran.reformat;
 
 import java.util.List;
 import static org.netbeans.cnd.api.lexer.CppTokenId.*;

Author(s):
Alexander Simon
 
 public class FortranPreprocessorFormatter {
     private FortranReformatterImpl context;
     private final FortranExtendedTokenSequence ts;
     private final FortranCodeStyle codeStyle;
     private final FortranDiffLinkedList diffs;
     private int prepocessorDepth = 0;
     private FortranBracesStack braces;
 
     
     /*package local*/ FortranPreprocessorFormatter(FortranReformatterImpl context){
         this. = context;
         this. = context.ts;
         this. = context.codeStyle;
         this. = context.diffs;
         this. = context.braces;
     }
     
     /*package local*/ void indentPreprocessor(Token<FortranTokenIdprevious) {
         TokenSequence<CppTokenIdprep = .embedded(CppTokenId.languagePreproc());
         if (prep == null){
             return;
         }
         prep.moveStart();
         while (prep.moveNext()) {
             if (!(prep.token().id() ==  ||
                     prep.token().id() == )) {
                 break;
             }
         }
         Token<CppTokenIddirective = null;
         boolean atSharp = false;
         if (prep.token() != null) {
             directive = prep.token();
         }
         PreprocessorStateStack ps = null;
         if (directive != null) {
             switch (directive.id()) {
                 case //("else", "preprocessor-keyword-directive"),
                case //("elif", "preprocessor-keyword-directive"),
                    --;
                    if (!.empty()){
                        ps = .pop();
                        ps.outputStack.add(.clone());
                        .reset(ps.inputStack);
                    }
                    break;
                case //("endif", "preprocessor-keyword-directive"),
                    --;
                    if (!.empty()){
                        ps = .pop();
                        ps.outputStack.add(.clone());
                        .reset(ps.getBestOutputStack());
                    }
                    break;
            }
            if (.doFormat()) {
                while(prep.movePrevious()) {
                    if (prep.token().id() == ) {
                        atSharp = true;
                        break;
                    }
                }
            }
        }
        if (atSharp) {
            selectPreprocessorIndent(previousprep);
        }
        if (directive != null) {
            switch (directive.id()) {
                case //("if", "preprocessor-keyword-directive"),
                case //("ifdef", "preprocessor-keyword-directive"),
                case //("ifndef", "preprocessor-keyword-directive"),
                    ++;
                    .push(new PreprocessorStateStack(.clone()));
                    break;
                case //("else", "preprocessor-keyword-directive"),
                case //("elif", "preprocessor-keyword-directive"),
                    ++;
                    if (ps != null) {
                        .push(ps);
                    } else {
                        .push(new PreprocessorStateStack(.clone()));
                    }
                    break;
            }
        }
    }
    
    private void selectPreprocessorIndent(Token<FortranTokenIdpreviousTokenSequence<CppTokenIdprep) {
        Token<CppTokenIdnext = null;
        if (prep.moveNext()) {
            next = prep.token();
            prep.movePrevious();
        }
        switch(.indentPreprocessorDirectives()) {
            case :
                 indentByCode(previousprepnext);
                 break;
            case :
                 noIndent(previousprepnext);
                 break;
            case :
                 indentByPreprocessor(previousprepnext);
                 break;
        }
    }
    private void noIndent(Token<FortranTokenIdpreviousTokenSequence<CppTokenIdprepToken<CppTokenIdnext) {
        indentBefore(previous, 0, false); // NOI18N
        indentAfter(prepnext, 0); // NOI18N
    }
    private void indentBefore(Token<FortranTokenIdpreviousint spacesboolean isIndent) {
        DiffResult diff = .getDiffs(, -1);
        if (diff != null) {
            if (diff.after != null) {
                diff.after.replaceSpaces(spacesisIndent); // NOI18N
                if (diff.replace != null && !diff.after.hasNewLine()){
                    diff.replace.replaceSpaces(0, false); // NOI18N
                }
                return;
            } else if (diff.replace != null) {
                diff.replace.replaceSpaces(spacesisIndent); // NOI18N
                return;
            }
        }
        if (previous != null && previous.id() == .) {
            if (!Diff.equals(previous.text().toString(), 0, spacesisIndent)){
                .replacePrevious(previous, 0, spacesisIndent);
            }
        } else {
            if (spaces > 0){
                .addBeforeCurrent(0, spacesisIndent);
            }
        }
    }
    private void indentAfter(TokenSequence<CppTokenIdprepToken<CppTokenIdnextint spaces) {
        if (next.id() == ) {
            if (!Diff.equals(next.text().toString(), 0, spacesfalse)){
                .addFirst(prep.offset() + prep.token().length(),
                               prep.offset() + prep.token().length() + next.length(), 0, spacesfalse);
            }
        } else {
            if (spaces > 0) {
                .addFirst(prep.offset()+ prep.token().length(),
                               prep.offset()+ prep.token().length(), 0, spacesfalse);
            }
        }
    }
    private void indentByCode(Token<FortranTokenIdpreviousTokenSequence<CppTokenIdprepToken<CppTokenIdnext) {
        if (.sharpAtStartLine()) {
            indentBefore(previous, 0, false); // NOI18N
            indentAfter(prepnext.getIndent());
        } else {
            indentBefore(previous.getIndent(), true);
            indentAfter(prepnext, 0); // NOI18N
        }
    }
    private void indentByPreprocessor(Token<FortranTokenIdpreviousTokenSequence<CppTokenIdprepToken<CppTokenIdnext) {
        if (.sharpAtStartLine()) {
            indentBefore(previous, 0, false); // NOI18N
            indentAfter(prepnextgetPreprocessorIndent());
        } else {
            indentBefore(previousgetPreprocessorIndent(), true);
            indentAfter(prepnext, 0);
        }
    }
    private int getPreprocessorIndent(int shift) {
        if (shift > 0) {
            return shift * .indentSize();
        } else {
            return 0;
        }
    }
    private static class PreprocessorStateStack {
        private FortranBracesStack inputStack;
        private List<FortranBracesStackoutputStack = new ArrayList<FortranBracesStack>();
        private PreprocessorStateStack(FortranBracesStack inputStack){
            this. = inputStack;
        }
        private FortranBracesStack getBestOutputStack(){
            if (.size()>0){
                FortranBracesStack min =null;
                int minLen = .;
                FortranBracesStack max =null;
                int maxLen = .;
                int inLen = .getLength();
                for(FortranBracesStack out : ){
                    int currentLen = out.getLength();
                    if (currentLen < inLen){
                        if (currentLen <= minLen) {
                            min = out;
                            minLen = currentLen;
                        }
                    } else if (currentLen > inLen) {
                        if (currentLen >= maxLen) {
                            max = out;
                            maxLen = currentLen;
                        }
                    }
                }
                if (min != null && max == null) {
                    return min;
                }
                if (max != null) {
                    return max;
                }
                return .get(.size()-1);
            }
            return ;
        }
        
    }
New to GrepCode? Check out our FAQ X