Java Code Examples for com.intellij.psi.PsiFile#getFirstChild()

The following examples show how to use com.intellij.psi.PsiFile#getFirstChild() . 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: PsiFileHelper.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Collection<PsiNameIdentifierOwner> getExpressions(@NotNull PsiFile file, @Nullable String name) {
    Collection<PsiNameIdentifierOwner> result = new ArrayList<>();

    if (name != null) {
        PsiElement element = file.getFirstChild();
        while (element != null) {
            if (element instanceof PsiNameIdentifierOwner && name.equals(((PsiNameIdentifierOwner) element).getName())) {
                result.add((PsiNameIdentifierOwner) element);
            }
            element = element.getNextSibling();
        }
    }

    return result;
}
 
Example 2
Source File: DartHelper.java    From haystack with MIT License 5 votes vote down vote up
public static PsiElement createBlockFromText(Project myProject, String text) {
    PsiFile file = createDummyFile(myProject, "dummy(){" + text + "}");
    PsiElement child = file.getFirstChild();
    if (child instanceof DartFunctionDeclarationWithBodyOrNative) {
        DartFunctionBody functionBody =
                ((DartFunctionDeclarationWithBodyOrNative) child).getFunctionBody();
        IDartBlock block = PsiTreeUtil.getChildOfType(functionBody, IDartBlock.class);
        DartStatements statements = block == null ? null : block.getStatements();
        return statements;
    } else {
        return null;
    }
}
 
Example 3
Source File: DartHelper.java    From haystack with MIT License 5 votes vote down vote up
public static PsiElement createMethodFromText(Project myProject, String text) {
    PsiFile file = createDummyFile(myProject, "class dummy{\n" + text + "}");
    PsiElement child = file.getFirstChild();
    if (child instanceof DartClassDefinition) {
        DartClassBody body = PsiTreeUtil.getChildOfType(child, DartClassBody.class);
        DartClassMembers members = PsiTreeUtil.getChildOfType(body, DartClassMembers.class);
        return members.getFirstChild();
    } else {
        return null;
    }
}
 
Example 4
Source File: DartHelper.java    From haystack with MIT License 5 votes vote down vote up
public static PsiElement createClassFromText(Project myProject, String text) {
    PsiFile file = createDummyFile(myProject, text);
    PsiElement child = file.getFirstChild();
    if (child instanceof DartClassDefinition) {
        return child;
    } else {
        return null;
    }
}
 
Example 5
Source File: DartHelper.java    From haystack with MIT License 5 votes vote down vote up
public static DartClassMembers createFieldFromText(Project myProject, String text) {
    PsiFile file = createDummyFile(myProject, "class dummy{" + text + "}");
    PsiElement child = file.getFirstChild();
    if (child instanceof DartClassDefinition) {

        DartClassMembers members =
                ((DartClassDefinition) child).getClassBody().getClassMembers();
        return members;
    } else {
        return null;
    }
}
 
Example 6
Source File: BuildElementGenerator.java    From intellij with Apache License 2.0 5 votes vote down vote up
public Expression createExpressionFromText(String text) {
  PsiFile dummyFile = createDummyFile(text);
  PsiElement element = dummyFile.getFirstChild();
  if (element instanceof Expression) {
    return (Expression) element;
  }
  throw new RuntimeException("Could not parse text as expression: '" + text + "'");
}
 
Example 7
Source File: BuildElementGenerator.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Returns null if the text can't be parsed as a statement. */
@Nullable
public Statement createStatementFromText(String text) {
  PsiFile dummyFile = createDummyFile(text);
  PsiElement element = dummyFile.getFirstChild();
  if (element instanceof Statement) {
    return (Statement) element;
  }
  return null;
}
 
Example 8
Source File: SupressAddShebangInspectionQuickfix.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiFile file = descriptor.getPsiElement().getContainingFile();
    if (file == null) {
        return;
    }

    if (!FileModificationService.getInstance().preparePsiElementForWrite(file)) {
        return;
    }

    PsiComment suppressionComment = SupressionUtil.createSuppressionComment(project, inspectionId);

    PsiElement firstChild = file.getFirstChild();
    PsiElement inserted;
    if (firstChild != null) {
        inserted = file.addBefore(suppressionComment, firstChild);
    } else {
        inserted = file.add(suppressionComment);
    }

    if (inserted != null) {
        file.addAfter(BashPsiElementFactory.createNewline(project), inserted);
    }

    UndoUtil.markPsiFileForUndo(file);
}
 
Example 9
Source File: DoubleBracketsQuickfix.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private static PsiElement useDoubleBrackets(Project project, String command) {
    String newCommand = "[[" + command + "]]";
    for (Replacement replacement : Replacement.values()) {
        newCommand = replacement.apply(newCommand);
    }

    PsiFile dummyBashFile = BashPsiElementFactory.createDummyBashFile(project, newCommand);
    return dummyBashFile.getFirstChild();
}
 
