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

type name and reference oen thing again

parent 0023a469
No related branches found
No related tags found
1 merge request!1Resolve "Renaming of Nonterminal Does Not Rename All Occurences"
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
Showing
with 26 additions and 68 deletions
...@@ -19,7 +19,7 @@ GrammarFile ::= comment* ((type_decl | relation) comment*)* ...@@ -19,7 +19,7 @@ GrammarFile ::= comment* ((type_decl | relation) comment*)*
comment ::= (WHITESPACE | MULTILINECOMMENT | DOCCOMMENT | SINGLELINECOMMENT) comment ::= (WHITESPACE | MULTILINECOMMENT | DOCCOMMENT | SINGLELINECOMMENT)
type_decl ::= ABSTRACT? type_name (COL type_reference)? (ASSIGN (component | nta_component)*)? SCOL type_decl ::= ABSTRACT? type_name (COL type_name)? (ASSIGN (component | nta_component)*)? SCOL
{ {
extends="org.jastadd.tooling.grammar.psi.impl.GrammarTypeDeclImplExtension" extends="org.jastadd.tooling.grammar.psi.impl.GrammarTypeDeclImplExtension"
implements="org.jastadd.tooling.grammar.psi.GrammarNamedElement" implements="org.jastadd.tooling.grammar.psi.GrammarNamedElement"
...@@ -27,7 +27,7 @@ type_decl ::= ABSTRACT? type_name (COL type_reference)? (ASSIGN (component | nta ...@@ -27,7 +27,7 @@ type_decl ::= ABSTRACT? type_name (COL type_reference)? (ASSIGN (component | nta
nta_component ::= SLASH component SLASH nta_component ::= SLASH component SLASH
component ::= (component_name COL type_reference STAR?) | (type_reference STAR?) | (LBRACKET component_name COL type_reference RBRACKET) | (LBRACKET type_reference RBRACKET) | (LT component_name (COL (java_type_use))? GT) component ::= (component_name COL type_name STAR?) | (type_name STAR?) | (LBRACKET component_name COL type_name RBRACKET) | (LBRACKET type_name RBRACKET) | (LT component_name (COL (java_type_use))? GT)
java_type_use ::= parameterized_java_type_use | simple_java_type_use java_type_use ::= parameterized_java_type_use | simple_java_type_use
...@@ -37,16 +37,16 @@ simple_java_type_use ::= java_name (DOT java_name)* ...@@ -37,16 +37,16 @@ simple_java_type_use ::= java_name (DOT java_name)*
relation ::= REL ((unnamed_role LEFT navigable_role) | (navigable_role RIGHT unnamed_role) | (navigable_role BIDIRECTIONAL navigable_role)) SCOL relation ::= REL ((unnamed_role LEFT navigable_role) | (navigable_role RIGHT unnamed_role) | (navigable_role BIDIRECTIONAL navigable_role)) SCOL
unnamed_role ::= type_reference | navigable_role unnamed_role ::= type_name | navigable_role
navigable_role ::= type_reference DOT component_name (STAR | QUESTION_MARK)? navigable_role ::= type_name DOT component_name (STAR | QUESTION_MARK)?
// for auto-completion, it is helpful if we can distinguish the different IDs //// for auto-completion, it is helpful if we can distinguish the different IDs
type_name ::= ID //type_name ::= ID
type_reference ::= ID type_name ::= ID
{ {
extends="org.jastadd.tooling.grammar.psi.impl.GrammarTypeReferenceImplExtension" extends="org.jastadd.tooling.grammar.psi.impl.GrammarTypeNameImplExtension"
implements="org.jastadd.tooling.grammar.psi.GrammarNamedElement" implements="org.jastadd.tooling.grammar.psi.GrammarNamedElement"
} }
......
...@@ -8,7 +8,7 @@ import com.intellij.lang.annotation.HighlightSeverity; ...@@ -8,7 +8,7 @@ import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.psi.GrammarComponent; import org.jastadd.tooling.grammar.psi.GrammarComponent;
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 GrammarAnnotator implements Annotator { public class GrammarAnnotator implements Annotator {
...@@ -18,9 +18,9 @@ public class GrammarAnnotator implements Annotator { ...@@ -18,9 +18,9 @@ public class GrammarAnnotator implements Annotator {
if (element instanceof GrammarComponent) { if (element instanceof GrammarComponent) {
GrammarComponent component = (GrammarComponent) element; GrammarComponent component = (GrammarComponent) element;
if (component.getTypeReference() != null && component.getComponentName() != null) { if (component.getTypeName() != null && component.getComponentName() != null) {
String name = component.getComponentName().getText(); String name = component.getComponentName().getText();
if (name != null && !name.equals("") && name.equals(component.getTypeReference().getName())) { if (name != null && !name.equals("") && name.equals(component.getTypeName().getName())) {
holder.newAnnotation(HighlightSeverity.WEAK_WARNING, "Redundant name") holder.newAnnotation(HighlightSeverity.WEAK_WARNING, "Redundant name")
.range(component.getComponentName().getTextRange()) .range(component.getComponentName().getTextRange())
.highlightType(ProblemHighlightType.WEAK_WARNING) .highlightType(ProblemHighlightType.WEAK_WARNING)
...@@ -30,8 +30,8 @@ public class GrammarAnnotator implements Annotator { ...@@ -30,8 +30,8 @@ public class GrammarAnnotator implements Annotator {
.create(); .create();
} }
} }
} else if (element instanceof GrammarTypeReference) { } else if (element instanceof GrammarTypeName) {
GrammarTypeReference reference = (GrammarTypeReference) element; GrammarTypeName reference = (GrammarTypeName) element;
if (GrammarUtil.findTypeDecl(element.getProject(), reference.getName()).isEmpty()) { if (GrammarUtil.findTypeDecl(element.getProject(), reference.getName()).isEmpty()) {
holder.newAnnotation(HighlightSeverity.ERROR, "Undefined reference") holder.newAnnotation(HighlightSeverity.ERROR, "Undefined reference")
.range(element.getTextRange()) .range(element.getTextRange())
......
...@@ -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()));
} }
} }
......
...@@ -4,7 +4,6 @@ package org.jastadd.tooling.grammar; ...@@ -4,7 +4,6 @@ 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.GrammarTypeName; 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 GrammarTypeName) || (elementToRename instanceof GrammarTypeReference); return elementToRename instanceof GrammarTypeName;
} }
} }
...@@ -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());
......
...@@ -15,12 +15,7 @@ public class GrammarElementFactory { ...@@ -15,12 +15,7 @@ public class GrammarElementFactory {
public static GrammarTypeName createTypeName(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 (GrammarTypeName) file.getFirstChild().getFirstChild(); return (GrammarTypeName) (Objects.requireNonNull(file.getFirstChild().getNode().findChildByType(GrammarTypes.TYPE_NAME)).getPsi());
}
public static GrammarTypeReference createTypeReference(Project project, String name) {
final GrammarFile file = createFile(project, "X : " + name + ";");
return (GrammarTypeReference) (Objects.requireNonNull(file.getFirstChild().getNode().findChildByType(GrammarTypes.TYPE_REFERENCE)).getPsi());
} }
public static GrammarComponentName createComponentName(Project project, String name) { public static GrammarComponentName createComponentName(Project project, String name) {
......
...@@ -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);
} }
......
...@@ -5,7 +5,7 @@ import com.intellij.psi.PsiElement; ...@@ -5,7 +5,7 @@ import com.intellij.psi.PsiElement;
import org.jastadd.tooling.grammar.psi.GrammarComponentName; 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.GrammarNamedElement; import org.jastadd.tooling.grammar.psi.GrammarNamedElement;
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 GrammarComponentNameImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement { public class GrammarComponentNameImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
......
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.GrammarTypeReference;
import org.jetbrains.annotations.NotNull;
public class GrammarTypeReferenceImplExtension extends GrammarNamedElementImpl implements GrammarNamedElement {
public GrammarTypeReferenceImplExtension(@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) {
GrammarTypeReference name = GrammarElementFactory.createTypeReference(getProject(), newName);
ASTNode newKeyNode = name.getNode().getFirstChildNode();
getNode().replaceChild(keyNode, newKeyNode);
}
return this;
}
public PsiElement getNameIdentifier() {
return getNode().getPsi();
}
}
...@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment