/*
 * Copyright 2000-2015 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.codeInspection;

import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.lang.properties.PropertiesBundle;
import com.intellij.lang.properties.PropertySuppressableInspectionBase;
import com.intellij.lang.properties.psi.codeStyle.PropertiesCodeStyleSettings;
import com.intellij.lang.properties.psi.impl.PropertiesFileImpl;
import com.intellij.lang.properties.psi.impl.PropertyImpl;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author Dmitry Batkovich
 */
public class WrongPropertyKeyValueDelimiterInspection extends PropertySuppressableInspectionBase implements CleanupLocalInspectionTool {

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    if (!(holder.getFile() instanceof PropertiesFileImpl)) {
      return PsiElementVisitor.EMPTY_VISITOR;
    }
    final PropertiesCodeStyleSettings codeStyleSettings = PropertiesCodeStyleSettings.getInstance(holder.getProject());
    final char codeStyleKeyValueDelimiter = codeStyleSettings.KEY_VALUE_DELIMITER;
    return new PsiElementVisitor() {
      @Override
      public void visitElement(PsiElement element) {
        if (element instanceof PropertyImpl) {
          final Character delimiter = ((PropertyImpl)element).getKeyValueDelimiter();
          if (delimiter != null && !delimiter.equals(codeStyleKeyValueDelimiter)) {
            holder.registerProblem(element, PropertiesBundle.message("wrong.property.key.value.delimiter.inspection.display.name"), new ReplaceKeyValueDelimiterQuickFix(element));
          }
        }
      }
    };
  }

  private static final class ReplaceKeyValueDelimiterQuickFix extends LocalQuickFixOnPsiElement implements HighPriorityAction {
    public ReplaceKeyValueDelimiterQuickFix(@NotNull PsiElement element) {
      super(element);
    }

    @NotNull
    @Override
    public String getText() {
      return getFamilyName();
    }

    @Override
    public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement element, @NotNull PsiElement endElement) {
      ((PropertyImpl) element).replaceKeyValueDelimiterWithDefault();
    }

    @NotNull
    @Override
    public String getFamilyName() {
      return "Replace Property Key/Value Delimiter According Code Style";
    }
  }
}

@@ -3022,17 +3022,17 @@
 ace 
-P
+p
 roperty 
 Key/
@@ -3031,19 +3031,19 @@
 rty 
-Key/V
+key/v
 alue 
-D
+d
 elim
@@ -3047,17 +3047,17 @@
 limiter 
-A
+a
 ccording
@@ -3061,14 +3061,14 @@
 ing 
-C
+c
 ode 
-S
+s
 tyle

proper capitalization