@Override
    protected void addCompletions(
        @NotNull CompletionParameters completionParameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      if (!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
        return;
      }

      PsiElement psiElement = completionParameters.getPosition();
      YAMLCompoundValue yamlCompoundValue =
          PsiTreeUtil.getParentOfType(psiElement, YAMLCompoundValue.class);
      if (yamlCompoundValue == null) {
        return;
      }

      yamlCompoundValue = PsiTreeUtil.getParentOfType(yamlCompoundValue, YAMLCompoundValue.class);
      if (yamlCompoundValue == null) {
        return;
      }

      String value = YamlHelper.getYamlKeyValueAsString(yamlCompoundValue, "class", true);
      if (value != null) {
        PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), value);
        if (phpClass != null) {
          FormUtil.attachFormAliasesCompletions(phpClass, completionResultSet);
        }
      }
    }
  @Override
  protected void addCompletions(
      @NotNull CompletionParameters completionParameters,
      ProcessingContext processingContext,
      @NotNull CompletionResultSet completionResultSet) {

    if (!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
      return;
    }

    XmlEventParser xmlEventParser =
        ServiceXmlParserFactory.getInstance(
            completionParameters.getPosition().getProject(), XmlEventParser.class);
    for (Map.Entry<String, String> event : xmlEventParser.get().entrySet()) {
      completionResultSet.addElement(
          LookupElementBuilder.create(event.getKey())
              .withTypeText(event.getValue(), true)
              .withIcon(Symfony2Icons.EVENT));
    }

    for (EventDispatcherSubscribedEvent event :
        EventDispatcherSubscriberUtil.getSubscribedEvents(
            completionParameters.getPosition().getProject())) {
      completionResultSet.addElement(
          LookupElementBuilder.create(event.getStringValue())
              .withTypeText("EventSubscriber", true)
              .withIcon(Symfony2Icons.EVENT));
    }
  }
 public TypeCompletionHandler(
     @NotNull CompletionParameters parameters, @NotNull CompletionResultSet result, int weight) {
   super(parameters, result);
   _filter = getDefaultFilter(parameters.getPosition());
   _weight = weight;
   _prefixMatcher = result.getPrefixMatcher();
   _javaCtx = parameters.getPosition() instanceof PsiIdentifier;
 }
    @Override
    protected void addCompletions(
        @NotNull CompletionParameters parameters,
        ProcessingContext context,
        @NotNull CompletionResultSet result) {

      for (String[] completion :
          RegExpLanguageHosts.getInstance().getPosixCharacterClasses(parameters.getPosition())) {
        result.addElement(
            LookupElementBuilder.create(completion[0])
                .withTypeText((completion.length > 1) ? completion[1] : null)
                .withIcon(emptyIcon)
                .withInsertHandler(
                    new InsertHandler<LookupElement>() {
                      @Override
                      public void handleInsert(InsertionContext context, LookupElement item) {
                        context.setAddCompletionChar(false);
                        final Editor editor = context.getEditor();
                        final Document document = editor.getDocument();
                        final int tailOffset = context.getTailOffset();
                        if (document.getTextLength() < tailOffset + 2
                            || !document
                                .getText(new TextRange(tailOffset, tailOffset + 2))
                                .equals(":]")) {
                          document.insertString(tailOffset, ":]");
                        }
                        editor.getCaretModel().moveCaretRelatively(2, 0, false, false, true);
                      }
                    }));
      }
    }
    @Override
    protected void addCompletions(
        @NotNull CompletionParameters completionParameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      PsiElement position = completionParameters.getPosition();
      if (!Symfony2ProjectComponent.isEnabled(position)) {
        return;
      }

      YAMLCompoundValue yamlCompoundValue =
          PsiTreeUtil.getParentOfType(position, YAMLCompoundValue.class);
      if (yamlCompoundValue == null) {
        return;
      }

      String className =
          YamlHelper.getYamlKeyValueAsString(yamlCompoundValue, "targetEntity", false);
      if (className == null) {
        return;
      }

      PhpClass phpClass = PhpElementsUtil.getClass(position.getProject(), className);
      if (phpClass == null) {
        return;
      }

      for (DoctrineModelField field : EntityHelper.getModelFields(phpClass)) {
        if (field.getRelation() != null) {
          completionResultSet.addElement(new DoctrineModelFieldLookupElement(field));
        }
      }
    }
    public void addCompletions(
        @NotNull CompletionParameters parameters,
        ProcessingContext context,
        @NotNull CompletionResultSet resultSet) {

      PsiElement element = parameters.getPosition().getParent();
      Project project = element.getProject();

      if (!SilexProjectComponent.isEnabled(project)) {
        return;
      }

      if (!(element instanceof StringLiteralExpression)) {
        return;
      }

      Container container =
          Utils.findContainerForFirstParameterOfPimpleMethod((StringLiteralExpression) element);
      if (container == null) {
        return;
      }

      for (Service service : container.getServices().values()) {
        resultSet.addElement(new ServiceLookupElement(service, project));
      }

      for (Parameter parameter : container.getParameters().values()) {
        resultSet.addElement(new ParameterLookupElement(parameter));
      }

      resultSet.stopHere();
    }
    @Override
    protected void addCompletions(
        @NotNull CompletionParameters parameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      PsiElement position = parameters.getPosition();
      if (!Symfony2ProjectComponent.isEnabled(position)) {
        return;
      }

      PsiElement prevSiblingOfType =
          PsiElementUtils.getPrevSiblingOfType(
              position, YamlElementPatternHelper.getPreviousCommaSibling());
      if (prevSiblingOfType == null) {
        return;
      }

      String service = PsiElementUtils.trimQuote(prevSiblingOfType.getText());
      if (StringUtils.isBlank(service)) {
        return;
      }

      PhpClass phpClass = ServiceUtil.getServiceClass(prevSiblingOfType.getProject(), service);
      if (phpClass == null) {
        return;
      }

      for (Method method : phpClass.getMethods()) {
        if (method.getAccess().isPublic() && !(method.getName().startsWith("__"))) {
          completionResultSet.addElement(new PhpLookupElement(method));
        }
      }
    }
    protected void addCompletions(
        @NotNull CompletionParameters completionParameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      if (!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
        return;
      }

      PsiElement psiElement = completionParameters.getPosition();
      YAMLCompoundValue yamlCompoundValue =
          PsiTreeUtil.getParentOfType(psiElement, YAMLCompoundValue.class);
      if (yamlCompoundValue == null) {
        return;
      }

      addYamlClassMethods(yamlCompoundValue, completionResultSet, this.yamlArrayKeyName);
    }
    @Override
    public void addCompletions(
        @NotNull final CompletionParameters parameters,
        final ProcessingContext context,
        @NotNull final CompletionResultSet result) {
      for (final String[] completion :
          RegExpLanguageHosts.getInstance().getKnownCharacterClasses(parameters.getPosition())) {
        addLookupElement(result, completion[0], completion[1], emptyIcon);
      }

      for (String[] stringArray :
          RegExpLanguageHosts.getInstance().getAllKnownProperties(parameters.getPosition())) {
        addLookupElement(
            result,
            "p{" + stringArray[0] + "}",
            stringArray.length > 1 ? stringArray[1] : null,
            PlatformIcons.PROPERTY_ICON);
      }
    }
  public void addCompletions(
      @NotNull final CompletionParameters parameters,
      final ProcessingContext context,
      @NotNull final CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    while (element != null && !(element instanceof CfmlAttributeImpl)) {
      element = element.getParent();
    }
    if (element == null) {
      return;
    }
    CfmlAttributeImpl attribute = (CfmlAttributeImpl) element;
    String attributeName = attribute.getFirstChild().getText();
    while (element != null && !(element instanceof CfmlTag)) {
      element = element.getParent();
    }
    if (element == null) {
      return;
    }
    CfmlTag tag = (CfmlTag) element;
    String tagName = tag.getTagName();

    String[] attributeValue =
        CfmlUtil.getAttributeValues(tagName, attributeName, parameters.getPosition().getProject());

    if ("type".equalsIgnoreCase(attributeName) && "cfargument".equalsIgnoreCase(tagName)
        || "returntype".equalsIgnoreCase(attributeName) && "cffunction".equalsIgnoreCase(tagName)) {
      Object[] objects =
          CfmlComponentReference.buildVariants(
              attribute.getPureAttributeValue(),
              element.getContainingFile(),
              element.getProject(),
              null,
              true);
      for (Object o : objects) {
        result.addElement((LookupElement) o);
      }
    }
    for (String s : attributeValue) {
      result.addElement(LookupElementBuilder.create(s).withCaseSensitivity(false));
    }
  }
 private static boolean isTopLevelKeyCompletion(@NotNull CompletionParameters parameters) {
   PsiElement psiElement = parameters.getPosition();
   Document document = JsPsiUtils.getDocument(parameters.getOriginalFile());
   if (document != null) {
     TextRange textRange = psiElement.getTextRange();
     int startLine = document.getLineNumber(textRange.getStartOffset());
     int startOffset = document.getLineStartOffset(startLine);
     return startOffset == textRange.getStartOffset();
   }
   return false;
 }
 @Override
 public void addCompletions(
     @NotNull final CompletionParameters parameters,
     final ProcessingContext context,
     @NotNull final CompletionResultSet result) {
   for (String[] stringArray :
       RegExpLanguageHosts.getInstance().getAllKnownProperties(parameters.getPosition())) {
     result.addElement(
         TailTypeDecorator.withTail(
             createLookupElement(stringArray[0], null, emptyIcon),
             TailType.createSimpleTailType('}')));
   }
 }
 protected Set<AbstractElement> computeFollowElements(final CompletionParameters parameters) {
   Editor _editor = parameters.getEditor();
   Document _document = _editor.getDocument();
   PsiElement _position = parameters.getPosition();
   ASTNode _node = _position.getNode();
   int _startOffset = _node.getStartOffset();
   TextRange _textRange = new TextRange(0, _startOffset);
   final String text = _document.getText(_textRange);
   final Collection<FollowElement> followElements =
       this.contentAssistParser.getFollowElements(text, false);
   final HashSet<AbstractElement> allElements = CollectionLiterals.<AbstractElement>newHashSet();
   this.followElementComputer.computeFollowElements(followElements, allElements);
   return allElements;
 }
    protected void addCompletions(
        @NotNull CompletionParameters completionParameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      if (!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
        return;
      }

      // TODO: move this to pattern; filters match on parameter array
      // - [ setContainer, [ @service_container ] ]
      PsiElement psiElement = completionParameters.getPosition();
      if (psiElement.getParent() == null
          || !(psiElement.getParent().getContext() instanceof YAMLSequence)) {
        return;
      }

      YAMLKeyValue callYamlKeyValue = PsiTreeUtil.getParentOfType(psiElement, YAMLKeyValue.class);
      if (callYamlKeyValue == null) {
        return;
      }

      addYamlClassMethods(callYamlKeyValue.getContext(), completionResultSet, "class");
    }
 private static void addBasePathCompletionsIfNeeded(
     @NotNull CompletionParameters parameters,
     @NotNull CompletionResultSet result,
     @NotNull BipartiteString caretBipartiteElementText) {
   YAMLKeyValue keyValue =
       ObjectUtils.tryCast(parameters.getPosition().getParent(), YAMLKeyValue.class);
   if (keyValue != null) {
     if (keyValue.getParent() instanceof YAMLDocument && BasePathInfo.isBasePathKey(keyValue)) {
       BasePathInfo basePathInfo = newBasePathInfo(parameters);
       if (basePathInfo != null) {
         VirtualFile configDir = basePathInfo.getConfigDir();
         if (configDir != null) {
           addPathCompletions(result, caretBipartiteElementText, configDir, true);
         }
       }
     }
   }
 }
  @NotNull
  @Override
  public ThreeState shouldFocusLookup(@NotNull CompletionParameters parameters) {
    // 1. Do not automatically insert completion for first reference expression in block inside
    // function literal if it has no parameters yet.

    // 2. The same but for the case when first expression is additionally surrounded with brackets

    PsiElement position = parameters.getPosition();
    JetFunctionLiteralExpression functionLiteral =
        PsiTreeUtil.getParentOfType(position, JetFunctionLiteralExpression.class);

    if (functionLiteral != null) {
      PsiElement expectedReference = position.getParent();
      if (expectedReference instanceof JetSimpleNameExpression) {
        if (PsiTreeUtil.findChildOfType(functionLiteral, JetParameterList.class) == null) {
          {
            // 1.
            PsiElement expectedBlock = expectedReference.getParent();
            if (expectedBlock instanceof JetBlockExpression) {
              if (expectedReference.getPrevSibling() == null) {
                return ThreeState.NO;
              }
            }
          }

          {
            // 2.
            PsiElement expectedParenthesized = expectedReference.getParent();
            if (expectedParenthesized instanceof JetParenthesizedExpression) {
              PsiElement expectedBlock = expectedParenthesized.getParent();
              if (expectedBlock instanceof JetBlockExpression) {
                if (expectedParenthesized.getPrevSibling() == null) {
                  return ThreeState.NO;
                }
              }
            }
          }
        }
      }
    }

    return ThreeState.UNSURE;
  }
 private static void addInnerSequencePathCompletionsIfNeeded(
     @NotNull CompletionParameters parameters,
     @NotNull CompletionResultSet result,
     @NotNull BipartiteString caretBipartiteElementText) {
   PsiElement element = parameters.getPosition();
   YAMLKeyValue keyValue =
       JstdConfigFileUtils.getVerifiedHierarchyHead(
           element.getParent(),
           new Class[] {YAMLSequence.class, YAMLCompoundValue.class},
           YAMLKeyValue.class);
   BasePathInfo basePathInfo = newBasePathInfo(parameters);
   boolean keyMatched =
       keyValue != null && JstdConfigFileUtils.isTopLevelKeyWithInnerFileSequence(keyValue);
   if (basePathInfo != null && keyMatched) {
     VirtualFile basePath = basePathInfo.getBasePath();
     if (basePath != null && keyValue.getParent() instanceof YAMLDocument) {
       addPathCompletions(result, caretBipartiteElementText, basePath, false);
     }
   }
 }
 private static void addTopLevelKeysCompletionIfNeeded(
     @NotNull CompletionParameters parameters,
     @NotNull CompletionResultSet result,
     @NotNull BipartiteString caretBipartiteElementText) {
   PsiElement element = parameters.getPosition();
   YAMLDocument yamlDocument = ObjectUtils.tryCast(element.getParent(), YAMLDocument.class);
   if (yamlDocument == null) {
     yamlDocument =
         JstdConfigFileUtils.getVerifiedHierarchyHead(
             element.getParent(), new Class[] {YAMLKeyValue.class}, YAMLDocument.class);
   }
   if (yamlDocument != null) {
     String prefix = caretBipartiteElementText.getPrefix();
     result = result.withPrefixMatcher(prefix);
     for (String key : JstdConfigFileUtils.VALID_TOP_LEVEL_KEYS) {
       if (key.startsWith(prefix)) {
         result.addElement(LookupItem.fromString(key + ":"));
       }
     }
   }
 }
    @Override
    protected void addCompletions(
        @NotNull CompletionParameters parameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      if (!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
        return;
      }

      PhpIndex phpIndex = PhpIndex.getInstance(parameters.getOriginalFile().getProject());
      for (PhpClass phpClass : phpIndex.getAllSubclasses("\\Doctrine\\ORM\\EntityRepository")) {
        String presentableFQN = phpClass.getPresentableFQN();
        if (presentableFQN != null) {
          completionResultSet.addElement(
              LookupElementBuilder.create(phpClass.getName())
                  .withTypeText(phpClass.getPresentableFQN(), true)
                  .withIcon(phpClass.getIcon()));
        }
      }
    }
  @NotNull
  @Override
  public ThreeState shouldFocusLookup(@NotNull CompletionParameters parameters) {
    final PsiElement position = parameters.getPosition();

    if (position.getParent() instanceof GrReferenceElement
        && psiElement()
            .afterLeaf(psiElement().withText("(").withParent(GrForStatement.class))
            .accepts(position)) {
      return ThreeState.NO;
    }

    if (position.getParent() instanceof GrReferenceExpression) {
      final GrReferenceExpression ref = (GrReferenceExpression) position.getParent();
      final GrExpression qualifier = ref.getQualifierExpression();
      if (qualifier == null) {
        if (isPossibleClosureParameter(ref)) return ThreeState.NO;
        if (parameters.getOriginalFile().getUserData(GroovyShellAction.GROOVY_SHELL_FILE)
            == Boolean.TRUE) {
          return ThreeState.NO;
        }

        GrExpression runtimeQualifier = PsiImplUtil.getRuntimeQualifier(ref);
        if (runtimeQualifier != null && runtimeQualifier.getType() == null) {
          return ThreeState.NO;
        }

        return ThreeState.YES;
      }

      if (qualifier.getType() == null) {
        return ThreeState.NO;
      }
      return ThreeState.YES;
    }
    return ThreeState.UNSURE;
  }
    public void addCompletions(
        @NotNull CompletionParameters parameters,
        ProcessingContext context,
        @NotNull CompletionResultSet resultSet) {

      PsiElement stringLiteralExpression = parameters.getPosition().getParent();
      Project project = stringLiteralExpression.getProject();

      if (!SilexProjectComponent.isEnabled(project)) {
        return;
      }

      if (!(stringLiteralExpression instanceof StringLiteralExpression)) {
        return;
      }

      PsiElement arrayKeyElement = stringLiteralExpression.getParent();
      PsiElement element = arrayKeyElement.getParent();

      if (element instanceof ArrayHashElement) {

        if (!arrayKeyElement.isEquivalentTo(element.getFirstChild())) {
          return;
        }

        element = element.getParent();
      }

      if (!(element instanceof ArrayCreationExpression)) {
        return;
      }

      PsiElement parameterList = element.getParent();
      if (!(parameterList instanceof ParameterList)) {
        return;
      }

      PsiElement[] params = ((ParameterList) parameterList).getParameters();
      if (!(params.length > 1 && params[1].isEquivalentTo(element))) {
        return;
      }

      PsiElement methodReference = parameterList.getParent();
      if (!(methodReference instanceof MethodReference)) {
        return;
      }

      String methodReferenceName = ((MethodReference) methodReference).getName();
      if ((methodReferenceName == null) || !(methodReferenceName.equals("register"))) {
        return;
      }

      Container container =
          Utils.findContainerForMethodReference((MethodReference) methodReference);
      if (container == null) {
        return;
      }

      for (Parameter parameter : container.getParameters().values()) {
        resultSet.addElement(new ParameterLookupElement(parameter));
      }

      resultSet.stopHere();
    }
    @Override
    protected void addCompletions(
        @NotNull CompletionParameters completionParameters,
        ProcessingContext processingContext,
        @NotNull CompletionResultSet completionResultSet) {

      PsiElement position = completionParameters.getPosition();
      if (!Symfony2ProjectComponent.isEnabled(position)) {
        return;
      }

      PsiElement psiElement =
          PsiTreeUtil.findFirstParent(
              position,
              new Condition<PsiElement>() {
                @Override
                public boolean value(PsiElement psiElement) {

                  if (psiElement instanceof YAMLKeyValue) {
                    String s = ((YAMLKeyValue) psiElement).getKeyText().toLowerCase();
                    if ("joinTable".equalsIgnoreCase(s)) {
                      return true;
                    }
                  }

                  return false;
                }
              });

      if (psiElement == null) {
        return;
      }

      PsiElement yamlCompoundValue = psiElement.getParent();
      if (!(yamlCompoundValue instanceof YAMLCompoundValue)) {
        return;
      }

      String className =
          YamlHelper.getYamlKeyValueAsString(
              (YAMLCompoundValue) yamlCompoundValue, "targetEntity", false);
      if (className == null) {
        return;
      }

      PhpClass phpClass =
          ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), className);
      if (phpClass == null) {
        return;
      }

      for (DoctrineModelField field : EntityHelper.getModelFields(phpClass)) {
        if (field.getRelation() == null) {
          String columnName = field.getColumn();
          if (columnName == null) {
            completionResultSet.addElement(
                LookupElementBuilder.create(field.getName()).withIcon(Symfony2Icons.DOCTRINE));
          } else {
            completionResultSet.addElement(
                LookupElementBuilder.create(columnName)
                    .withTypeText(field.getName(), false)
                    .withIcon(Symfony2Icons.DOCTRINE));
          }
        }
      }
    }
  @Override
  protected void addCompletions(
      @NotNull CompletionParameters params,
      ProcessingContext ctx,
      @NotNull CompletionResultSet results) {

    PsiElement curr = params.getPosition().getOriginalElement();

    // n: attributes
    if (curr.getParent() instanceof XmlAttribute) {
      for (LookupElementBuilder x : ATTR_LOOKUPS) results.addElement(x);
      return;
    }

    // Keywords
    if (curr.getNode().getElementType() == LatteTokenTypes.MACRO_NAME) {
      for (LookupElementBuilder x : KEYWORD_LOOKUPS) results.addElement(x);

      HashSet<String> openedMacros =
          new HashSet<String>(); // macros which are opened before (should complete closing)
      HashSet<String> closedMacros =
          new HashSet<String>(); // which are closed (should not complete closing)

      // Go up and find open keywords (and offer them for closing)
      PsiElement cursor = curr;
      while (cursor != null && (cursor.getPrevSibling() != null || cursor.getParent() != null)) {
        // macro found {xx} found
        if (cursor instanceof MacroNodeImpl) {
          MacroNodeImpl node = (MacroNodeImpl) cursor;
          String name = node.getMacroName();

          if (name.charAt(0) == '/') { // closing macro
            closedMacros.add(name.substring(1));
          } else if (PAIR_MACROS.contains(name)) {
            if (closedMacros.contains(name)) closedMacros.remove(name); // is closed later
            else openedMacros.add(name); // not closed, let us close it
          }
        }

        PsiElement tmp;
        if ((tmp = cursor.getPrevSibling()) != null) cursor = tmp; // Go prev if possible...
        else cursor = cursor.getParent(); // ... or up
      }

      for (String name : openedMacros) {
        if (name.equals("if")) {
          results.addElement(reuseClosingTag("else"));
          results.addElement(reuseClosingTag("elseif"));
        }
        results.addElement(reuseClosingTag("/" + name));
      }

      results.stopHere();
    }

    // Modifiers (if after pipe)
    PsiElement prev = curr.getPrevSibling();
    if (prev != null && prev instanceof PsiWhiteSpace) prev = prev.getPrevSibling();
    if (prev != null && prev.getNode().getElementType() == LatteTokenTypes.MODIFIER) {
      for (LookupElementBuilder x : FILTER_LOOKUPS) results.addElement(x);
      results.stopHere();
    }
  }