/*
 * Copyright 2000-2014 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.codeInsight.daemon;

import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil;
import com.intellij.psi.*;
import com.intellij.psi.augment.PsiAugmentProvider;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

public class PsiAugmentProviderTest extends LightCodeInsightFixtureTestCase {

  private static final String LOMBOK_VAL_FQN = "lombok.val";
  private static final String LOMBOK_VAL_SHORT_NAME = "val";

  public void testLombokVal() {
    PlatformTestUtil.registerExtension(PsiAugmentProvider.EP_NAME, new PsiAugmentProvider() {
      @NotNull
      @Override
      public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
        return Collections.<Psi>emptyList();
      }

      @Nullable
      @Override
      protected PsiType inferType(PsiTypeElement typeElement) {
        final PsiElement parent = typeElement.getParent();
        if (parent instanceof PsiLocalVariable && ((PsiLocalVariable)parent).getInitializer() != null || 
            parent instanceof PsiParameter && ((PsiParameter)parent).getDeclarationScope() instanceof PsiForeachStatement) {
          final String text = typeElement.getText();
          if (LOMBOK_VAL_SHORT_NAME.equals(text) || LOMBOK_VAL_FQN.equals(text)) {
            final PsiJavaCodeReferenceElement referenceElement = typeElement.getInnermostComponentReferenceElement();
            if (referenceElement != null) {
              final PsiElement resolve = referenceElement.resolve();
              if (resolve instanceof PsiClass) {
                if (parent instanceof PsiLocalVariable) {
                  final PsiExpression initializer = ((PsiVariable)parent).getInitializer();
                  assertNotNull(initializer);
                  final PsiType initializerType = initializer.getType();
                  if (initializer instanceof PsiNewExpression) {
                    final PsiJavaCodeReferenceElement reference = ((PsiNewExpression)initializer).getClassOrAnonymousClassReference();
                    if (reference != null) {
                      final PsiReferenceParameterList parameterList = reference.getParameterList();
                      if (parameterList != null) {
                        final PsiTypeElement[] elements = parameterList.getTypeParameterElements();
                        if (elements.length == 1 && elements[0].getType() instanceof PsiDiamondType) {
                          return TypeConversionUtil.erasure(initializerType);
                        }
                      }
                    }
                  }
                  return initializerType;
                }
                final PsiForeachStatement foreachStatement = (PsiForeachStatement)((PsiParameter)parent).getDeclarationScope();
                assertNotNull(foreachStatement);
                final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
                if (iteratedValue != null) {
                  return JavaGenericsUtil.getCollectionItemType(iteratedValue);
                }
              }
            }
          }
        }
        return null;
      }
    }, myTestRootDisposable);
    myFixture.addClass("package lombok; public @interface val{}");
    myFixture.testHighlighting(false, false, false, getTestName(false) + ".java");
  }

  @Override
  protected String getTestDataPath() {
    return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/augment";
  }

}

@@ -737,24 +737,158 @@
 nericsUtil;%0A
+import com.intellij.idea.Bombed;%0Aimport com.intellij.openapi.command.WriteCommandAction;%0Aimport com.intellij.openapi.editor.Document;%0A
 import com.i
@@ -902,16 +902,16 @@
 .psi.*;%0A
-
 import c
@@ -1217,16 +1217,43 @@
 lable;%0A%0A
+import java.util.Calendar;%0A
 import j
@@ -1500,16 +1500,28 @@
 %22val%22;%0A%0A
+  @Override%0A
   public
@@ -1530,25 +1530,53 @@
 oid 
-testLombokVal() %7B
+setUp() throws Exception %7B%0A    super.setUp();
 %0A   
@@ -2127,17 +2127,16 @@
  null %7C%7C
- 
 %0A       
@@ -4354,16 +4354,53 @@
 al%7B%7D%22);%0A
+  %7D%0A%0A  public void testLombokVal() %7B%0A
     myFi
@@ -4479,16 +4479,1059 @@
 );%0A  %7D%0A%0A
+  @Bombed(day = 10, month = Calendar.NOVEMBER, year = 2015, user = %22Roman Shevchenko%22)%0A  public void testLombokEditing() throws Exception %7B%0A    final PsiFile file = myFixture.configureByText(%22a.java%22, %22import lombok.val; class Foo %7B%7Bval o = ;%7D%7D%22);%0A    final PsiElement elementAt = file.findElementAt(35);%0A    assertNotNull(elementAt);%0A    final PsiElement parent = elementAt.getParent();%0A    assertInstanceOf(parent, PsiLocalVariable.class);%0A    final PsiLocalVariable var = (PsiLocalVariable)parent;%0A    var.getType(); //cache%0A    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());%0A    final Document document = documentManager.getDocument(file);%0A    assertNotNull(document);%0A    WriteCommandAction.runWriteCommandAction(getProject(), () -%3E %7B%0A      document.insertString(39, %221%22);%0A      documentManager.commitAllDocuments();%0A    %7D);%0A    assertTrue(var.isValid());%0A    final PsiType type = var.getType();%0A    assertNotNull(type);%0A    assertTrue(type.equalsToText(CommonClassNames.JAVA_LANG_STRING));%0A  %7D%0A%0A
   @Overr

test that PsiTYpeElementImpl doesn't clear cache on commit document (for Roman)