package org.jetbrains.postfixCompletion.completion;

import com.intellij.codeInsight.completion.CompletionContributor;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.template.CustomLiveTemplate;
import com.intellij.patterns.ElementPattern;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.postfixCompletion.templates.PostfixLiveTemplate;

import static com.intellij.patterns.PsiJavaPatterns.psiElement;

public class PostfixTemplateCompletionContributor extends CompletionContributor {

  private static final TokenSet SUITABLE_ELEMENTS = TokenSet.orSet(ElementType.KEYWORD_BIT_SET,
                                                                   ElementType.LITERAL_BIT_SET,
                                                                   TokenSet.create(JavaTokenType.IDENTIFIER));

  public PostfixTemplateCompletionContributor() {
    extend(CompletionType.BASIC, identifierAfterDot(), new PostfixTemplatesCompletionProvider());
  }

  @Nullable
  public static PostfixLiveTemplate getPostfixLiveTemplate(@NotNull PsiFile file, int offset) {
    PostfixLiveTemplate postfixLiveTemplate = CustomLiveTemplate.EP_NAME.findExtension(PostfixLiveTemplate.class);
    return postfixLiveTemplate != null && postfixLiveTemplate.isApplicable(file, offset, false) ? postfixLiveTemplate : null;
  }

  private static ElementPattern<? extends PsiElement> identifierAfterDot() {
    return psiElement().withElementType(SUITABLE_ELEMENTS).afterLeaf(psiElement(JavaTokenType.DOT));
  }
}

@@ -697,16 +697,77 @@
 Element;
+%0Aimport static com.intellij.patterns.StandardPatterns.string;
 %0A%0Apublic
@@ -1815,25 +1815,41 @@
 ent(
-JavaTokenType.DOT
+).withText(string().endsWith(%22.%22)
 ));%0A

Fix psi pattern for 1.switch