Example 10
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static PsiElement getLocalServiceName(PsiFile psiFile, String serviceName) {

    if(!(psiFile.getFirstChild() instanceof XmlDocument)) {
        return null;
    }

    XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
    if(xmlTags == null) {
        return null;
    }

    for(XmlTag xmlTag: xmlTags) {
        if(xmlTag.getName().equals("container")) {
            for(XmlTag servicesTag: xmlTag.getSubTags()) {
                if(servicesTag.getName().equals("services")) {
                    for(XmlTag serviceTag: servicesTag.getSubTags()) {
                        String serviceNameId = serviceTag.getAttributeValue("id");
                        if(serviceNameId != null && serviceNameId.equalsIgnoreCase(serviceName)) {
                            return serviceTag;
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 11
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static PsiElement getLocalParameterName(PsiFile psiFile, String serviceName) {

    if(!(psiFile.getFirstChild() instanceof XmlDocumentImpl)) {
        return null;
    }

    XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
    if(xmlTags == null) {
        return null;
    }

    for(XmlTag xmlTag: xmlTags) {
        if(xmlTag.getName().equals("container")) {
            for(XmlTag servicesTag: xmlTag.getSubTags()) {
                if(servicesTag.getName().equals("parameters")) {
                    for(XmlTag serviceTag: servicesTag.getSubTags()) {
                        XmlAttribute attrValue = serviceTag.getAttribute("key");
                        if(attrValue != null) {
                            String serviceNameId = attrValue.getValue();
                            if(serviceNameId != null && serviceNameId.equals(serviceName)) {
                                return serviceTag;
                            }
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 12
Source File: CommonHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static HashMap<String, String> parsePhpArrayConfig(Project project, String filePath) {
    PsiFile pf = CommonHelper.getPsiFile(project, filePath);
    HashMap<String, String> hashx = new HashMap<String, String>();
    if (pf != null) {
        PsiElement groupStatement = pf.getFirstChild();
        if (groupStatement != null) {
            for (PsiElement pl : groupStatement.getChildren()) {
                if (pl.toString().equals("Return")) {
                    PsiElement[] pl2 = pl.getChildren();
                    if (pl2.length > 0 && pl2[0].toString().equals("Array creation expression")) {
                        ArrayCreationExpressionImpl ar = (ArrayCreationExpressionImpl) pl2[0];
                        if (ar.getChildren().length > 0) {
                            for (ArrayHashElement he : ar.getHashElements()) {
                                String val = he.getValue().getText();
                                String key = he.getKey().getText();
                                hashx.put(CommonHelper.rmQuotes(
                                        key.substring(0, key.length() > 40 ? 40 : key.length())
                                ),
                                        CommonHelper.rmQuotes(
                                                val.substring(0, val.length() > 40 ? 40 : val.length()))
                                );
                            }
                        }
                    }
                }
            }
        }
    }
    return hashx;
}
 
Example 13
Source File: NewArrayValueLookupElement.java    From yiistorm with MIT License 5 votes vote down vote up
public void insertIntoArrayConfig(String string, String openFilePath) {

        String relpath = openFilePath.replace(project.getBasePath(), "").substring(1).replace("\\", "/");
        VirtualFile vf = project.getBaseDir().findFileByRelativePath(relpath);

        if (vf == null) {
            return;
        }
        PsiFile pf = PsiManager.getInstance(project).findFile(vf);

        String lineSeparator = " " + ProjectCodeStyleSettingsManager.getSettings(project).getLineSeparator();
        if (vf != null) {

            PsiElement groupStatement = pf.getFirstChild();
            if (groupStatement != null) {
                PsiDocumentManager.getInstance(project).commitDocument(pf.getViewProvider().getDocument());

                pf.getManager().reloadFromDisk(pf);
                for (PsiElement pl : groupStatement.getChildren()) {
                    if (pl.toString().equals("Return")) {
                        PsiElement[] pl2 = pl.getChildren();
                        if (pl2.length > 0 && pl2[0].toString().equals("Array creation expression")) {
                            ArrayCreationExpressionImpl ar = (ArrayCreationExpressionImpl) pl2[0];

                            ArrayHashElement p = (ArrayHashElement) PhpPsiElementFactory.createFromText(project,
                                    PhpElementTypes.HASH_ARRAY_ELEMENT,
                                    "array('" + string + "'=>'')");
                            PsiElement closingBrace = ar.getLastChild();
                            String preLast = closingBrace.getPrevSibling().toString();
                            if (!preLast.equals("Comma")) {
                                pf.getViewProvider().getDocument().insertString(
                                        closingBrace.getTextOffset(), "," + lineSeparator + p.getText());
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
 
Example 14
Source File: Intentions.java    From glsl4idea with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected PsiElement createExpressionFromText(PsiElement element, String expression) {
    String name = "dummy.glsl";
    //todo: parser must be able to handle this somehow...
    PsiFile dummyFile = PsiFileFactory.getInstance(element.getProject()).createFileFromText(name, GLSLSupportLoader.GLSL.getLanguage(), expression);
    return dummyFile.getFirstChild();
}