com.jetbrains.php.completion.insert.PhpInsertHandlerUtil Java Examples

The following examples show how to use com.jetbrains.php.completion.insert.PhpInsertHandlerUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwigPathFunctionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull RouteLookupElement lookupElement) {
    context.getDocument().insertString(context.getStartOffset(), "{{ path('");

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("'");

    Set<String> vars = lookupElement.getRoute().getVariables();
    if(vars.size() > 0) {
        List<String> items = new ArrayList<>();
        for(String var: vars) {
            items.add(String.format("'%s': '%s'", var, "x"));
        }
        stringBuilder.append(", {");
        stringBuilder.append(StringUtils.join(items, ", "));
        stringBuilder.append("}");
    }

    stringBuilder.append(") }}");
    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), stringBuilder.toString());
}
 
Example #2
Source File: FluidTypeInsertHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {

        PsiElement psiElement = lookupElement.getPsiElement();
        if (psiElement instanceof Method && needParameter((Method) psiElement)) {
            if (PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "(")) {
                return;
            }

            String addText = "()";
            PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText);

            context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);
        }
    }
 
Example #3
Source File: ClassConstantInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement lookupElement) {

    if(!(lookupElement instanceof ClassConstantLookupElementInterface) || !(lookupElement.getObject() instanceof PhpClass)) {
        return;
    }

    PhpReferenceInsertHandler.getInstance().handleInsert(context, lookupElement);
    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "::class");
}
 
Example #4
Source File: TwigTranslationFilterInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull TranslatorLookupElement lookupElement) {
    PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());

    String domain = null;

    if(elementAt != null) {
        PsiElement element = TwigUtil.getElementOnTwigViewProvider(elementAt);
        TwigUtil.DomainScope domainScope = TwigUtil.getTwigFileDomainScope(element != null ? element : elementAt);
        String transDefaultDomain = domainScope.getDefaultDomain();

        String myDomain = lookupElement.getDomain();
        if(!transDefaultDomain.equalsIgnoreCase(myDomain)) {
            domain = myDomain;
        }
    }

    // preappend
    context.getDocument().insertString(context.getStartOffset(), "{{ '");

    StringBuilder stringBuilder = new StringBuilder();

    // 'foo.bar'
    stringBuilder.append("'|trans");

    // domain
    if(domain != null) {
        stringBuilder.append(String.format("({}, '%s')", domain));
    }

    stringBuilder.append(" }}");

    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), stringBuilder.toString());
}
 
Example #5
Source File: PhpConstGotoCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    super.handleInsert(context, lookupElement);

    if (context.getCompletionChar() == ':') {
        context.setAddCompletionChar(false);
    }

    if (!PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), SCOPE_OPERATOR)) {
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), SCOPE_OPERATOR);
    }

    PhpCompletionUtil.showCompletion(context);
}
 
Example #6
Source File: AnnotationMethodInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    if(lookupElement.getObject() instanceof AnnotationValue) {
        String addText = "=\"\"";

        if(((AnnotationValue) lookupElement.getObject()).getType() == AnnotationValue.Type.Array) {
            addText = "={}";
        }
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText);

    } else {
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "=\"\"");
    }

    context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);
}
 
Example #7
Source File: AnnotationTagInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "()");
    context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

    String lookupString = lookupElement.getLookupString();
    if(AnnotationIndex.getControllerAnnotations().containsKey(lookupString)) {
        String useStatement = AnnotationIndex.getControllerAnnotations().get(lookupString).getUse();
        if(null != useStatement) {
            AnnotationUseImporter.insertUse(context, useStatement);
        }
    }

}
 
Example #8
Source File: FunctionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {

        // check this:
        // {{ form_javasc|() }}
        // {{ form_javasc| }}
        if(PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "(")) {
            return;
        }

        String addText = "()";
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText);

        context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

    }
 
Example #9
Source File: AnnotationPropertyInsertHandler.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {

        // value completion should not fire when already presented:
        // eng| = "value"
        // eng|="value"
        if(PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "=") || PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), " =")) {
           return;
        }

        // move caret back
        int i = -1;

        // append completion text depend on value:
        // engine="|"
        // engine={|}
        // engine=<boolean|integer>
        if(lookupElement.getObject() instanceof AnnotationProperty) {
            String addText = "=\"\"";

            if(((AnnotationProperty) lookupElement.getObject()).getAnnotationPropertyEnum() == AnnotationPropertyEnum.ARRAY) {
                addText = "={}";
            }

            if(((AnnotationProperty) lookupElement.getObject()).getAnnotationPropertyEnum() == AnnotationPropertyEnum.INTEGER || ((AnnotationProperty) lookupElement.getObject()).getAnnotationPropertyEnum() == AnnotationPropertyEnum.BOOLEAN) {
                addText = "=";
                i = 0;
            }

            PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText);


        } else {
            PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "=\"\"");
        }

        if(i != 0) {
            context.getEditor().getCaretModel().moveCaretRelatively(i, 0, false, false, true);
        }

    }
 
