Search in sources :

Example 11 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class PsiJavaParserFacadeImpl method createTypeParameterFromText.

@NotNull
@Override
public PsiTypeParameter createTypeParameterFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException {
    final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, TYPE_PARAMETER, level(context)), context);
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
    if (!(element instanceof PsiTypeParameter)) {
        throw new IncorrectOperationException("Incorrect type parameter '" + text + "'");
    }
    return (PsiTypeParameter) element;
}
Also used : JavaDummyElement(com.intellij.psi.impl.source.JavaDummyElement) DummyHolder(com.intellij.psi.impl.source.DummyHolder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class PsiJavaParserFacadeImpl method createTypeElementFromText.

@NotNull
@Override
public PsiTypeElement createTypeElementFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException {
    final LanguageLevel level = level(context);
    final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, TYPE, level), context);
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
    if (!(element instanceof PsiTypeElement)) {
        throw new IncorrectOperationException("Incorrect type '" + text + "' (" + level + ")");
    }
    return (PsiTypeElement) element;
}
Also used : JavaDummyElement(com.intellij.psi.impl.source.JavaDummyElement) LanguageLevel(com.intellij.pom.java.LanguageLevel) DummyHolder(com.intellij.psi.impl.source.DummyHolder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class JavaResolveUtil method resolveImaginarySuperCallInThisPlace.

/**
   * @return the constructor (or a class if there are none)
   * which the "{@code super();}" no-args call resolves to if inserted in the {@code place} (typically it would be inserted in the sub class constructor)
   * No code modifications happen in this method; it's used for resolving multiple overloaded constructors.
   */
public static PsiElement resolveImaginarySuperCallInThisPlace(@NotNull PsiMember place, @NotNull Project project, @NotNull PsiClass superClassWhichTheSuperCallMustResolveTo) {
    PsiExpressionListImpl expressionList = new PsiExpressionListImpl();
    final DummyHolder result = DummyHolderFactory.createHolder(PsiManager.getInstance(project), place);
    final FileElement holder = result.getTreeElement();
    holder.rawAddChildren((TreeElement) expressionList.getNode());
    return PsiResolveHelper.SERVICE.getInstance(project).resolveConstructor(PsiTypesUtil.getClassType(superClassWhichTheSuperCallMustResolveTo), expressionList, place).getElement();
}
Also used : DummyHolder(com.intellij.psi.impl.source.DummyHolder) PsiExpressionListImpl(com.intellij.psi.impl.source.tree.java.PsiExpressionListImpl) FileElement(com.intellij.psi.impl.source.tree.FileElement)

Example 14 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class JavaSourceUtil method addParenthToReplacedChild.

public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) {
    CompositeElement parenthExpr = ASTFactory.composite(parenthType);
    TreeElement dummyExpr = (TreeElement) newChild.clone();
    final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild);
    new DummyHolder(manager, parenthExpr, null, charTableByTree);
    parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree);
    parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "("));
    parenthExpr.rawAddChildren(dummyExpr);
    parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")"));
    try {
        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
        PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr));
        parenthExpr = (CompositeElement) SourceTreeToPsiMap.psiToTreeNotNull(formatted);
    } catch (IncorrectOperationException e) {
        // should not happen
        LOG.error(e);
    }
    newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild));
    dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild);
    // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification
    TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr));
    return parenthExpr;
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) DummyHolder(com.intellij.psi.impl.source.DummyHolder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CharTable(com.intellij.util.CharTable)

Example 15 with DummyHolder

use of com.intellij.psi.impl.source.DummyHolder in project intellij-community by JetBrains.

the class PsiJavaParserFacadeImpl method createResourceFromText.

@NotNull
@Override
public PsiResourceVariable createResourceFromText(@NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException {
    final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, RESOURCE, level(context)), context);
    final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
    if (!(element instanceof PsiResourceVariable)) {
        throw new IncorrectOperationException("Incorrect resource '" + text + "'");
    }
    return (PsiResourceVariable) element;
}
Also used : JavaDummyElement(com.intellij.psi.impl.source.JavaDummyElement) DummyHolder(com.intellij.psi.impl.source.DummyHolder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DummyHolder (com.intellij.psi.impl.source.DummyHolder)23 IncorrectOperationException (com.intellij.util.IncorrectOperationException)15 NotNull (org.jetbrains.annotations.NotNull)15 JavaDummyElement (com.intellij.psi.impl.source.JavaDummyElement)14 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 ASTNode (com.intellij.lang.ASTNode)2 Project (com.intellij.openapi.project.Project)2 LanguageLevel (com.intellij.pom.java.LanguageLevel)2 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)2 CharTable (com.intellij.util.CharTable)2 Nullable (org.jetbrains.annotations.Nullable)2 FileASTNode (com.intellij.lang.FileASTNode)1 Language (com.intellij.lang.Language)1 PsiBuilder (com.intellij.lang.PsiBuilder)1 Lexer (com.intellij.lexer.Lexer)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 FileType (com.intellij.openapi.fileTypes.FileType)1 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1