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

Merge branch 'bugfix/nonterminal-renaming' into 'main'

Resolve "Renaming of Nonterminal Does Not Rename All Occurences"

Closes #1

See merge request !1
parents 84a51cae a96dfdce
No related branches found
No related tags found
1 merge request!1Resolve "Renaming of Nonterminal Does Not Rename All Occurences"
Pipeline #11889 passed
Showing
with 91 additions and 50 deletions
...@@ -7,7 +7,7 @@ import com.intellij.util.ProcessingContext; ...@@ -7,7 +7,7 @@ import com.intellij.util.ProcessingContext;
import org.jastadd.tooling.grammar.parser.GrammarTypes; import org.jastadd.tooling.grammar.parser.GrammarTypes;
import org.jastadd.tooling.grammar.psi.GrammarElementFactory; import org.jastadd.tooling.grammar.psi.GrammarElementFactory;
import org.jastadd.tooling.grammar.psi.GrammarTypeDecl; import org.jastadd.tooling.grammar.psi.GrammarTypeDecl;
import org.jastadd.tooling.grammar.psi.GrammarTypeReference; import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jastadd.tooling.util.JastAddIcons; import org.jastadd.tooling.util.JastAddIcons;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -24,7 +24,7 @@ public class GrammarCompletionContributor extends CompletionContributor { ...@@ -24,7 +24,7 @@ public class GrammarCompletionContributor extends CompletionContributor {
@NotNull ProcessingContext context, @NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) { @NotNull CompletionResultSet resultSet) {
for (GrammarTypeDecl decl : GrammarUtil.findTypeDecl(parameters.getPosition().getProject())) { for (GrammarTypeDecl decl : GrammarUtil.findTypeDecl(parameters.getPosition().getProject())) {
GrammarTypeReference ref = GrammarElementFactory.createTypeReference(parameters.getPosition().getProject(), decl.getName()); GrammarTypeName ref = GrammarElementFactory.createTypeName(parameters.getPosition().getProject(), decl.getName());
resultSet.addElement(LookupElementBuilder.create(ref).withIcon(JastAddIcons.FILE).withTypeText(decl.getContainingFile().getName())); resultSet.addElement(LookupElementBuilder.create(ref).withIcon(JastAddIcons.FILE).withTypeText(decl.getContainingFile().getName()));
} }
} }
......
...@@ -3,8 +3,7 @@ package org.jastadd.tooling.grammar; ...@@ -3,8 +3,7 @@ package org.jastadd.tooling.grammar;
import com.intellij.lang.refactoring.RefactoringSupportProvider; import com.intellij.lang.refactoring.RefactoringSupportProvider;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.psi.GrammarDeclaredName; import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jastadd.tooling.grammar.psi.GrammarTypeReference;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
...@@ -14,7 +13,7 @@ public class GrammarRefactoringSupportProvider extends RefactoringSupportProvide ...@@ -14,7 +13,7 @@ public class GrammarRefactoringSupportProvider extends RefactoringSupportProvide
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement elementToRename, @Nullable PsiElement context) { public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement elementToRename, @Nullable PsiElement context) {
// in-place rename is still not available since not all requirements are met // in-place rename is still not available since not all requirements are met
// see https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006918740-How-do-I-enable-in-place-rename-Renaming-via-dialog-works-fine- // see https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006918740-How-do-I-enable-in-place-rename-Renaming-via-dialog-works-fine-
return (elementToRename instanceof GrammarDeclaredName) || (elementToRename instanceof GrammarTypeReference); return elementToRename instanceof GrammarTypeName;
} }
} }
...@@ -48,7 +48,7 @@ public class GrammarReference extends PsiReferenceBase<PsiElement> implements Ps ...@@ -48,7 +48,7 @@ public class GrammarReference extends PsiReferenceBase<PsiElement> implements Ps
List<GrammarTypeDecl> typeDecls = GrammarUtil.findTypeDecl(project); List<GrammarTypeDecl> typeDecls = GrammarUtil.findTypeDecl(project);
List<LookupElement> variants = new ArrayList<>(); List<LookupElement> variants = new ArrayList<>();
for (final GrammarTypeDecl typeDecl : typeDecls) { for (final GrammarTypeDecl typeDecl : typeDecls) {
if (typeDecl.getName() != null && typeDecl.getName().length() > 0) { if (typeDecl.getName() != null && !typeDecl.getName().isEmpty()) {
variants.add(LookupElementBuilder variants.add(LookupElementBuilder
.create(typeDecl).withIcon(JastAddIcons.FILE) .create(typeDecl).withIcon(JastAddIcons.FILE)
.withPresentableText(typeDecl.getName()) .withPresentableText(typeDecl.getName())
......
...@@ -4,21 +4,21 @@ import com.intellij.openapi.util.TextRange; ...@@ -4,21 +4,21 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.PlatformPatterns; 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.grammar.psi.GrammarTypeReference; import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class GrammarReferenceContributor extends PsiReferenceContributor { public class GrammarReferenceContributor extends PsiReferenceContributor {
@Override @Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(PlatformPatterns.psiElement(GrammarTypeReference.class), registrar.registerReferenceProvider(PlatformPatterns.psiElement(GrammarTypeName.class),
new PsiReferenceProvider() { new PsiReferenceProvider() {
@NotNull @NotNull
@Override @Override
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element,
@NotNull ProcessingContext context) { @NotNull ProcessingContext context) {
GrammarTypeReference typeReference = (GrammarTypeReference) element; GrammarTypeName typeReference = (GrammarTypeName) element;
String value = typeReference.getText(); String value = typeReference.getText();
if (value != null) { if (value != null) {
TextRange range = new TextRange(0, value.length()); TextRange range = new TextRange(0, value.length());
......
...@@ -38,8 +38,8 @@ public class GrammarRemoveRedundantComponentNameFix extends BaseIntentionAction ...@@ -38,8 +38,8 @@ public class GrammarRemoveRedundantComponentNameFix extends BaseIntentionAction
@Override @Override
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws
IncorrectOperationException { IncorrectOperationException {
PsiElement first = component.getDeclaredName(); PsiElement first = component.getComponentName();
PsiElement last = component.getDeclaredName(); PsiElement last = component.getComponentName();
while (last != null && last.getNode().getElementType() != GrammarTypes.COL) { while (last != null && last.getNode().getElementType() != GrammarTypes.COL) {
last = last.getNextSibling(); last = last.getNextSibling();
} }
......
...@@ -2,6 +2,7 @@ package org.jastadd.tooling.grammar; ...@@ -2,6 +2,7 @@ package org.jastadd.tooling.grammar;
import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.fileTypes.LanguageFileType;
import org.jastadd.tooling.util.JastAddIcons; import org.jastadd.tooling.util.JastAddIcons;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
...@@ -21,6 +22,11 @@ public class RelAstFileType extends LanguageFileType { ...@@ -21,6 +22,11 @@ public class RelAstFileType extends LanguageFileType {
return "RelAst Grammar"; return "RelAst Grammar";
} }
@Override
public @Nls @NotNull String getDisplayName() {
return getName();
}
@NotNull @NotNull
@Override @Override
public String getDescription() { public String getDescription() {
......
...@@ -13,14 +13,14 @@ public class GrammarElementFactory { ...@@ -13,14 +13,14 @@ public class GrammarElementFactory {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static GrammarDeclaredName createDeclaredName(Project project, String name) { public static GrammarTypeName createTypeName(Project project, String name) {
final GrammarFile file = createFile(project, name + ";"); final GrammarFile file = createFile(project, name + ";");
return (GrammarDeclaredName) file.getFirstChild().getFirstChild(); return (GrammarTypeName) (Objects.requireNonNull(file.getFirstChild().getNode().findChildByType(GrammarTypes.TYPE_NAME)).getPsi());
} }
public static GrammarTypeReference createTypeReference(Project project, String name) { public static GrammarComponentName createComponentName(Project project, String name) {
final GrammarFile file = createFile(project, "X : " + name + ";"); final GrammarFile file = createFile(project, "X ::= " + name + ":X ;");
return (GrammarTypeReference) (Objects.requireNonNull(file.getFirstChild().getNode().findChildByType(GrammarTypes.TYPE_REFERENCE)).getPsi()); return (GrammarComponentName) file.getFirstChild().getFirstChild();
} }
public static GrammarFile createFile(Project project, String text) { public static GrammarFile createFile(Project project, String text) {
......
package org.jastadd.tooling.grammar.psi;
public interface GrammarTypeDeclExtension extends GrammarNamedElement {
}
...@@ -7,7 +7,7 @@ import com.intellij.util.IncorrectOperationException; ...@@ -7,7 +7,7 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class GrammarTypeReferenceManipulator extends AbstractElementManipulator<GrammarTypeReference> implements ElementManipulator<GrammarTypeReference> { public class GrammarTypeNameManipulator extends AbstractElementManipulator<GrammarTypeName> implements ElementManipulator<GrammarTypeName> {
/** /**
* Changes the element's text to the given new text. * Changes the element's text to the given new text.
* *
...@@ -19,9 +19,9 @@ public class GrammarTypeReferenceManipulator extends AbstractElementManipulator< ...@@ -19,9 +19,9 @@ public class GrammarTypeReferenceManipulator extends AbstractElementManipulator<
*/ */
@Nullable @Nullable
@Override @Override
public GrammarTypeReference handleContentChange(@NotNull GrammarTypeReference element, @NotNull TextRange range, String newContent) { public GrammarTypeName handleContentChange(@NotNull GrammarTypeName element, @NotNull TextRange range, String newContent) {
try { try {
return (GrammarTypeReference) element.setName(range.replace(element.getText(), newContent)); return (GrammarTypeName) element.setName(range.replace(element.getText(), newContent));
} catch (Exception e) { // e.g., in case the range is wrong } catch (Exception e) { // e.g., in case the range is wrong
throw new IncorrectOperationException(e); throw new IncorrectOperationException(e);
} }
......
package org.jastadd.tooling.grammar.psi;
public interface GrammarTypeReferenceExtension extends GrammarNamedElement {
}
...@@ -2,14 +2,14 @@ package org.jastadd.tooling.grammar.psi.impl; ...@@ -2,14 +2,14 @@ package org.jastadd.tooling.grammar.psi.impl;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.psi.GrammarComponentName;
import org.jastadd.tooling.grammar.psi.GrammarElementFactory; import org.jastadd.tooling.grammar.psi.GrammarElementFactory;
import org.jastadd.tooling.grammar.psi.GrammarTypeReference; import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
import org.jastadd.tooling.grammar.psi.GrammarTypeReferenceExtension;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class GrammarTypeReferenceImplExtension extends GrammarNamedElementImpl implements GrammarTypeReferenceExtension { public class GrammarComponentNameImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
public GrammarTypeReferenceImplExtension(@NotNull ASTNode node) { public GrammarComponentNameImplExtension(@NotNull ASTNode node) {
super(node); super(node);
} }
...@@ -22,7 +22,7 @@ public class GrammarTypeReferenceImplExtension extends GrammarNamedElementImpl i ...@@ -22,7 +22,7 @@ public class GrammarTypeReferenceImplExtension extends GrammarNamedElementImpl i
// 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) {
GrammarTypeReference name = GrammarElementFactory.createTypeReference(getProject(), newName); GrammarComponentName name = GrammarElementFactory.createComponentName(getProject(), newName);
ASTNode newKeyNode = name.getNode().getFirstChildNode(); ASTNode newKeyNode = name.getNode().getFirstChildNode();
getNode().replaceChild(keyNode, newKeyNode); getNode().replaceChild(keyNode, newKeyNode);
} }
......
...@@ -3,12 +3,12 @@ package org.jastadd.tooling.grammar.psi.impl; ...@@ -3,12 +3,12 @@ package org.jastadd.tooling.grammar.psi.impl;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.parser.GrammarTypes; import org.jastadd.tooling.grammar.parser.GrammarTypes;
import org.jastadd.tooling.grammar.psi.GrammarDeclaredName;
import org.jastadd.tooling.grammar.psi.GrammarElementFactory; import org.jastadd.tooling.grammar.psi.GrammarElementFactory;
import org.jastadd.tooling.grammar.psi.GrammarTypeDeclExtension; import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implements GrammarTypeDeclExtension { public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
public GrammarTypeDeclImplExtension(@NotNull ASTNode node) { public GrammarTypeDeclImplExtension(@NotNull ASTNode node) {
super(node); super(node);
...@@ -16,7 +16,7 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem ...@@ -16,7 +16,7 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem
public String getName() { public String getName() {
// this finds the *first* ID, which is what we want // this finds the *first* ID, which is what we want
ASTNode keyNode = getNode().findChildByType(GrammarTypes.DECLARED_NAME); ASTNode keyNode = getNode().findChildByType(GrammarTypes.TYPE_NAME);
if (keyNode != null) { if (keyNode != null) {
return keyNode.getText(); return keyNode.getText();
} else { } else {
...@@ -25,9 +25,9 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem ...@@ -25,9 +25,9 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem
} }
public PsiElement setName(@NotNull String newName) { public PsiElement setName(@NotNull String newName) {
ASTNode keyNode = getNode().findChildByType(GrammarTypes.DECLARED_NAME); ASTNode keyNode = getNode().findChildByType(GrammarTypes.TYPE_NAME);
if (keyNode != null) { if (keyNode != null) {
GrammarDeclaredName name = GrammarElementFactory.createDeclaredName(getProject(), newName); GrammarTypeName name = GrammarElementFactory.createTypeName(getProject(), newName);
ASTNode newKeyNode = name.getNode(); ASTNode newKeyNode = name.getNode();
getNode().replaceChild(keyNode, newKeyNode); getNode().replaceChild(keyNode, newKeyNode);
} }
...@@ -35,7 +35,7 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem ...@@ -35,7 +35,7 @@ public class GrammarTypeDeclImplExtension extends GrammarNamedElementImpl implem
} }
public PsiElement getNameIdentifier() { public PsiElement getNameIdentifier() {
ASTNode keyNode = getNode().findChildByType(GrammarTypes.DECLARED_NAME); ASTNode keyNode = getNode().findChildByType(GrammarTypes.TYPE_NAME);
if (keyNode != null) { if (keyNode != null) {
return keyNode.getPsi(); return keyNode.getPsi();
} else { } else {
......
package org.jastadd.tooling.grammar.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.psi.GrammarElementFactory;
import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
import org.jastadd.tooling.grammar.psi.GrammarTypeName;
import org.jetbrains.annotations.NotNull;
public class GrammarTypeNameImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
public GrammarTypeNameImplExtension(@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) {
GrammarTypeName name = GrammarElementFactory.createTypeName(getProject(), newName);
ASTNode newKeyNode = name.getNode().getFirstChildNode();
getNode().replaceChild(keyNode, newKeyNode);
}
return this;
}
public PsiElement getNameIdentifier() {
return getNode().getPsi();
}
}
...@@ -17,12 +17,12 @@ import java.util.Map; ...@@ -17,12 +17,12 @@ import java.util.Map;
public class JavaColorSettingsPage implements ColorSettingsPage { public class JavaColorSettingsPage implements ColorSettingsPage {
private static final AttributesDescriptor[] DESCRIPTORS = new AttributesDescriptor[]{ private static final AttributesDescriptor[] DESCRIPTORS = new AttributesDescriptor[]{
new AttributesDescriptor("Nonterminal Use", JavaSyntaxHighlighter.NT_USE), new AttributesDescriptor("Nonterminal use", JavaSyntaxHighlighter.NT_USE),
new AttributesDescriptor("High-Level API Use", JavaSyntaxHighlighter.HIGHLEVEL_API_USE), new AttributesDescriptor("High-Level API use", JavaSyntaxHighlighter.HIGHLEVEL_API_USE),
new AttributesDescriptor("Low-Level API Use", JavaSyntaxHighlighter.LOWLEVEL_API_USE), new AttributesDescriptor("Low-Level API use", JavaSyntaxHighlighter.LOWLEVEL_API_USE),
new AttributesDescriptor("Internal API Use", JavaSyntaxHighlighter.INTERNAL_API_USE), new AttributesDescriptor("Internal API use", JavaSyntaxHighlighter.INTERNAL_API_USE),
new AttributesDescriptor("Attribute Call", JavaSyntaxHighlighter.ATTRIBUTE_CALL), new AttributesDescriptor("Attribute call", JavaSyntaxHighlighter.ATTRIBUTE_CALL),
new AttributesDescriptor("Inter-Type Declaration Use", JavaSyntaxHighlighter.INTERTYPE_DECL_USE) new AttributesDescriptor("Inter-Type declaration use", JavaSyntaxHighlighter.INTERTYPE_DECL_USE)
}; };
@Nullable @Nullable
...@@ -63,13 +63,13 @@ public class JavaColorSettingsPage implements ColorSettingsPage { ...@@ -63,13 +63,13 @@ public class JavaColorSettingsPage implements ColorSettingsPage {
@NotNull @NotNull
@Override @Override
public AttributesDescriptor[] getAttributeDescriptors() { public AttributesDescriptor @NotNull [] getAttributeDescriptors() {
return DESCRIPTORS; return DESCRIPTORS;
} }
@NotNull @NotNull
@Override @Override
public ColorDescriptor[] getColorDescriptors() { public ColorDescriptor @NotNull [] getColorDescriptors() {
return ColorDescriptor.EMPTY_ARRAY; return ColorDescriptor.EMPTY_ARRAY;
} }
......
...@@ -38,8 +38,8 @@ ...@@ -38,8 +38,8 @@
<lang.refactoringSupport language="JastAddGrammar" <lang.refactoringSupport language="JastAddGrammar"
implementationClass="org.jastadd.tooling.grammar.GrammarRefactoringSupportProvider"/> implementationClass="org.jastadd.tooling.grammar.GrammarRefactoringSupportProvider"/>
<lang.elementManipulator forClass="org.jastadd.tooling.grammar.psi.GrammarTypeReference" <lang.elementManipulator forClass="org.jastadd.tooling.grammar.psi.GrammarTypeName"
implementationClass="org.jastadd.tooling.grammar.psi.GrammarTypeReferenceManipulator"/> implementationClass="org.jastadd.tooling.grammar.psi.GrammarTypeNameManipulator"/>
<lang.findUsagesProvider language="JastAddGrammar" <lang.findUsagesProvider language="JastAddGrammar"
...@@ -79,6 +79,14 @@ ...@@ -79,6 +79,14 @@
<annotator language="JAVA" implementationClass="org.jastadd.tooling.java.JavaMethodHighlighter"/> <annotator language="JAVA" implementationClass="org.jastadd.tooling.java.JavaMethodHighlighter"/>
<colorSettingsPage implementation="org.jastadd.tooling.java.JavaColorSettingsPage"/> <colorSettingsPage implementation="org.jastadd.tooling.java.JavaColorSettingsPage"/>
<psi.referenceContributor implementation="org.jastadd.tooling.aspect.AspectReferenceContributor"/>
<lang.elementManipulator forClass="org.jastadd.tooling.aspect.psi.JastAddAspectAstTypeName"
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