use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class MatchStatementScopeResolver 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 BLangMatch)) {
// In the ideal case, this will not get triggered
return false;
}
BLangMatch matchNode = (BLangMatch) treeVisitor.getBlockOwnerStack().peek();
DiagnosticPos matchNodePos = CommonUtil.toZeroBasedPosition(matchNode.getPosition());
DiagnosticPos nodePos = CommonUtil.toZeroBasedPosition((DiagnosticPos) node.getPosition());
List<BLangMatch.BLangMatchStmtPatternClause> patternClauseList = matchNode.getPatternClauses();
int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
int nodeLine = nodePos.getStartLine();
int nodeCol = nodePos.getStartColumn();
boolean isBeforeNode = false;
if ((line < nodeLine) || (line == nodeLine && col < nodeCol)) {
isBeforeNode = true;
} else if (patternClauseList.indexOf(node) == patternClauseList.size() - 1) {
isBeforeNode = (line < matchNodePos.getEndLine()) || (line == matchNodePos.getEndLine() && col < matchNodePos.getEndColumn());
}
if (isBeforeNode) {
Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
SymbolEnv matchEnv = createMatchEnv(matchNode, treeVisitor.getSymbolEnv());
treeVisitor.populateSymbols(visibleSymbolEntries, matchEnv);
treeVisitor.setTerminateVisitor(true);
}
return isBeforeNode;
}
use of org.wso2.charon3.core.utils.codeutils.Node 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.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for enums.
* @param enumNode ballerina enum node.
* @return documentation for enum.
*/
public static EnumDoc createDocForNode(EnumNode enumNode) {
String enumName = enumNode.getName().getValue();
List<Variable> enumerators = new ArrayList<>();
// Iterate through the enumerators
if (enumNode.getEnumerators().size() > 0) {
for (EnumNode.Enumerator enumerator : enumNode.getEnumerators()) {
String desc = fieldAnnotation((BLangNode) enumNode, (BLangNode) enumerator);
Variable variable = new Variable(enumerator.getName().getValue(), "", desc);
enumerators.add(variable);
}
}
return new EnumDoc(enumName, description((BLangNode) enumNode), new ArrayList<>(), enumerators);
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for connectors.
* @param connectorNode ballerina connector node.
* @return documentation for connectors.
*/
public static ConnectorDoc createDocForNode(BLangConnector connectorNode) {
String connectorName = connectorNode.getName().value;
List<Variable> parameters = new ArrayList<>();
List<Documentable> actions = new ArrayList<>();
// Iterate through the connector parameters
if (connectorNode.getParameters().size() > 0) {
for (BLangVariable param : connectorNode.getParameters()) {
String dataType = type(param);
String desc = paramAnnotation(connectorNode, param);
Variable variable = new Variable(param.getName().value, dataType, desc);
parameters.add(variable);
}
}
// Iterate through the actions of the connectors
if (connectorNode.getActions().size() > 0) {
for (BLangAction action : connectorNode.getActions()) {
actions.add(createDocForNode(action));
}
}
return new ConnectorDoc(connectorName, description(connectorNode), actions, parameters);
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method fieldAnnotation.
/**
* Get description annotation of the field.
* @param node parent node.
* @param param field.
* @return description of the field.
*/
private static String fieldAnnotation(BLangNode node, BLangNode param) {
String subName = "";
if (param instanceof BLangVariable) {
BLangVariable paramVariable = (BLangVariable) param;
subName = (paramVariable.getName() == null) ? paramVariable.type.tsymbol.name.value : paramVariable.getName().getValue();
} else if (param instanceof BLangEnum.Enumerator) {
BLangEnum.Enumerator paramEnumVal = (BLangEnum.Enumerator) param;
subName = paramEnumVal.getName().getValue();
}
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String attribVal = bLangLiteral.toString();
if (annotation.getAnnotationName().getValue().equals("Field") && attribVal.startsWith(subName + ":")) {
return attribVal.split(subName + ":")[1].trim();
}
}
// annotation's value
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
if (annotation.getAnnotationName().getValue().equals("Field")) {
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
return bLangLiteral.toString();
}
}
return "";
}
Aggregations