Search in sources :

Example 1 with LookupElementDecorator

use of com.intellij.codeInsight.lookup.LookupElementDecorator in project intellij-community by JetBrains.

the class XmlClosingTagInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    Editor editor = context.getEditor();
    Document document = editor.getDocument();
    Project project = context.getProject();
    if (item instanceof LookupElementDecorator) {
        ((LookupElementDecorator) item).getDelegate().handleInsert(context);
    }
    PsiDocumentManager.getInstance(project).commitDocument(document);
    int lineOffset = document.getLineStartOffset(document.getLineNumber(editor.getCaretModel().getOffset()));
    CodeStyleManager.getInstance(project).adjustLineIndent(document, lineOffset);
}
Also used : Project(com.intellij.openapi.project.Project) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 2 with LookupElementDecorator

use of com.intellij.codeInsight.lookup.LookupElementDecorator in project intellij-community by JetBrains.

the class JavaConstructorCallElement method markClassItemWrapped.

private void markClassItemWrapped(@NotNull LookupElement classItem) {
    LookupElement delegate = classItem;
    while (true) {
        delegate.putUserData(WRAPPING_CONSTRUCTOR_CALL, this);
        if (!(delegate instanceof LookupElementDecorator))
            break;
        delegate = ((LookupElementDecorator) delegate).getDelegate();
    }
}
Also used : LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 3 with LookupElementDecorator

use of com.intellij.codeInsight.lookup.LookupElementDecorator in project intellij-community by JetBrains.

the class MavenPomXmlCompletionTagListenerContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (TemplateManager.getInstance(parameters.getOriginalFile().getProject()).getActiveTemplate(parameters.getEditor()) != null) {
        // Don't brake the template.
        return;
    }
    PsiFile psiFile = parameters.getOriginalFile();
    if (!(psiFile instanceof XmlFile))
        return;
    if (!MavenDomUtil.isProjectFile(psiFile))
        return;
    DomFileDescription<?> description = DomManager.getDomManager(psiFile.getProject()).getDomFileDescription((XmlFile) psiFile);
    if (!(description instanceof MavenDomProjectModelDescription))
        return;
    result.runRemainingContributors(parameters, r -> {
        final LookupElement lookupElement = r.getLookupElement();
        if (myHandledTags.contains(lookupElement.getLookupString())) {
            LookupElement decorator = LookupElementDecorator.withInsertHandler(lookupElement, new InsertHandler<LookupElementDecorator<LookupElement>>() {

                @Override
                public void handleInsert(final InsertionContext context, LookupElementDecorator<LookupElement> item) {
                    lookupElement.handleInsert(context);
                    Object object = lookupElement.getObject();
                    if ("dependency".equals(lookupElement.getLookupString()) && object instanceof XmlTag && "maven-4.0.0.xsd".equals(((XmlTag) object).getContainingFile().getName())) {
                        context.commitDocument();
                        CaretModel caretModel = context.getEditor().getCaretModel();
                        PsiElement psiElement = context.getFile().findElementAt(caretModel.getOffset());
                        XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
                        if (xmlTag != null) {
                            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(xmlTag);
                            if (domElement instanceof MavenDomDependency) {
                                String s = "\n<groupId></groupId>\n<artifactId></artifactId>\n";
                                context.getDocument().insertString(caretModel.getOffset(), s);
                                caretModel.moveToOffset(caretModel.getOffset() + s.length() - "</artifactId>\n".length());
                                context.commitDocument();
                                new ReformatCodeProcessor(context.getProject(), context.getFile(), xmlTag.getTextRange(), false).run();
                                MavenDependencyCompletionUtil.invokeCompletion(context, CompletionType.BASIC);
                            }
                        }
                    }
                }
            });
            r = r.withLookupElement(decorator);
        }
        result.passResult(r);
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) XmlFile(com.intellij.psi.xml.XmlFile) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) DomElement(com.intellij.util.xml.DomElement) MavenDomProjectModelDescription(org.jetbrains.idea.maven.dom.MavenDomProjectModelDescription) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

LookupElementDecorator (com.intellij.codeInsight.lookup.LookupElementDecorator)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 DomElement (com.intellij.util.xml.DomElement)1 MavenDomProjectModelDescription (org.jetbrains.idea.maven.dom.MavenDomProjectModelDescription)1 MavenDomDependency (org.jetbrains.idea.maven.dom.model.MavenDomDependency)1