下面列出了com.intellij.psi.PsiFile#getFirstChild ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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 + "'");
}
/** 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;
}
@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);
}
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();
}
@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;
}
@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;
}
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;
}
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;
}
}
}
}
}
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();
}