use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class StatementToCompoundIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiPerlStatementImpl statement = getStatement(element);
if (statement == null) {
return;
}
PsiPerlStatementModifier modifier = statement.getModifier();
if (modifier == null) {
return;
}
PsiElement keyword = modifier.getFirstChild();
if (keyword == null) {
return;
}
PsiPerlExpr modifierExpression = PsiTreeUtil.getChildOfType(modifier, PsiPerlExpr.class);
PsiPerlExpr valueExpression = modifierExpression instanceof PsiPerlParenthesisedExpr ? ((PsiPerlParenthesisedExpr) modifierExpression).getExpr() : modifierExpression;
String conditionText = valueExpression == null ? "" : valueExpression.getText();
TextRange modifierRangeInStatement = TextRange.from(modifier.getStartOffsetInParent(), modifier.getTextLength());
String statementText = modifierRangeInStatement.replace(statement.getText(), "");
StringBuilder newCode = new StringBuilder();
newCode.append(keyword.getText()).append("(").append(conditionText).append("){\n").append(statementText).append("\n}");
PerlFileImpl fakeFile = PerlElementFactory.createFile(project, newCode.toString());
PsiElement[] children = fakeFile.getChildren();
assert children.length == 1;
statement.replace(children[0]);
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlUseVarsQuickFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
Collection<PerlVariableDeclarationElement> declarations = myVariablesProvider.getValue();
if (declarations.isEmpty()) {
startElement.delete();
return;
}
StringBuilder newCode = new StringBuilder("our");
if (declarations.size() > 1) {
newCode.append("(");
}
newCode.append(StringUtil.join(ContainerUtil.map(declarations, PsiElement::getText), ","));
if (declarations.size() > 1) {
newCode.append(")");
}
newCode.append(";");
PerlFileImpl fakeFile = PerlElementFactory.createFile(myStartElement.getProject(), newCode.toString());
PsiPerlStatement newElement = PsiTreeUtil.findChildOfType(fakeFile, PsiPerlStatement.class);
if (newElement != null) {
startElement.replace(newElement);
}
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlRenameFileProcessor method getPostRenameCallback.
@Nullable
@Override
public Runnable getPostRenameCallback(final PsiElement element, String newName, RefactoringElementListener elementListener) {
if (newName.endsWith(".pm")) {
final Project project = element.getProject();
final String currentPackageName = ((PerlFileImpl) element).getFilePackageName();
if (currentPackageName != null) {
String[] nameChunks = currentPackageName.split(PerlPackageUtil.PACKAGE_SEPARATOR);
nameChunks[nameChunks.length - 1] = newName.replaceFirst("\\.pm$", "");
final String newPackageName = StringUtils.join(nameChunks, PerlPackageUtil.PACKAGE_SEPARATOR);
final String newFileName = ((PerlFileImpl) element).getVirtualFile().getParent().getPath() + '/' + newName;
return () -> {
VirtualFile newFile = LocalFileSystem.getInstance().findFileByPath(newFileName);
if (newFile != null) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(newFile);
if (psiFile != null) {
final RenameRefactoring[] refactoring = { null };
for (PerlNamespaceDefinitionElement namespaceDefinition : PsiTreeUtil.findChildrenOfType(psiFile, PerlNamespaceDefinitionElement.class)) {
if (currentPackageName.equals(namespaceDefinition.getPackageName())) {
if (refactoring[0] == null) {
refactoring[0] = RefactoringFactory.getInstance(psiFile.getProject()).createRename(namespaceDefinition, newPackageName);
} else {
refactoring[0].addElement(namespaceDefinition, newPackageName);
}
}
}
if (refactoring[0] != null) {
ApplicationManager.getApplication().invokeLater(refactoring[0]::run);
}
}
}
};
}
}
return super.getPostRenameCallback(element, newName, elementListener);
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlFormattingScalarDerefCollapse method apply.
@Override
public int apply() {
int delta = 0;
if (myScalarElement.isValid() && myIndexElement.isValid()) {
String newCode = "$" + myScalarElement.getNode().getText() + myIndexElement.getNode().getText() + ";";
PerlFileImpl newFile = PerlElementFactory.createFile(myScalarElement.getProject(), newCode);
PsiPerlStatement statement = PsiTreeUtil.findChildOfType(newFile, PsiPerlStatement.class);
if (statement != null) {
delta = new PerlFormattingReplace(myScalarElement, myIndexElement, statement.getFirstChild(), statement.getFirstChild()).apply();
}
}
return delta;
}
use of com.perl5.lang.perl.psi.impl.PerlFileImpl in project Perl5-IDEA by Camelcade.
the class PerlFormattingSimpleDereferenceUnwrap method apply.
@Override
public int apply() {
int delta = 0;
if (myCastElement.isValid() && myVariableElement.isValid()) {
String newCode = getCode();
PerlFileImpl newFile = PerlElementFactory.createFile(myCastElement.getProject(), newCode);
PerlCastExpression newCastExpression = PsiTreeUtil.findChildOfType(newFile, PerlCastExpression.class);
if (newCastExpression != null) {
delta = new PerlFormattingReplace(myCastElement, newCastExpression).apply();
}
}
return delta;
}
Aggregations