Skip to content
Snippets Groups Projects
Commit 1f5392d9 authored by Johannes Mey's avatar Johannes Mey
Browse files

rename more occurences of ast type names. use at own risk!

parent 1137b28b
Branches
No related tags found
1 merge request!1Resolve "Renaming of Nonterminal Does Not Rename All Occurences"
...@@ -215,6 +215,10 @@ type ::= reference_type | primitive_type ...@@ -215,6 +215,10 @@ type ::= reference_type | primitive_type
reference_type ::= ( primitive_type (LBRACKET RBRACKET)+ ) | ( class_or_interface_type (LBRACKET RBRACKET)* ) reference_type ::= ( primitive_type (LBRACKET RBRACKET)+ ) | ( class_or_interface_type (LBRACKET RBRACKET)* )
class_or_interface_type ::= java_identifier type_arguments? (DOT java_identifier type_arguments? )* class_or_interface_type ::= java_identifier type_arguments? (DOT java_identifier type_arguments? )*
{
extends="org.jastadd.tooling.aspect.psi.impl.JastAddAspectClassOrInterfaceTypeImplExtension"
implements="org.jastadd.tooling.grammar.psi.GrammarNamedElement"
}
type_arguments ::= LT (type_argument (COMMA type_argument)* )? GT type_arguments ::= LT (type_argument (COMMA type_argument)* )? GT
......
...@@ -5,6 +5,7 @@ import com.intellij.patterns.PlatformPatterns; ...@@ -5,6 +5,7 @@ import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*; import com.intellij.psi.*;
import com.intellij.util.ProcessingContext; import com.intellij.util.ProcessingContext;
import org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName; import org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName;
import org.jastadd.tooling.aspect.psi.JastAddAspectClassOrInterfaceType;
import org.jastadd.tooling.grammar.GrammarReference; import org.jastadd.tooling.grammar.GrammarReference;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -27,6 +28,23 @@ public class AspectReferenceContributor extends PsiReferenceContributor { ...@@ -27,6 +28,23 @@ public class AspectReferenceContributor extends PsiReferenceContributor {
return PsiReference.EMPTY_ARRAY; return PsiReference.EMPTY_ARRAY;
} }
}); });
registrar.registerReferenceProvider(PlatformPatterns.psiElement(JastAddAspectClassOrInterfaceType.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element,
@NotNull ProcessingContext context) {
JastAddAspectClassOrInterfaceType typeReference = (JastAddAspectClassOrInterfaceType) element;
if (typeReference.getTypeArgumentsList().isEmpty() && typeReference.getJavaIdentifierList().size() == 1) {
String value = typeReference.getJavaIdentifierList().get(0).getText();
if (value != null) {
TextRange range = new TextRange(0, value.length());
return new PsiReference[]{new GrammarReference(element, range)};
}
}
return PsiReference.EMPTY_ARRAY;
}
});
} }
} }
...@@ -11,7 +11,7 @@ public class AspectElementFactory { ...@@ -11,7 +11,7 @@ public class AspectElementFactory {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static JastAddAspectAstTypeName createComponentName(Project project, String name) { public static JastAddAspectAstTypeName createAstTypeName(Project project, String name) {
final AspectFile file = createFile(project, "aspect A{syn int " + name + ".attributeName();}"); final AspectFile file = createFile(project, "aspect A{syn int " + name + ".attributeName();}");
PsiElement result = file.getFirstChild().findElementAt(17); PsiElement result = file.getFirstChild().findElementAt(17);
if (result != null) { if (result != null) {
...@@ -20,6 +20,15 @@ public class AspectElementFactory { ...@@ -20,6 +20,15 @@ public class AspectElementFactory {
return null; return null;
} }
public static JastAddAspectClassOrInterfaceType createClassOrInterfaceType(Project project, String name) {
final AspectFile file = createFile(project, "aspect Navigation{X<" + name + "> X.z=0;}");
PsiElement result = file.getFirstChild().findElementAt(20);
if (result != null) {
return (JastAddAspectClassOrInterfaceType) result.getParent().getParent();
}
return null;
}
public static AspectFile createFile(Project project, String text) { public static AspectFile createFile(Project project, String text) {
String name = "dummy.jrag"; String name = "dummy.jrag";
return (AspectFile) PsiFileFactory.getInstance(project). return (AspectFile) PsiFileFactory.getInstance(project).
......
package org.jastadd.tooling.aspect.psi;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.AbstractElementManipulator;
import com.intellij.psi.ElementManipulator;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JastAddAspectClassOrInterfaceTypeManipulator extends AbstractElementManipulator<JastAddAspectClassOrInterfaceType> implements ElementManipulator<JastAddAspectClassOrInterfaceType> {
/**
* Changes the element's text to the given new text.
*
* @param element element to be changed
* @param range range within the element
* @param newContent new element text
* @return changed element
* @throws IncorrectOperationException if something goes wrong
*/
@Nullable
@Override
public JastAddAspectClassOrInterfaceType handleContentChange(@NotNull JastAddAspectClassOrInterfaceType element, @NotNull TextRange range, String newContent) {
try {
if (element.getJavaIdentifierList().size() != 1 || !element.getTypeArgumentsList().isEmpty()) {
throw new IncorrectOperationException("Operation only valid for \"primitive\" instances of JastAddAspectClassOrInterfaceType");
}
return (JastAddAspectClassOrInterfaceType) element.setName(range.replace(element.getText(), newContent));
} catch (Exception e) { // e.g., in case the range is wrong
throw new IncorrectOperationException(e);
}
}
}
...@@ -4,9 +4,7 @@ import com.intellij.lang.ASTNode; ...@@ -4,9 +4,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jastadd.tooling.aspect.psi.AspectElementFactory; import org.jastadd.tooling.aspect.psi.AspectElementFactory;
import org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName; import org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName;
import org.jastadd.tooling.grammar.psi.GrammarElementFactory;
import org.jastadd.tooling.grammar.psi.GrammarNamedElement; import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jastadd.tooling.grammar.psi.impl.GrammarNamedElementImpl; import org.jastadd.tooling.grammar.psi.impl.GrammarNamedElementImpl;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -25,7 +23,7 @@ public class JastAddAspectAstTypeNameImplExtension extends GrammarNamedElementIm ...@@ -25,7 +23,7 @@ public class JastAddAspectAstTypeNameImplExtension extends GrammarNamedElementIm
// FIXME this can break the grammar when the type is used in an unnamed component (and in many other cases probably) // FIXME this can break the grammar when the type is used in an unnamed component (and in many other cases probably)
ASTNode keyNode = getNode().getFirstChildNode(); ASTNode keyNode = getNode().getFirstChildNode();
if (keyNode != null) { if (keyNode != null) {
JastAddAspectAstTypeName name = AspectElementFactory.createComponentName(getProject(), newName); JastAddAspectAstTypeName name = AspectElementFactory.createAstTypeName(getProject(), newName);
ASTNode newKeyNode = name.getNode().getFirstChildNode(); ASTNode newKeyNode = name.getNode().getFirstChildNode();
getNode().replaceChild(keyNode, newKeyNode); getNode().replaceChild(keyNode, newKeyNode);
} }
......
package org.jastadd.tooling.aspect.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jastadd.tooling.aspect.psi.AspectElementFactory;
import org.jastadd.tooling.aspect.psi.JastAddAspectClassOrInterfaceType;
import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
import org.jastadd.tooling.grammar.psi.impl.GrammarNamedElementImpl;
import org.jetbrains.annotations.NotNull;
public class JastAddAspectClassOrInterfaceTypeImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
public JastAddAspectClassOrInterfaceTypeImplExtension(@NotNull ASTNode node) {
super(node);
}
public String getName() {
// this finds the *first* ID, which is what we want
return getNode().getText();
}
public PsiElement setName(@NotNull String newName) {
// FIXME this can break the grammar when the type is used in an unnamed component (and in many other cases probably)
ASTNode keyNode = getNode().getFirstChildNode();
if (keyNode != null) {
JastAddAspectClassOrInterfaceType name = AspectElementFactory.createClassOrInterfaceType(getProject(), newName);
ASTNode newKeyNode = name.getNode().getFirstChildNode();
getNode().replaceChild(keyNode, newKeyNode);
}
return this;
}
public PsiElement getNameIdentifier() {
return getNode().getPsi();
}
}
...@@ -84,6 +84,9 @@ ...@@ -84,6 +84,9 @@
<lang.elementManipulator forClass="org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName" <lang.elementManipulator forClass="org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName"
implementationClass="org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeNameManipulator"/> implementationClass="org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeNameManipulator"/>
<lang.elementManipulator forClass="org.jastadd.tooling.aspect.psi.JastAddAspectClassOrInterfaceType"
implementationClass="org.jastadd.tooling.aspect.psi.JastAddAspectClassOrInterfaceTypeManipulator"/>
</extensions> </extensions>
<actions> <actions>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment