Skip to content

Commit

Permalink
Merge pull request #29709 from pubudu91/fix-28969
Browse files Browse the repository at this point in the history
Introduce a separate symbol kind for enum members
  • Loading branch information
pubudu91 committed Apr 1, 2021
2 parents c6bc0a5 + 27ae9e0 commit 21d191b
Show file tree
Hide file tree
Showing 17 changed files with 707 additions and 684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
import io.ballerina.compiler.api.symbols.ConstantSymbol;
import io.ballerina.compiler.api.symbols.FunctionSymbol;
import io.ballerina.compiler.api.symbols.Qualifier;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDescKind;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
import org.wso2.ballerinalang.util.Flags;

import java.util.List;

import static io.ballerina.compiler.api.symbols.SymbolKind.CONSTANT;
import static io.ballerina.compiler.api.symbols.SymbolKind.ENUM_MEMBER;
import static org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols.isFlagOn;

/**
* Represent Constant Symbol.
*
Expand All @@ -42,7 +46,8 @@ public class BallerinaConstantSymbol extends BallerinaVariableSymbol implements
private BallerinaConstantSymbol(String name, List<Qualifier> qualifiers, List<AnnotationSymbol> annots,
TypeSymbol typeDescriptor, TypeSymbol broaderType, Object constValue,
BSymbol bSymbol, CompilerContext context) {
super(name, SymbolKind.CONSTANT, qualifiers, annots, typeDescriptor, bSymbol, context);
super(name, isFlagOn(bSymbol.flags, Flags.ENUM_MEMBER) ? ENUM_MEMBER : CONSTANT, qualifiers, annots,
typeDescriptor, bSymbol, context);
this.constValue = constValue;
this.broaderType = broaderType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum SymbolKind {
OBJECT_FIELD,
CLASS_FIELD,
ENUM,
ENUM_MEMBER,
PARAMETER,
PATH_PARAMETER
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,9 @@ public enum Flag {
/**
* Indicates flagged node allows never type.
*/
NEVER_ALLOWED;
NEVER_ALLOWED,
/**
* Indicates flagged node is a member of an enum.
*/
ENUM_MEMBER
}
Original file line number Diff line number Diff line change
Expand Up @@ -3475,6 +3475,7 @@ public BLangConstant transformEnumMember(EnumMemberNode member, Boolean publicQu
BLangConstant bLangConstant = (BLangConstant) TreeBuilder.createConstantNode();
bLangConstant.pos = getPosition(member);
bLangConstant.flagSet.add(Flag.CONSTANT);
bLangConstant.flagSet.add(Flag.ENUM_MEMBER);
if (publicQualifier) {
bLangConstant.flagSet.add(Flag.PUBLIC);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class Flags {
public static final long FIELD = REST_PARAM << 1; // 38
public static final long ANY_FUNCTION = FIELD << 1; // 39
public static final long INFER = ANY_FUNCTION << 1; // 40
public static final long ENUM_MEMBER = INFER << 1; // 41


public static long asMask(Set<Flag> flagSet) {
Expand Down Expand Up @@ -196,6 +197,9 @@ public static long asMask(Set<Flag> flagSet) {
case ANY_FUNCTION:
mask |= ANY_FUNCTION;
break;
case ENUM_MEMBER:
mask |= ENUM_MEMBER;
break;
}
}
return mask;
Expand Down Expand Up @@ -311,6 +315,9 @@ public static Set<Flag> unMask(long mask) {
case ANY_FUNCTION:
flagVal = ANY_FUNCTION;
break;
case ENUM_MEMBER:
flagVal = ENUM_MEMBER;
break;
default:
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static Optional<TypeSymbol> getTypeDescriptor(Symbol symbol) {
case METHOD:
return Optional.ofNullable(((FunctionSymbol) symbol).typeDescriptor());
case CONSTANT:
case ENUM_MEMBER:
return Optional.ofNullable(((ConstantSymbol) symbol).typeDescriptor());
case CLASS:
return Optional.of((ClassSymbol) symbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.ballerina.compiler.api.symbols.ConstantSymbol;
import io.ballerina.compiler.api.symbols.Documentation;
import io.ballerina.compiler.api.symbols.SymbolKind;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.commons.CompletionContext;
import org.eclipse.lsp4j.CompletionItem;
Expand Down Expand Up @@ -49,7 +50,12 @@ public static CompletionItem build(ConstantSymbol constantSymbol, CompletionCont
completionItem.setInsertText(constantSymbol.getName().get());
completionItem.setDetail(CommonUtil.getModifiedTypeName(context, constantSymbol.typeDescriptor()));
completionItem.setDocumentation(getDocumentation(constantSymbol));
completionItem.setKind(CompletionItemKind.Variable);

if (constantSymbol.kind() == SymbolKind.ENUM_MEMBER) {
completionItem.setKind(CompletionItemKind.EnumMember);
} else {
completionItem.setKind(CompletionItemKind.Variable);
}

return completionItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected List<LSCompletionItem> getCompletionItemList(List<? extends Symbol> sc
}
if (symbol.kind() == FUNCTION || symbol.kind() == METHOD) {
completionItems.add(populateBallerinaFunctionCompletionItem(symbol, ctx));
} else if (symbol.kind() == SymbolKind.CONSTANT) {
} else if (symbol.kind() == SymbolKind.CONSTANT || symbol.kind() == SymbolKind.ENUM_MEMBER) {
CompletionItem constantCItem = ConstantCompletionItemBuilder.build((ConstantSymbol) symbol, ctx);
completionItems.add(new SymbolCompletionItem(ctx, symbol, constantCItem));
} else if (symbol.kind() == SymbolKind.VARIABLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,

private Predicate<Symbol> constantFilter() {
return symbol -> {
if (symbol.kind() != SymbolKind.CONSTANT) {
if (symbol.kind() != SymbolKind.CONSTANT && symbol.kind() != SymbolKind.ENUM_MEMBER) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,
}
*/
QualifiedNameReferenceNode nameRef = (QualifiedNameReferenceNode) nodeAtCursor;
Predicate<Symbol> filter = symbol ->
symbol.kind() == SymbolKind.TYPE_DEFINITION
|| symbol.kind() == SymbolKind.VARIABLE
|| symbol.kind() == SymbolKind.CONSTANT
|| symbol.kind() == SymbolKind.FUNCTION
|| symbol.kind() == SymbolKind.CLASS;
Predicate<Symbol> filter = symbol -> symbol.kind() != SymbolKind.SERVICE_DECLARATION;
List<Symbol> moduleContent = QNameReferenceUtil.getModuleContent(context, nameRef, filter);

completionItems.addAll(this.getCompletionItemList(moduleContent, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,
completionItems.addAll(this.getModuleCompletionItems(context));
}
} else if (this.onExpressionContext(context, node)) {
Predicate<Symbol> predicate = symbol -> symbol.kind() == SymbolKind.CONSTANT;
Predicate<Symbol> predicate =
symbol -> symbol.kind() == SymbolKind.CONSTANT || symbol.kind() == SymbolKind.ENUM_MEMBER;
List<Symbol> constants;
if (QNameReferenceUtil.onQualifiedNameIdentifier(context, nodeAtCursor)) {
QualifiedNameReferenceNode qNameRef = (QualifiedNameReferenceNode) nodeAtCursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ protected List<LSCompletionItem> getPatternClauseCompletions(BallerinaCompletion

protected Predicate<Symbol> constantFilter() {
// also, should include the enum members as well. Since currently both are same, this is fine
return symbol -> symbol.kind() == SymbolKind.CONSTANT;
return symbol -> symbol.kind() == SymbolKind.CONSTANT || symbol.kind() == SymbolKind.ENUM_MEMBER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private static Hover getHoverForSymbol(Symbol symbol, HoverContext context) {
case CONSTANT:
case ANNOTATION:
case ENUM:
case ENUM_MEMBER:
case VARIABLE:
return getDescriptionOnlyHoverObject(symbol);
case TYPE:
Expand Down
Loading

0 comments on commit 21d191b

Please sign in to comment.