Example #10
Source File: MacroCustomFunctionInsertHandler.java    From intellij-latte with MIT License 4 votes vote down vote up
private void insertParenthesesCodeStyleAware(@NotNull Editor editor) {
	PhpInsertHandlerUtil.insertStringAtCaret(editor, "()");
}
 
Example #11
Source File: TwigAssetFunctionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull AssetLookupElement lookupElement) {
    context.getDocument().insertString(context.getStartOffset(), "{{ asset('");
    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "') }}");
}
 
Example #12
Source File: TwigExtensionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement, @NotNull TwigExtension twigExtension) {
    // {{ form_javasc|() }}
    // {{ form_javasc| }}
    if(PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "(")) {
        return;
    }

    FunctionInsertHandler.getInstance().handleInsert(context, lookupElement);

    // if first parameter is a string type; add quotes
    for (PsiElement psiElement : PhpElementsUtil.getPsiElementsBySignature(context.getProject(), twigExtension.getSignature())) {
        if(!(psiElement instanceof Function)) {
            continue;
        }

        Parameter[] parameters = ((Function) psiElement).getParameters();

        // skip Twig parameter, we need first function parameter
        int parameter = 0;
        if(twigExtension.getOption("needs_context") != null) {
            parameter++;
        }

        if(twigExtension.getOption("needs_environment") != null) {
            parameter++;
        }

        if(parameters.length <= parameter) {
            continue;
        }

        if(!isString(parameters[parameter].getType())) {
            continue;
        }

        // wrap caret with '' so we have foobar('<caret>')
        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "''");
        context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

        return;
    }
}
 
Example #13
Source File: TwigTranslationGeneratorAction.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) {
    int caretOffset = editor.getCaretModel().getOffset();
    if(caretOffset <= 0) {
        return;
    }

    PsiElement psiElement = getInjectedTwigElement(psiFile, editor);
    if(psiElement == null) {
        return;
    }

    PsiFile containingFile = psiElement.getContainingFile();
    if(!(containingFile instanceof TwigFile)) {
        return;
    }

    PsiElement element = TwigUtil.getElementOnTwigViewProvider(psiElement);
    TwigUtil.DomainScope twigFileDomainScope = TwigUtil.getTwigFileDomainScope(element != null ? element : psiElement);

    final String defaultDomain = twigFileDomainScope.getDefaultDomain();
    final String domain = twigFileDomainScope.getDomain();

    List<String> collect = TranslationUtil.getTranslationLookupElementsOnDomain(project, domain)
        .stream()
        .map(LookupElement::getLookupString)
        .sorted()
        .collect(Collectors.toList());

    final JBList<String> list = new JBList<>(collect);

    JBPopupFactory.getInstance().createListPopupBuilder(list)
        .setTitle(String.format("Symfony: Translations \"%s\"", StringUtils.abbreviate(domain, 20)))
        .setItemChoosenCallback(() -> {
            String selectedValue = list.getSelectedValue();

            new WriteCommandAction.Simple(editor.getProject(), String.format("Symfony: Add Translation \"%s\"", StringUtils.abbreviate(selectedValue, 20))) {
                @Override
                protected void run() {
                    String s;

                    if (!domain.equals(defaultDomain)) {
                        s = String.format("{{ '%s'|trans({}, '%s') }}", selectedValue, domain);
                    } else {
                        s = String.format("{{ '%s'|trans }}", selectedValue);
                    }

                    PhpInsertHandlerUtil.insertStringAtCaret(editor, s);
                }
            }.execute();
        })
        .createPopup()
        .showInBestPositionFor(editor);
}
 
Example #14
Source File: TwigTypeInsertHandler.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {

        PsiElement psiElement = lookupElement.getPsiElement();
        if(psiElement instanceof Method && needParameter((Method) psiElement)) {

            // check this:
            // {{ form_javasc|() }}
            // {{ form_javasc| }}
            if(PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), "(")) {
                return;
            }

            String addText = "()";
            PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), addText);

            context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

        }


    }
 
Example #15
Source File: ViewHelperArgumentInsertHandler.java    From idea-php-typo3-plugin with MIT License 3 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {

        if (PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), ":")) {
            return;
        }

        PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), ": ");

        context.getEditor().getCaretModel().moveCaretRelatively(0, 0, false, false, true);
    }