Skip to content

Commit edd9ee5

Browse files
committed
Intention action to migrate TwigExtension getFilters(), getFunctions(), and getTests() methods to use PHP attributes (#[AsTwigFilter], #[AsTwigFunction], #[AsTwigTest]).
1 parent 58f9c5c commit edd9ee5

File tree

10 files changed

+1337
-85
lines changed

10 files changed

+1337
-85
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
1818
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
1919
import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil;
20+
import fr.adrienbrault.idea.symfony2plugin.util.CodeUtil;
2021
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2122
import icons.SymfonyIcons;
2223
import org.jetbrains.annotations.NotNull;
@@ -80,7 +81,7 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
8081
psiDocManager.commitDocument(document);
8182
psiDocManager.doPostponedOperationsAndUnblockDocument(document);
8283

83-
reformatAddedAttribute(project, document, methodStartOffset);
84+
CodeUtil.reformatAddedAttribute(project, document, methodStartOffset);
8485

8586
// position caret after the opening quote of the path: #[Route('<caret>/...
8687
if (editor != null) {
@@ -89,34 +90,6 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
8990
}
9091
}
9192

92-
private void reformatAddedAttribute(@NotNull Project project, @NotNull Document document, int attributeStartOffset) {
93-
PsiDocumentManager psiDocManager = PsiDocumentManager.getInstance(project);
94-
PsiFile freshFile = psiDocManager.getPsiFile(document);
95-
if (freshFile == null) {
96-
return;
97-
}
98-
99-
PsiElement elementAtOffset = freshFile.findElementAt(attributeStartOffset);
100-
if (elementAtOffset == null) {
101-
return;
102-
}
103-
104-
Method freshMethod = PsiTreeUtil.getParentOfType(elementAtOffset, Method.class);
105-
if (freshMethod == null) {
106-
return;
107-
}
108-
109-
PsiElement nameIdentifier = freshMethod.getNameIdentifier();
110-
if (nameIdentifier == null) {
111-
return;
112-
}
113-
114-
int endOffset = nameIdentifier.getTextRange().getEndOffset();
115-
116-
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
117-
codeStyleManager.reformatRange(freshFile, attributeStartOffset, endOffset);
118-
}
119-
12093
@Override
12194
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) {
12295
if (!Symfony2ProjectComponent.isEnabled(project)) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/CommandToInvokableIntention.java

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.intellij.util.IncorrectOperationException;
1515
import com.jetbrains.php.lang.psi.elements.*;
1616
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
17+
import fr.adrienbrault.idea.symfony2plugin.util.CodeUtil;
1718
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1819
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
1920
import icons.SymfonyIcons;
@@ -74,61 +75,8 @@ private void removeCommandExtends(@NotNull PhpClass phpClass) {
7475
return;
7576
}
7677

77-
// Navigate the PSI tree to find the extends clause
78-
var extendsList = phpClass.getExtendsList();
79-
80-
List<ClassReference> references = extendsList.getReferenceElements();
81-
if (references.isEmpty()) {
82-
return;
83-
}
84-
85-
// Find the Command reference
86-
ClassReference commandRef = null;
87-
for (ClassReference classRef : references) {
88-
PhpClass resolvedClass = (PhpClass) classRef.resolve();
89-
if (resolvedClass != null && "\\Symfony\\Component\\Console\\Command\\Command".equals(resolvedClass.getFQN())) {
90-
commandRef = classRef;
91-
break;
92-
}
93-
}
94-
95-
if (commandRef == null) {
96-
return;
97-
}
98-
99-
// If this is the only extends, remove the entire extends clause using Document API
100-
// This avoids PSI null issues by editing at the text level
101-
if (references.size() == 1) {
102-
// Find whitespace before the extends list - we want to delete from there
103-
// to include any spaces between the class name and "extends"
104-
PsiElement startElement = extendsList;
105-
PsiElement prev = extendsList.getPrevSibling();
106-
107-
// Skip backwards through whitespace to find the actual start
108-
while (prev != null && (prev.getText().trim().isEmpty() || "extends".equals(prev.getText().trim()))) {
109-
startElement = prev;
110-
prev = prev.getPrevSibling();
111-
}
112-
113-
// Use Document API to delete the text range
114-
PsiFile file = phpClass.getContainingFile();
115-
Document document = PsiDocumentManager.getInstance(phpClass.getProject()).getDocument(file);
116-
117-
if (document != null) {
118-
int startOffset = startElement.getTextRange().getStartOffset();
119-
int endOffset = extendsList.getTextRange().getEndOffset();
120-
121-
document.deleteString(startOffset, endOffset);
122-
123-
// Commit and synchronize PSI
124-
PsiDocumentManager psiDocManager = PsiDocumentManager.getInstance(phpClass.getProject());
125-
psiDocManager.commitDocument(document);
126-
psiDocManager.doPostponedOperationsAndUnblockDocument(document);
127-
}
128-
} else {
129-
// Multiple extends, just remove this one reference (PSI deletion is safe here)
130-
commandRef.delete();
131-
}
78+
// Use shared utility method to remove the extends clause
79+
CodeUtil.removeExtendsClause(phpClass, "\\Symfony\\Component\\Console\\Command\\Command");
13280
}
13381

13482
private void optimizeImports(@NotNull PsiFile file) {

0 commit comments

Comments
 (0)