/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.formatting;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;

public class IndentInfo {

  private final int mySpaces;
  private final int myIndentSpaces;
  private final int myLineFeeds;
  //
  // The flag is used for compatibility with older versions of Intellij IDEA where spaces could be replaced with tabs inside a line of code,
  // not only in the beginning of the line.
  //
  private static boolean myInlineTabsEnabled = "true".equalsIgnoreCase(System.getProperty("enable.inline.tabs"));

  /** @see WhiteSpace#setForceSkipTabulationsUsage(boolean)  */
  private boolean   myForceSkipTabulationsUsage;

  public IndentInfo(final int lineFeeds, final int indentSpaces, final int spaces) {
    this(lineFeeds, indentSpaces, spaces, false);
  }

  public IndentInfo(final int lineFeeds, final int indentSpaces, final int spaces, final boolean forceSkipTabulationsUsage) {
    mySpaces = spaces;
    myIndentSpaces = indentSpaces;
    myLineFeeds = lineFeeds;
    myForceSkipTabulationsUsage = forceSkipTabulationsUsage;
  }

  public int getSpaces() {
    return mySpaces;
  }

  public int getIndentSpaces() {
    return myIndentSpaces;
  }

  /**
   * Builds string that contains line feeds, white spaces and tabulation symbols known to the current {@link IndentInfo} object.
   *
   * @param options              indentation formatting options
   */
  public String generateNewWhiteSpace(CommonCodeStyleSettings.IndentOptions options) {
    StringBuffer buffer = new StringBuffer();
    StringUtil.repeatSymbol(buffer, '\n', myLineFeeds);

    if (options.USE_TAB_CHARACTER && !myForceSkipTabulationsUsage) {
      if (options.SMART_TABS) {
        return fillUsingSmartTabs(buffer, options);
      }
      else if (myLineFeeds > 0 || myInlineTabsEnabled) {
        return fillUsingTabs(buffer, options);
      }
    }
    int spaces = getTotalSpaces();
    if (spaces > 0) {
      StringUtil.repeatSymbol(buffer, ' ', spaces);
    }
    return buffer.toString();

  }

  private String fillUsingTabs(StringBuffer buffer, CommonCodeStyleSettings.IndentOptions options) {
    int size = getTotalSpaces();
    while (size > 0) {
      if (size >= options.TAB_SIZE) {
        buffer.append('\t');
        size -= options.TAB_SIZE;
      }
      else {
        buffer.append(' ');
        size--;
      }
    }
    return buffer.toString();
  }

  private String fillUsingSmartTabs(StringBuffer buffer, CommonCodeStyleSettings.IndentOptions options) {
    int tabCount = myIndentSpaces / options.TAB_SIZE;
    int leftSpaces = myIndentSpaces - tabCount * options.TAB_SIZE;
    if (tabCount > 0) {
      StringUtil.repeatSymbol(buffer, '\t', tabCount);
    }
    if (leftSpaces + mySpaces > 0) {
      StringUtil.repeatSymbol(buffer, ' ', leftSpaces + mySpaces);
    }
    return buffer.toString();
  }

  public int getTotalSpaces() {
    return myIndentSpaces + mySpaces;
  }
}

@@ -868,317 +868,8 @@
 eds;
-%0A  //%0A  // The flag is used for compatibility with older versions of Intellij IDEA where spaces could be replaced with tabs inside a line of code,%0A  // not only in the beginning of the line.%0A  //%0A  private static boolean myInlineTabsEnabled = %22true%22.equalsIgnoreCase(System.getProperty(%22enable.inline.tabs%22));
 %0A%0A  
@@ -2030,164 +2030,218 @@
     
-return fillUsingSmartTabs(buffer, options);%0A      %7D%0A      else if (myLineFeeds %3E 0 %7C%7C myInlineTabsEnabled) %7B%0A        return fillUsingTabs(buffer, options
+int tabCount = myIndentSpaces / options.TAB_SIZE;%0A        int leftSpaces = myIndentSpaces - tabCount * options.TAB_SIZE;%0A        if (tabCount %3E 0) %7B%0A          StringUtil.repeatSymbol(buffer, '%5Ct', tabCount
 );%0A
+  
     
@@ -2252,54 +2252,32 @@
     
-%7D%0A
     i
-nt spaces = getTotalSpaces();%0A    if (s
+f (leftSpaces + myS
 pace
@@ -2277,32 +2277,36 @@
 mySpaces %3E 0) %7B%0A
+    
       StringUtil
@@ -2332,17 +2332,32 @@
 r, ' ', 
-s
+leftSpaces + myS
 paces);%0A
@@ -2364,151 +2364,43 @@
     
+   
+ 
 %7D%0A    
-return buffer.toString();%0A%0A
   %7D%0A
-%0A
   
-private String fillUsingTabs(StringBuffer buffer, CommonCodeStyleSettings.IndentOptions options) %7B%0A
+    else %7B%0A   
     
+ 
 int 
@@ -2420,24 +2420,28 @@
 alSpaces();%0A
+    
     while (s
@@ -2453,24 +2453,28 @@
  0) %7B%0A      
+ 
+   
 if (size %3E= 
@@ -2493,32 +2493,36 @@
 SIZE) %7B%0A        
+   
+ 
 buffer.append('%5C
@@ -2522,24 +2522,28 @@
 pend('%5Ct');%0A
+    
         size
@@ -2566,39 +2566,51 @@
 SIZE;%0A      
-%7D%0A
+    %7D%0A   
       
+ 
 else %7B%0A
+    
         buff
@@ -2629,24 +2629,28 @@
 ');%0A        
+ 
+   
 size--;%0A    
@@ -2647,26 +2647,24 @@
 ze--;%0A      
-%7D%0A
     %7D%0A    re
@@ -2665,207 +2665,49 @@
     
-return buffer.toString();%0A  %7D%0A%0A  private String fillUsingSmartTabs(StringBuffer buffer, CommonCodeStyleSettings.IndentOptions options) %7B%0A    int tabCount = myIndentSpaces / options.TAB_SIZE;%0A
+    %7D%0A      %7D%0A    %7D%0A    else %7B%0A  
     int 
 left
@@ -2698,37 +2698,33 @@
 %7B%0A      int 
-leftS
+s
 paces = 
 myIndentSpac
@@ -2715,147 +2715,36 @@
 s = 
-myIndentSpaces - tabCount * options.TAB_SIZE;%0A    if (tabCount %3E 0) %7B%0A      StringUtil.repeatSymbol(buffer, '%5Ct', tabCount
+getTotalSpaces(
 );%0A  
-  %7D%0A
     if (
 left
@@ -2739,32 +2739,17 @@
     if (
-leftSpaces + myS
+s
 paces %3E 
@@ -2749,24 +2749,26 @@
 aces %3E 0) %7B%0A
+  
       String
@@ -2798,32 +2798,17 @@
 r, ' ', 
-leftSpaces + myS
+s
 paces);%0A
@@ -2807,25 +2807,34 @@
 paces);%0A    
-%7D
+  %7D%0A    %7D%0A
 %0A    return 
@@ -2852,16 +2852,17 @@
 ring();%0A
+%0A
   %7D%0A%0A  p

Revert the fix for IDEA-79095