本文整理汇总了Java中com.intellij.psi.PsiElementFactory.createStatementFromText方法的典型用法代码示例。如果您正苦于以下问题:Java PsiElementFactory.createStatementFromText方法的具体用法?Java PsiElementFactory.createStatementFromText怎么用?Java PsiElementFactory.createStatementFromText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.PsiElementFactory
的用法示例。
在下文中一共展示了PsiElementFactory.createStatementFromText方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addAfter
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
/**
* Add new statement after given anchor statement creating code block, if necessary
*
* @param anchor existing statement
* @param newStatement a new statement which should be added after an existing one
* @return added physical statement
*/
public static PsiStatement addAfter(PsiStatement anchor, PsiStatement newStatement)
{
PsiElement oldStatement = anchor;
PsiElement parent = oldStatement.getParent();
while(parent instanceof PsiLabeledStatement)
{
oldStatement = parent;
parent = oldStatement.getParent();
}
final PsiElement result;
if(parent instanceof PsiCodeBlock)
{
result = parent.addAfter(newStatement, oldStatement);
}
else
{
PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject());
final PsiBlockStatement newBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", oldStatement);
final PsiElement codeBlock = newBlockStatement.getCodeBlock();
codeBlock.add(oldStatement);
codeBlock.add(newStatement);
result = ((PsiBlockStatement) oldStatement.replace(newBlockStatement)).getCodeBlock().getStatements()[1];
}
return (PsiStatement) result;
}
示例2: replaceStatement
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
protected static void replaceStatement(
@NotNull PsiStatement statement,
@NotNull @NonNls String newStatementText)
throws IncorrectOperationException {
final Project project = statement.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = psiFacade.getElementFactory();
final PsiStatement newStatement =
factory.createStatementFromText(newStatementText, statement);
final PsiElement replacementExp = statement.replace(newStatement);
final CodeStyleManager styleManager =
CodeStyleManager.getInstance(project);
styleManager.reformat(replacementExp);
}
示例3: replaceStatement
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
protected static void replaceStatement(@NonNls @NotNull String newStatementText, @NonNls @NotNull PsiStatement statement) {
final Project project = statement.getProject();
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, statement);
final PsiElement insertedElement = statement.replace(newStatement);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(insertedElement);
}
示例4: replaceStatementAndShorten
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
protected static void replaceStatementAndShorten(@NonNls @NotNull String newStatementText, @NonNls @NotNull PsiStatement statement) {
final Project project = statement.getProject();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
final PsiElementFactory factory = psiFacade.getElementFactory();
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, statement);
final PsiElement insertedElement = statement.replace(newStatement);
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
final PsiElement shortenedElement = javaCodeStyleManager.shortenClassReferences(insertedElement);
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(shortenedElement);
}
示例5: addStatementBefore
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
protected static void addStatementBefore(String newStatementText, PsiReturnStatement anchor) {
final Project project = anchor.getProject();
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, anchor);
final PsiElement addedStatement = anchor.getParent().addBefore(newStatement, anchor);
CodeStyleManager.getInstance(project).reformat(addedStatement);
}
示例6: createReturnThis
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
static PsiStatement createReturnThis(@NotNull PsiElementFactory psiElementFactory, @Nullable PsiElement context) {
return psiElementFactory.createStatementFromText("return this;", context);
}
示例7: addBefore
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
/**
* Adds new statements before given anchor statement creating a new code block, if necessary
*
* @param anchor existing statement
* @param newStatements the new statements which should be added before the existing one
* @return last added physical statement
*/
public static PsiStatement addBefore(PsiStatement anchor, PsiStatement... newStatements)
{
if(newStatements.length == 0)
{
throw new IllegalArgumentException();
}
PsiElement oldStatement = anchor;
PsiElement parent = oldStatement.getParent();
while(parent instanceof PsiLabeledStatement)
{
oldStatement = parent;
parent = oldStatement.getParent();
}
if(newStatements.length == 1 && oldStatement instanceof PsiEmptyStatement)
{
return (PsiStatement) oldStatement.replace(newStatements[0]);
}
PsiElement result = null;
if(parent instanceof PsiCodeBlock)
{
for(PsiStatement statement : newStatements)
{
result = parent.addBefore(statement, oldStatement);
}
}
else
{
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject());
final PsiBlockStatement newBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", oldStatement);
final PsiElement codeBlock = newBlockStatement.getCodeBlock();
for(PsiStatement newStatement : newStatements)
{
codeBlock.add(newStatement);
}
codeBlock.add(oldStatement);
final PsiStatement[] statements = ((PsiBlockStatement) oldStatement.replace(newBlockStatement)).getCodeBlock().getStatements();
result = statements[statements.length - 2];
}
return (PsiStatement) result;
}
示例8: sideEffectAwareRemove
import com.intellij.psi.PsiElementFactory; //导入方法依赖的package包/类
protected void sideEffectAwareRemove(Project project, PsiElement psiInitializer, PsiElement elementToDelete, PsiVariable variable)
{
if(!FileModificationService.getInstance().prepareFileForWrite(elementToDelete.getContainingFile()))
{
return;
}
final PsiElement declaration = variable.getParent();
final List<PsiElement> sideEffects = new ArrayList<PsiElement>();
boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects);
int res = RemoveUnusedVariableUtil.DELETE_ALL;
if(hasSideEffects)
{
hasSideEffects = PsiUtil.isStatement(psiInitializer);
res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable, FileEditorManager.getInstance(project).getSelectedTextEditor
(), hasSideEffects, sideEffects.get(0).getText(), variable.getTypeElement().getText() +
" " +
variable.getName() +
";<br>" +
PsiExpressionTrimRenderer.render((PsiExpression) psiInitializer));
}
try
{
if(res == RemoveUnusedVariableUtil.DELETE_ALL)
{
elementToDelete.delete();
}
else if(res == RemoveUnusedVariableUtil.MAKE_STATEMENT)
{
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null);
final PsiElement parent = elementToDelete.getParent();
if(parent instanceof PsiExpressionStatement)
{
parent.replace(statementFromText);
}
else
{
declaration.getParent().addAfter(statementFromText, declaration);
elementToDelete.delete();
}
}
}
catch(IncorrectOperationException e)
{
LOG.error(e);
}
}