use of org.wso2.ballerinalang.compiler.tree.BLangObject in project ballerina by ballerina-lang.
the class ObjectTypeScopeResolver method isCursorBeforeNode.
/**
* Check whether the cursor is positioned before the given node start.
*
* @param nodePosition Position of the node
* @param node Node
* @param treeVisitor {@link TreeVisitor} current tree visitor instance
* @param completionContext Completion operation context
* @return {@link Boolean} Whether the cursor is before the node start or not
*/
@Override
public boolean isCursorBeforeNode(DiagnosticPos nodePosition, Node node, TreeVisitor treeVisitor, TextDocumentServiceContext completionContext) {
if (!(treeVisitor.getBlockOwnerStack().peek() instanceof BLangObject)) {
return false;
}
BLangObject ownerObject = (BLangObject) treeVisitor.getBlockOwnerStack().peek();
DiagnosticPos zeroBasedPos = CommonUtil.toZeroBasedPosition(nodePosition);
DiagnosticPos blockOwnerPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) treeVisitor.getBlockOwnerStack().peek().getPosition());
int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
boolean isLastField = false;
if ((!ownerObject.fields.isEmpty() && node instanceof BLangVariable && ownerObject.fields.indexOf(node) == ownerObject.fields.size() - 1 && ownerObject.functions.isEmpty()) || (!ownerObject.functions.isEmpty() && node instanceof BLangFunction && ownerObject.functions.indexOf(node) == ownerObject.functions.size() - 1)) {
isLastField = true;
}
if ((line < zeroBasedPos.getStartLine() || (line == zeroBasedPos.getStartLine() && col < zeroBasedPos.getStartColumn())) || (isLastField && ((blockOwnerPos.getEndLine() > line && zeroBasedPos.getEndLine() < line) || (blockOwnerPos.getEndLine() == line && blockOwnerPos.getEndColumn() > col)))) {
Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
treeVisitor.populateSymbols(visibleSymbolEntries, null);
treeVisitor.setTerminateVisitor(true);
return true;
}
return false;
}
use of org.wso2.ballerinalang.compiler.tree.BLangObject in project ballerina by ballerina-lang.
the class SymbolEnter method defineObjectMembers.
private void defineObjectMembers(List<? extends BLangObject> objects, SymbolEnv pkgEnv) {
objects.forEach(obj -> {
SymbolEnv objEnv = SymbolEnv.createObjectEnv(obj, obj.symbol.scope, pkgEnv);
defineObjectInitFunction(obj, objEnv);
obj.functions.forEach(f -> defineNode(f, objEnv));
});
}
use of org.wso2.ballerinalang.compiler.tree.BLangObject in project ballerina by ballerina-lang.
the class SymbolEnter method defineObjectFields.
private void defineObjectFields(List<? extends BLangObject> objectNodes, SymbolEnv pkgEnv) {
objectNodes.forEach(object -> {
// Create object type
SymbolEnv objectEnv = SymbolEnv.createObjectEnv(object, object.symbol.scope, pkgEnv);
BStructType objectType = (BStructType) object.symbol.type;
objectType.fields = object.fields.stream().peek(field -> defineNode(field, objectEnv)).map(field -> new BStructField(names.fromIdNode(field.name), field.symbol)).collect(Collectors.toList());
});
}
use of org.wso2.ballerinalang.compiler.tree.BLangObject in project ballerina by ballerina-lang.
the class SymbolEnter method defineObjectInitFunction.
private void defineObjectInitFunction(BLangObject object, SymbolEnv conEnv) {
BLangFunction initFunction = object.initFunction;
if (initFunction == null) {
initFunction = createInitFunction(object.pos, "", Names.OBJECT_INIT_SUFFIX);
}
initFunction.attachedFunction = true;
// Set cached receiver to the init function
initFunction.receiver = object.receiver;
initFunction.flagSet.add(Flag.ATTACHED);
// Add object level variables to the init function
BLangFunction finalInitFunction = initFunction;
object.fields.stream().filter(f -> f.expr != null).forEachOrdered(v -> finalInitFunction.initFunctionStmts.put(v.symbol, (BLangStatement) createAssignmentStmt(v)));
object.initFunction = initFunction;
defineNode(object.initFunction, conEnv);
}
use of org.wso2.ballerinalang.compiler.tree.BLangObject in project ballerina by ballerina-lang.
the class BLangPackageBuilder method endTypeDefinition.
public void endTypeDefinition(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean publicStruct) {
// TODO only adding object type for now
if (!this.objectStack.isEmpty()) {
BLangObject objectNode = (BLangObject) this.objectStack.pop();
objectNode.pos = pos;
objectNode.setName(this.createIdentifier(identifier));
if (publicStruct) {
objectNode.flagSet.add(Flag.PUBLIC);
}
objectNode.isAnonymous = false;
// Create an user defined type with object type
TypeNode objectType = createUserDefinedType(pos, ws, (BLangIdentifier) TreeBuilder.createIdentifierNode(), objectNode.name);
// Create and add receiver to attached functions
BLangVariable receiver = (BLangVariable) TreeBuilder.createVariableNode();
receiver.pos = pos;
IdentifierNode name = createIdentifier(Names.SELF.getValue());
receiver.setName(name);
receiver.addWS(ws);
receiver.docTag = DocTag.RECEIVER;
receiver.setTypeNode(objectType);
// Cache receiver to add to init function in symbolEnter
objectNode.receiver = receiver;
objectNode.functions.forEach(f -> f.setReceiver(receiver));
this.compUnit.addTopLevelNode(objectNode);
}
}
Aggregations