/*
 * Copyright 2000-2016 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.lang.properties.formatting;

import com.intellij.formatting.*;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.formatter.FormattingDocumentModelImpl;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.impl.source.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Dmitry Batkovich
 */
public class PropertiesFormattingModelBuilder implements FormattingModelBuilder {
  @NotNull
  @Override
  public PropertiesFormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    final ASTNode root = TreeUtil.getFileElement((TreeElement)SourceTreeToPsiMap.psiElementToTree(element));
    final FormattingDocumentModelImpl documentModel = FormattingDocumentModelImpl.createOn(element.getContainingFile());
    return new PropertiesFormattingModel(root, documentModel, settings);
  }

  @Nullable
  @Override
  public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffset) {
    return null;
  }

  private static class PropertiesFormattingModel implements FormattingModel {
    private final FormattingDocumentModelImpl myDocumentModel;
    private PropertiesRootBlock myRoot;

    public PropertiesFormattingModel(ASTNode root, FormattingDocumentModelImpl documentModel, CodeStyleSettings settings) {
      myRoot = new PropertiesRootBlock(root, settings);
      myDocumentModel = documentModel;
    }

    @NotNull
    @Override
    public Block getRootBlock() {
      return myRoot;
    }

    @NotNull
    @Override
    public FormattingDocumentModel getDocumentModel() {
      return myDocumentModel;
    }

    @Override
    public TextRange replaceWhiteSpace(TextRange textRange, String whiteSpace) {
      return textRange;
    }

    @Override
    public TextRange shiftIndentInsideRange(ASTNode node, TextRange range, int indent) {
      return null;
    }

    @Override
    public void commitChanges() {

    }
  }
}

@@ -941,16 +941,75 @@
 elImpl;%0A
+import com.intellij.psi.formatter.PsiBasedFormattingModel;%0A
 import c
@@ -1393,34 +1393,24 @@
 de%0A  public 
-Properties
 FormattingMo
@@ -1725,245 +1725,15 @@
 ew P
-ropertiesFormattingModel(root, documentModel, settings);%0A  %7D%0A%0A  @Nullable%0A  @Override%0A  public TextRange getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffset) %7B%0A    return null;%0A  %7D%0A%0A  private static class Properties
+siBased
 Form
@@ -1747,579 +1747,112 @@
 odel
- imp
+(e
 lement
-s FormattingModel %7B%0A    private final FormattingDocumentModelImpl myDocumentModel;%0A    private PropertiesRootBlock myRoot;%0A%0A    public PropertiesFormattingModel(ASTNode root, FormattingDocumentModelImpl documentModel, CodeStyleSettings settings) %7B%0A      myRoot = new PropertiesRootBlock(root, settings);%0A      myDocumentModel = documentModel;%0A    %7D%0A%0A    @NotNull%0A    @Override%0A    public Block getRootBlock() %7B%0A      return myRoot
+.getContainingFile(), new PropertiesRootBlock(root, settings), documentModel)
 ;%0A
-  
   %7D%0A%0A
-  
   @N
-otNull%0A    @Override%0A    public FormattingDocumentModel getDocumentModel() %7B%0A      return myDocumentModel;%0A    %7D%0A%0A  
+ullable%0A
   @O
@@ -1851,34 +1851,32 @@
 ble%0A  @Override%0A
-  
   public TextRan
@@ -1882,204 +1882,85 @@
 nge 
-replaceWhiteSpace(TextRange textRange, String whiteSpace) %7B%0A      return textRange;%0A    %7D%0A%0A    @Override%0A    public TextRange shiftIndentInsideRange(ASTNode node, TextRange range, int inden
+getRangeAffectingIndent(PsiFile file, int offset, ASTNode elementAtOffse
 t) %7B%0A
-  
     
@@ -1976,70 +1976,8 @@
 ll;%0A
-    %7D%0A%0A    @Override%0A    public void commitChanges() %7B%0A%0A    %7D%0A
   %7D%0A

use PsiBasedFormattingModel (IDEA-163408)