use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class HoverUtil method getHoverInformation.
/**
* Get the hover information for the given hover context.
*
* @param bLangPackage resolved bLangPackage for the hover context.
* @param hoverContext context of the hover.
* @return hover content.
*/
public static Hover getHoverInformation(BLangPackage bLangPackage, TextDocumentServiceContext hoverContext) {
Hover hover;
switch(hoverContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY)) {
case ContextConstants.FUNCTION:
BLangFunction bLangFunction = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangFunction != null) {
if (bLangFunction.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangFunction.docAttachments);
} else {
hover = getAnnotationContent(bLangFunction.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.STRUCT:
BLangStruct bLangStruct = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangStruct != null) {
if (bLangStruct.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangStruct.docAttachments);
} else {
hover = getAnnotationContent(bLangStruct.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.OBJECT:
BLangObject bLangObject = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangObject != null) {
if (bLangObject.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangObject.docAttachments);
} else {
hover = getAnnotationContent(bLangObject.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ENUM:
BLangEnum bLangEnum = bLangPackage.enums.stream().filter(bEnum -> bEnum.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangEnum != null) {
if (bLangEnum.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangEnum.docAttachments);
} else {
hover = getAnnotationContent(bLangEnum.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.TRANSFORMER:
BLangTransformer bLangTransformer = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangTransformer != null) {
if (bLangTransformer.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangTransformer.docAttachments);
} else {
hover = getAnnotationContent(bLangTransformer.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.CONNECTOR:
BLangConnector bLangConnector = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangConnector != null) {
if (bLangConnector.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangConnector.docAttachments);
} else {
hover = getAnnotationContent(bLangConnector.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ACTION:
BLangAction bLangAction = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) hoverContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangAction != null) {
if (bLangAction.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangAction.docAttachments);
} else {
hover = getAnnotationContent(bLangAction.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.ENDPOINT:
BLangEndpoint bLangEndpoint = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangEndpoint != null) {
hover = getAnnotationContent(bLangEndpoint.annAttachments);
} else {
hover = getDefaultHoverObject();
}
break;
case ContextConstants.VARIABLE:
BLangVariable bLangVariable = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangVariable != null) {
if (bLangVariable.docAttachments.size() > 0) {
hover = getDocumentationContent(bLangVariable.docAttachments);
} else {
hover = getAnnotationContent(bLangVariable.annAttachments);
}
} else {
hover = getDefaultHoverObject();
}
break;
default:
hover = new Hover();
List<Either<String, MarkedString>> contents = new ArrayList<>();
contents.add(Either.forLeft(""));
hover.setContents(contents);
break;
}
return hover;
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class BallerinaParserService method generateJSON.
public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs) throws InvocationTargetException, IllegalAccessException {
if (node == null) {
return JsonNull.INSTANCE;
}
Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream().flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet());
JsonObject nodeJson = new JsonObject();
JsonArray wsJsonArray = new JsonArray();
Set<Whitespace> ws = node.getWS();
if (ws != null && !ws.isEmpty()) {
for (Whitespace whitespace : ws) {
JsonObject wsJson = new JsonObject();
wsJson.addProperty("ws", whitespace.getWs());
wsJson.addProperty("i", whitespace.getIndex());
wsJson.addProperty("text", whitespace.getPrevious());
wsJson.addProperty("static", whitespace.isStatic());
wsJsonArray.add(wsJson);
}
nodeJson.add("ws", wsJsonArray);
}
Diagnostic.DiagnosticPosition position = node.getPosition();
if (position != null) {
JsonObject positionJson = new JsonObject();
positionJson.addProperty("startColumn", position.getStartColumn());
positionJson.addProperty("startLine", position.getStartLine());
positionJson.addProperty("endColumn", position.getEndColumn());
positionJson.addProperty("endLine", position.getEndLine());
nodeJson.add("position", positionJson);
}
/* Virtual props */
JsonArray type = getType(node);
if (type != null) {
nodeJson.add(SYMBOL_TYPE, type);
}
if (node.getKind() == NodeKind.INVOCATION) {
assert node instanceof BLangInvocation : node.getClass();
BLangInvocation invocation = (BLangInvocation) node;
if (invocation.symbol != null && invocation.symbol.kind != null) {
nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString());
}
}
for (Method m : methods) {
String name = m.getName();
if (name.equals("getWS") || name.equals("getPosition")) {
continue;
}
String jsonName;
if (name.startsWith("get")) {
jsonName = toJsonName(name, 3);
} else if (name.startsWith("is")) {
jsonName = toJsonName(name, 2);
} else {
continue;
}
Object prop = m.invoke(node);
/* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */
if (node.getKind() == NodeKind.LITERAL && "value".equals(jsonName)) {
if (prop instanceof String) {
nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"');
nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop));
} else {
nodeJson.addProperty(jsonName, String.valueOf(prop));
}
continue;
}
if (node.getKind() == NodeKind.ANNOTATION && node instanceof BLangAnnotation) {
JsonArray attachmentPoints = new JsonArray();
((BLangAnnotation) node).getAttachmentPoints().stream().map(BLangAnnotationAttachmentPoint::getAttachmentPoint).map(BLangAnnotationAttachmentPoint.AttachmentPoint::getValue).map(JsonPrimitive::new).forEach(attachmentPoints::add);
nodeJson.add("attachmentPoints", attachmentPoints);
}
if (node.getKind() == NodeKind.USER_DEFINED_TYPE && jsonName.equals("typeName")) {
IdentifierNode typeNode = (IdentifierNode) prop;
Node structNode;
if (typeNode.getValue().startsWith("$anonStruct$") && (structNode = anonStructs.remove(typeNode.getValue())) != null) {
JsonObject anonStruct = generateJSON(structNode, anonStructs).getAsJsonObject();
anonStruct.addProperty("anonStruct", true);
nodeJson.add("anonStruct", anonStruct);
continue;
}
}
if (prop instanceof List && jsonName.equals("types")) {
// Currently we don't need any Symbols for the UI. So skipping for now.
continue;
}
/* Node classes */
if (prop instanceof Node) {
nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs));
} else if (prop instanceof List) {
List listProp = (List) prop;
JsonArray listPropJson = new JsonArray();
nodeJson.add(jsonName, listPropJson);
for (Object listPropItem : listProp) {
if (listPropItem instanceof Node) {
/* Remove top level anon func and struct */
if (node.getKind() == NodeKind.COMPILATION_UNIT) {
if (listPropItem instanceof BLangStruct && ((BLangStruct) listPropItem).isAnonymous) {
anonStructs.put(((BLangStruct) listPropItem).getName().getValue(), ((BLangStruct) listPropItem));
continue;
}
if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) {
continue;
}
}
listPropJson.add(generateJSON((Node) listPropItem, anonStructs));
} else {
logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem);
}
}
/* Runtime model classes */
} else if (prop instanceof Set && jsonName.equals("flags")) {
Set flags = (Set) prop;
for (Flag flag : Flag.values()) {
nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag));
}
} else if (prop instanceof Set) {
// TODO : limit this else if to getInputs getOutputs of transform.
Set vars = (Set) prop;
JsonArray listVarJson = new JsonArray();
nodeJson.add(jsonName, listVarJson);
for (Object obj : vars) {
listVarJson.add(obj.toString());
}
} else if (prop instanceof NodeKind) {
String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString());
nodeJson.addProperty(jsonName, kindName);
} else if (prop instanceof OperatorKind) {
nodeJson.addProperty(jsonName, prop.toString());
/* Generic classes */
} else if (prop instanceof String) {
nodeJson.addProperty(jsonName, (String) prop);
} else if (prop instanceof Number) {
nodeJson.addProperty(jsonName, (Number) prop);
} else if (prop instanceof Boolean) {
nodeJson.addProperty(jsonName, (Boolean) prop);
} else if (prop instanceof Enum) {
nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name()));
} else if (prop != null) {
nodeJson.addProperty(jsonName, prop.toString());
String message = "Node " + node.getClass().getSimpleName() + " contains unknown type prop: " + jsonName + " of type " + prop.getClass();
logger.error(message);
}
}
return nodeJson;
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for structs.
* @param structNode ballerina struct node.
* @return documentation for structs.
*/
public static StructDoc createDocForNode(BLangStruct structNode) {
String structName = structNode.getName().value;
// Check if its an anonymous struct
if (structName.contains(ANONYMOUS_STRUCT)) {
structName = "Anonymous Struct";
}
List<Field> fields = new ArrayList<>();
// Iterate through the struct fields
if (structNode.getFields().size() > 0) {
for (BLangVariable param : structNode.getFields()) {
String dataType = type(param);
String desc = fieldAnnotation(structNode, param);
String defaultValue = "";
if (null != param.getInitialExpression()) {
defaultValue = param.getInitialExpression().toString();
}
Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
fields.add(variable);
}
}
return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class Generator method generatePage.
/**
* Generate the page when the bal package is passed.
* @param balPackage The current package that is being viewed.
* @param packages List of available packages.
* @return A page model for the current package.
*/
public static Page generatePage(BLangPackage balPackage, List<Link> packages) {
ArrayList<Documentable> documentables = new ArrayList<>();
String currentPackageName = (balPackage.symbol).pkgID.name.value;
// Check for structs in the package
if (balPackage.getStructs().size() > 0) {
for (BLangStruct struct : balPackage.getStructs()) {
if (struct.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(struct));
}
}
}
// Check for functions in the package
if (balPackage.getFunctions().size() > 0) {
for (BLangFunction function : balPackage.getFunctions()) {
if (function.getFlags().contains(Flag.PUBLIC)) {
if (function.getReceiver() != null) {
if (documentables.size() > 0) {
for (Documentable parentDocumentable : documentables) {
TypeNode langType = function.getReceiver().getTypeNode();
String typeName = (langType instanceof BLangUserDefinedType ? ((BLangUserDefinedType) langType).typeName.value : langType.toString());
if (typeName.equals(parentDocumentable.name)) {
parentDocumentable.children.add(createDocForNode(function));
}
}
}
} else {
// If there's no receiver type i.e. no struct binding to the function
documentables.add(createDocForNode(function));
}
}
}
}
// Check for connectors in the package
for (BLangConnector connector : balPackage.getConnectors()) {
if (connector.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(connector));
}
}
// Check for connectors in the package
for (EnumNode enumNode : balPackage.getEnums()) {
if (enumNode.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(enumNode));
}
}
// Check for annotations
for (BLangAnnotation annotation : balPackage.getAnnotations()) {
if (annotation.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(annotation));
}
}
// Check for global variables
for (BLangVariable var : balPackage.getGlobalVariables()) {
if (var.getFlags().contains(Flag.PUBLIC)) {
documentables.add(createDocForNode(var));
}
}
// Create the links to select which page or package is active
List<Link> links = new ArrayList<>();
PackageName packageNameHeading = null;
for (Link pkgLink : packages) {
if (pkgLink.caption.value.equals(currentPackageName)) {
packageNameHeading = (PackageName) pkgLink.caption;
links.add(new Link(pkgLink.caption, pkgLink.href, true));
} else {
links.add(new Link(pkgLink.caption, pkgLink.href, false));
}
}
return new Page(packageNameHeading, documentables, links);
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class BallerinaStructDocGenTest method testStruct.
@Test(description = "Test a Bal file with structs")
public void testStruct() {
try {
Map<String, BLangPackage> docsMap = BallerinaDocGenerator.generatePackageDocsFromBallerina(sourceRoot, "balWithStruct.bal");
Assert.assertNotNull(docsMap);
Assert.assertEquals(docsMap.size(), 1);
BLangPackage balPackage = docsMap.get(".");
List<BLangStruct> structs = balPackage.getStructs();
Assert.assertEquals(structs.size(), 1);
BLangStruct struct = (BLangStruct) structs.iterator().next();
Assert.assertEquals(struct.getFields().size(), 3);
Assert.assertEquals(struct.getAnnotationAttachments().size(), 4);
} catch (IOException e) {
Assert.fail();
} finally {
BallerinaDocGenTestUtils.cleanUp();
}
}
Aggregations