Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import consulo.annotation.component.ExtensionImpl;
import consulo.language.psi.PsiElement;
import consulo.application.util.matcher.NameUtil;
import consulo.localize.LocalizeValue;
import consulo.usage.UsageViewNodeTextLocation;
import consulo.usage.UsageViewShortNameLocation;
import consulo.usage.UsageViewTypeLocation;
Expand All @@ -35,36 +36,40 @@

/**
* @author gregory
* Date: 2011-07-17
* @since 2011-07-17
*/
@ExtensionImpl
public class BnfDescriptionProvider implements ElementDescriptionProvider {
@Override
@RequiredReadAction
public String getElementDescription(@Nonnull PsiElement psiElement, @Nonnull ElementDescriptionLocation location) {
public LocalizeValue getElementDescription(@Nonnull PsiElement psiElement, @Nonnull ElementDescriptionLocation location) {
if (location == UsageViewNodeTextLocation.INSTANCE && psiElement instanceof BnfComposite) {
return getElementDescription(psiElement, UsageViewTypeLocation.INSTANCE) + " " +
"'" + getElementDescription(psiElement, UsageViewShortNameLocation.INSTANCE) + "'";
return LocalizeValue.of(
getElementDescription(psiElement, UsageViewTypeLocation.INSTANCE).get() + " " +
"'" + getElementDescription(psiElement, UsageViewShortNameLocation.INSTANCE).get() + "'"
);
Comment on lines +47 to +50

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recursive getElementDescription(...) calls now return LocalizeValue, so this concatenation goes through LocalizeValue.toString() without saying so. If toString() ever gives something other than the resolved text, the usage-view node text breaks without any error. I'd call .get() explicitly so the intent is clear and the result doesn't depend on toString():

Suggested change
return LocalizeValue.of(
getElementDescription(psiElement, UsageViewTypeLocation.INSTANCE) + " " +
"'" + getElementDescription(psiElement, UsageViewShortNameLocation.INSTANCE) + "'"
);
return LocalizeValue.of(
getElementDescription(psiElement, UsageViewTypeLocation.INSTANCE).get() + " " +
"'" + getElementDescription(psiElement, UsageViewShortNameLocation.INSTANCE).get() + "'"
);

}
if (psiElement instanceof BnfRule rule) {
if (location == UsageViewTypeLocation.INSTANCE) {
return "Grammar Rule";
return LocalizeValue.localizeTODO("Grammar Rule");
}
return rule.getName();
return LocalizeValue.of(rule.getName());
}
else if (psiElement instanceof BnfAttr attr) {
if (location == UsageViewTypeLocation.INSTANCE) {
BnfRule rule = PsiTreeUtil.getParentOfType(psiElement, BnfRule.class);
return (rule == null ? "Grammar " : "Rule ") + "Attribute";
return rule == null ? LocalizeValue.localizeTODO("Grammar Attribute") : LocalizeValue.localizeTODO("Rule Attribute");
}
return attr.getName();
return LocalizeValue.of(attr.getName());
}
else if (psiElement instanceof BnfComposite) {
if (location == UsageViewTypeLocation.INSTANCE) {
return StringUtil.join(NameUtil.nameToWords(psiElement.getNode().getElementType().toString()), " ");
return LocalizeValue.of(StringUtil.join(NameUtil.nameToWords(psiElement.getNode().getElementType().toString()), " "));
}
return psiElement instanceof PsiNamedElement namedElement ? namedElement.getName() : psiElement.getClass().getSimpleName();
return psiElement instanceof PsiNamedElement namedElement
? LocalizeValue.ofNullable(namedElement.getName())
: LocalizeValue.of(psiElement.getClass().getSimpleName());
}
return null;
return LocalizeValue.empty();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This provider is a global @ExtensionImpl, so it gets asked about every PSI element, including Java, XML and the rest, not just BNF. Before this change, null meant "not mine, ask the next provider". Please check that the migrated ElementDescriptionUtil.getElementDescription treats LocalizeValue.empty() the same way, for example with isEmpty() or == LocalizeValue.empty(), and not with a != null check. If it uses != null, this line would cut off the other providers and leave usage-view and find-usages text blank for non-BNF elements. If the new ElementDescriptionProvider contract says to return empty(), this is fine as written. I could not check the platform source from this runner.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ public boolean canFindUsagesFor(@Nonnull PsiElement psiElement) {
@Nonnull
@Override
public String getType(@Nonnull PsiElement element) {
return ElementDescriptionUtil.getElementDescription(element, UsageViewTypeLocation.INSTANCE);
return ElementDescriptionUtil.getElementDescription(element, UsageViewTypeLocation.INSTANCE).get();
}

@Nonnull
@Override
public String getDescriptiveName(@Nonnull PsiElement element) {
return ElementDescriptionUtil.getElementDescription(element, UsageViewLongNameLocation.INSTANCE);
return ElementDescriptionUtil.getElementDescription(element, UsageViewLongNameLocation.INSTANCE).get();
}

@Nonnull
@Override
public String getNodeText(@Nonnull PsiElement element, boolean useFullName) {
return ElementDescriptionUtil.getElementDescription(element, UsageViewNodeTextLocation.INSTANCE);
return ElementDescriptionUtil.getElementDescription(element, UsageViewNodeTextLocation.INSTANCE).get();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import consulo.language.icon.IconDescriptorUpdaters;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;
import consulo.localize.LocalizeValue;
import consulo.platform.base.icon.PlatformIconGroup;
import consulo.ui.image.Image;
import consulo.util.collection.ContainerUtil;
Expand Down Expand Up @@ -86,7 +87,7 @@ public boolean isAlwaysShowsPlus(StructureViewTreeElement element) {

@Override
public boolean isAlwaysLeaf(StructureViewTreeElement element) {
final Object value = element.getValue();
Object value = element.getValue();
return value instanceof BnfRule || value instanceof BnfAttr;
}

Expand All @@ -102,25 +103,25 @@ protected boolean isSuitable(PsiElement element) {
}

public static class MyElement extends PsiTreeElementBase<PsiElement> implements SortableTreeElement {

public MyElement(PsiElement element) {
super(element);
}

@Override
@RequiredReadAction
public String getAlphaSortKey() {
return getPresentableText();
return getPresentableText().get();
}

@Nonnull
@Override
@RequiredReadAction
public Collection<StructureViewTreeElement> getChildrenBase() {
PsiElement element = getElement();
if (element instanceof BnfRule || element instanceof BnfAttr) {
return Collections.emptyList();
}
final ArrayList<StructureViewTreeElement> result = new ArrayList<>();
List<StructureViewTreeElement> result = new ArrayList<>();
if (element instanceof BnfFile bnfFile) {
for (BnfAttrs o : bnfFile.getAttributes()) {
result.add(new MyElement(o));
Expand All @@ -139,33 +140,33 @@ else if (element instanceof BnfAttrs bnfAttrs) {

@Override
@RequiredReadAction
public String getPresentableText() {
public LocalizeValue getPresentableText() {
PsiElement element = getElement();
if (element instanceof BnfRule rule) {
return rule.getName();
return LocalizeValue.of(rule.getName());
}
else if (element instanceof BnfAttr attr) {
return getAttrDisplayName(attr);
return LocalizeValue.of(getAttrDisplayName(attr));
}
else if (element instanceof BnfAttrs attrs) {
List<BnfAttr> attrList = attrs.getAttrList();
final BnfAttr firstAttr = ContainerUtil.getFirstItem(attrList);
BnfAttr firstAttr = ContainerUtil.getFirstItem(attrList);
if (firstAttr == null) {
return "Attributes { <empty> }";
return LocalizeValue.localizeTODO("Attributes { <empty> }");
}
String suffix = attrList.size() > 1 ? " & " + attrList.size() + " more..." : " ";
return "Attributes { " + getAttrDisplayName(firstAttr) + suffix + "}";
return LocalizeValue.localizeTODO("Attributes { " + getAttrDisplayName(firstAttr) + suffix + "}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: most of this string is user data (the attribute name and value), and the rest is formatting. localizeTODO marks text that should eventually become a localize key, but a concatenated string can't be turned into one. Use LocalizeValue.of(...), as you did for the other data-driven presentations in this method, or a parameterized localize template later.

Suggested change
return LocalizeValue.localizeTODO("Attributes { " + getAttrDisplayName(firstAttr) + suffix + "}");
return LocalizeValue.of("Attributes { " + getAttrDisplayName(firstAttr) + suffix + "}");

}
else if (element instanceof BnfFileImpl file) {
return file.getName();
return LocalizeValue.of(file.getName());
}
return "" + element;
return LocalizeValue.of(String.valueOf(element));
}

@RequiredReadAction
private static String getAttrDisplayName(BnfAttr attr) {
final BnfAttrPattern attrPattern = attr.getAttrPattern();
final BnfExpression attrValue = attr.getExpression();
BnfAttrPattern attrPattern = attr.getAttrPattern();
BnfExpression attrValue = attr.getExpression();
String attrValueText = attrValue == null ? "" : attrValue instanceof BnfValueList ? "[ ... ]" : attrValue.getText();
return attr.getName() + (attrPattern == null ? "" : attrPattern.getText()) + " = " + attrValueText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import consulo.language.psi.PsiErrorElement;
import consulo.language.psi.PsiFile;
import consulo.language.psi.PsiWhiteSpace;
import consulo.localize.LocalizeValue;
import consulo.ui.ex.ColoredItemPresentation;
import consulo.ui.image.Image;
import consulo.util.lang.StringUtil;
Expand All @@ -44,7 +45,8 @@
import org.intellij.grammar.psi.BnfRule;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.jspecify.annotations.Nullable;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only org.jspecify import in the repo. grammar-kit/pom.xml and module-info.java don't declare it, so it only resolves transitively (presumably through consulo.annotation). If that's intentional, fine. Just be aware that this file now mixes jakarta.annotation.Nonnull with jspecify @Nullable. You could keep jakarta here for consistency with the rest of the module, or migrate the module to @NullMarked in a separate change.


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -132,42 +134,41 @@ public Collection<StructureViewTreeElement> getChildrenBase() {
@Override
@RequiredReadAction
public String getAlphaSortKey() {
return getPresentableText();
return getPresentableText().get();
}

@Nonnull
@Override
@RequiredReadAction
public String getPresentableText() {
public LocalizeValue getPresentableText() {
PsiElement element = getElement();
ASTNode node = element != null ? element.getNode() : null;
IElementType elementType = node != null ? node.getElementType() : null;
if (element instanceof LeafPsiElement) {
return elementType + ": '" + element.getText() + "'";
return LocalizeValue.of(elementType + ": '" + element.getText() + "'");
}
else if (element instanceof PsiErrorElement errorElem) {
return "PsiErrorElement: '" + errorElem.getErrorDescription() + "'";
return LocalizeValue.of("PsiErrorElement: '" + errorElem.getErrorDescription() + "'");
}
else if (elementType instanceof LivePreviewElementType.RuleType ruleType) {
BnfRule rule = ruleType.getRule(element.getProject());
if (rule != null) {
BnfFile file = (BnfFile)rule.getContainingFile();
String className = getRulePsiClassName(rule, getPsiClassFormat(file));
return className + ": '" + StringUtil.first(element.getText(), 30, true) + "'";
return LocalizeValue.of(className + ": '" + StringUtil.first(element.getText(), 30, true) + "'");
}
}
return elementType + "";
return LocalizeValue.of(String.valueOf(elementType));
}

@Nullable
@Override
public String getLocationString() {
public @Nullable String getLocationString() {
return null;
}

@Nullable
@Override
public Image getIcon() {
@RequiredReadAction
public @Nullable Image getIcon() {
PsiElement element = getElement();
if (element instanceof PsiErrorElement) {
return null; //AllIcons.General.Error;
Expand All @@ -183,9 +184,9 @@ else if (element instanceof LeafPsiElement) {
return null;
}

@Nullable
@Override
public TextAttributesKey getTextAttributesKey() {
@RequiredReadAction
public @Nullable TextAttributesKey getTextAttributesKey() {
return getElement() instanceof PsiErrorElement ? CodeInsightColors.ERRORS_ATTRIBUTES : null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
import org.intellij.grammar.psi.BnfRule;

/**
* Created by IntelliJ IDEA.
* Date: 8/11/11
* Time: 4:04 PM
*
* @author Vadim Romansky
* @since 2011-08-11
*/
public class InlineRuleDialog extends InlineOptionsDialog {
private final PsiReference myReference;
Expand All @@ -48,7 +45,7 @@ public InlineRuleDialog(Project project, BnfRule rule, PsiReference ref) {

@Override
protected LocalizeValue getNameLabelText() {
return LocalizeValue.localizeTODO(ElementDescriptionUtil.getElementDescription(myElement, UsageViewNodeTextLocation.INSTANCE));
return ElementDescriptionUtil.getElementDescription(myElement, UsageViewNodeTextLocation.INSTANCE);
}

@Override
Expand Down
Loading