use of org.wso2.transport.http.netty.config.Parameter in project ballerina by ballerina-lang.
the class TreeVisitor method isWithinParameterContext.
/**
* Check whether the cursor resides within the given node type's parameter context.
* Node name is used to identify the correct node
* @param nodeName Name of the node
* @param nodeType Node type (Function, Resource, Action or Connector)
* @return {@link Boolean} Whether the cursor is within the parameter context
*/
private boolean isWithinParameterContext(String nodeName, String nodeType) {
ParserRuleContext parserRuleContext = documentServiceContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
TokenStream tokenStream = documentServiceContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
String terminalToken = "";
// If the parser rule context is not parameter context or parameter list context, we skipp the calculation
if (!(parserRuleContext instanceof BallerinaParser.ParameterContext || parserRuleContext instanceof BallerinaParser.ParameterListContext)) {
return false;
}
int startTokenIndex = parserRuleContext.getStart().getTokenIndex();
ArrayList<String> terminalKeywords = new ArrayList<>(Arrays.asList(NODE_TYPE_ACTION, NODE_TYPE_CONNECTOR, NODE_TYPE_FUNCTION, NODE_TYPE_RESOURCE));
ArrayList<Token> filteredTokens = new ArrayList<>();
Token openBracket = null;
boolean isWithinParams = false;
// Find the index of the closing bracket
while (true) {
if (startTokenIndex > tokenStream.size()) {
// In the ideal case, should not reach this point
startTokenIndex = -1;
break;
}
Token token = tokenStream.get(startTokenIndex);
String tokenString = token.getText();
if (tokenString.equals(")")) {
break;
}
startTokenIndex++;
}
// Backtrack the token stream to find a terminal token
while (true) {
if (startTokenIndex < 0) {
break;
}
Token token = tokenStream.get(startTokenIndex);
String tokenString = token.getText();
if (terminalKeywords.contains(tokenString)) {
terminalToken = tokenString;
break;
}
if (token.getChannel() == Token.DEFAULT_CHANNEL) {
filteredTokens.add(token);
}
startTokenIndex--;
}
Collections.reverse(filteredTokens);
/*
This particular logic identifies a matching pair of closing and opening bracket and then check whether the
cursor is within those bracket pair
*/
if (nodeName.equals(filteredTokens.get(0).getText()) && terminalToken.equals(nodeType)) {
String tokenText;
for (Token token : filteredTokens) {
tokenText = token.getText();
if (tokenText.equals("(")) {
openBracket = token;
} else if (tokenText.equals(")") && openBracket != null) {
Position cursorPos = documentServiceContext.get(DocumentServiceKeys.POSITION_KEY).getPosition();
int openBLine = openBracket.getLine() - 1;
int openBCol = openBracket.getCharPositionInLine();
int closeBLine = token.getLine() - 1;
int closeBCol = token.getCharPositionInLine();
int cursorLine = cursorPos.getLine();
int cursorCol = cursorPos.getCharacter();
isWithinParams = (cursorLine > openBLine && cursorLine < closeBLine) || (cursorLine == openBLine && cursorCol > openBCol && cursorLine < closeBLine) || (cursorLine > openBLine && cursorCol < closeBCol && cursorLine == closeBLine) || (cursorLine == openBLine && cursorLine == closeBLine && cursorCol >= openBCol && cursorCol <= closeBCol);
if (isWithinParams) {
break;
} else {
openBracket = null;
}
}
}
}
return isWithinParams;
}
use of org.wso2.transport.http.netty.config.Parameter in project ballerina by ballerina-lang.
the class TreeVisitor method visit.
public void visit(BLangResource resourceNode) {
String resourceName = resourceNode.getName().getValue();
BSymbol resourceSymbol = resourceNode.symbol;
SymbolEnv resourceEnv = SymbolEnv.createResourceActionSymbolEnv(resourceNode, resourceSymbol.scope, symbolEnv);
if (isWithinParameterContext(resourceName, NODE_TYPE_RESOURCE)) {
this.populateSymbols(this.resolveAllVisibleSymbols(resourceEnv), resourceEnv);
setTerminateVisitor(true);
} else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(resourceNode.getPosition(), resourceNode, this, this.documentServiceContext)) {
// TODO:Handle Annotation attachments
// Visit the endpoints
resourceNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, resourceEnv));
// Cursor position is calculated against the resource parameter scope resolver
cursorPositionResolver = ResourceParamScopeResolver.class;
resourceNode.workers.forEach(w -> this.acceptNode(w, resourceEnv));
this.blockOwnerStack.push(resourceNode);
// Cursor position is calculated against the Block statement scope resolver
cursorPositionResolver = BlockStatementScopeResolver.class;
acceptNode(resourceNode.body, resourceEnv);
this.blockOwnerStack.pop();
}
}
use of org.wso2.transport.http.netty.config.Parameter in project ballerina by ballerina-lang.
the class HtmlDocTest method testFunctions.
@Test(description = "Functions in a package should be shown in the constructs")
public void testFunctions() throws Exception {
BLangPackage bLangPackage = createPackage("package x.y; public function hello(string name) returns (string){}");
Page page = generatePage(bLangPackage);
Assert.assertEquals(page.constructs.size(), 1);
Assert.assertEquals(page.constructs.get(0).name, "hello");
Assert.assertTrue(page.constructs.get(0) instanceof FunctionDoc, "Invalid documentable type");
FunctionDoc functionDoc = (FunctionDoc) page.constructs.get(0);
Assert.assertEquals(functionDoc.parameters.get(0).toString(), "string name", "Invalid parameter string value");
Assert.assertEquals(functionDoc.returnParams.get(0).toString(), "string", "Invalid return type");
}
use of org.wso2.transport.http.netty.config.Parameter in project ballerina by ballerina-lang.
the class HtmlDocTest method testEnumPropertiesExtracted.
@Test(description = "Enum properties should be available via construct")
public void testEnumPropertiesExtracted() throws Exception {
BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"The direction of the parameter\"}\n" + "@Field { value:\"IN: IN parameters are used to send values to stored procedures\"}\n" + "@Field { value:\"OUT: OUT parameters are used to get values from stored procedures\"}\n" + "public enum Direction { IN,OUT}");
EnumDoc enumDoc = Generator.createDocForNode(bLangPackage.getEnums().get(0));
Assert.assertEquals(enumDoc.name, "Direction", "Enum name should be extracted");
Assert.assertEquals(enumDoc.description, "The direction of the parameter", "Description of the " + "enum should be extracted");
// Enumerators inside the enum
Assert.assertEquals(enumDoc.enumerators.get(0).name, "IN", "Enumerator name should be extracted");
Assert.assertEquals(enumDoc.enumerators.get(0).description, "IN parameters are used to send values to " + "stored procedures", "Description of the enumerator should be extracted");
}
use of org.wso2.transport.http.netty.config.Parameter in project ballerina by ballerina-lang.
the class HtmlDocTest method testConnectorPropertiesExtracted.
@Test(description = "Connector properties should be available via construct", enabled = false)
public void testConnectorPropertiesExtracted() throws Exception {
BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Http client connector for outbound HTTP requests\"}\n" + "@Param { value:\"serviceUri: Url of the service\" }\n" + "@Param { value:\"n: connector options\" }" + "connector HttpClient (string serviceUri, int n) {" + "@Description { value:\"The POST action implementation of the HTTP ConnectorDoc\"}\n" + "@Param { value:\"path: Resource path \" }\n" + "@Param { value:\"req: An HTTP Request struct\" }\n" + "@Return { value:\"The response message\" }\n" + "@Return { value:\"Error occured during HTTP client invocation\" }\n" + "action post(string path, string req) (string, int) { return \"value within filter\"; }}");
ConnectorDoc connectorDoc = Generator.createDocForNode(bLangPackage.getConnectors().get(0));
Assert.assertEquals(connectorDoc.name, "HttpClient", "Connector name should be extracted");
Assert.assertEquals(connectorDoc.description, "Http client connector for outbound HTTP requests", "Description of the connector should be extracted");
Assert.assertEquals(connectorDoc.parameters.get(0).name, "serviceUri", "Parameter name should be extracted");
Assert.assertEquals(connectorDoc.parameters.get(0).dataType, "string", "Parameter type should be extracted");
Assert.assertEquals(connectorDoc.parameters.get(0).description, "Url of the service", "Description of the parameter type should be extracted");
// For actions inside the connector
ActionDoc actionDoc = (ActionDoc) connectorDoc.children.get(0);
Assert.assertEquals(actionDoc.name, "post", "Action name should be extracted");
Assert.assertEquals(actionDoc.description, "The POST action implementation of the HTTP ConnectorDoc", "Description of the action should be extracted");
Assert.assertEquals(actionDoc.parameters.get(0).name, "path", "Parameter name should be extracted");
Assert.assertEquals(actionDoc.parameters.get(0).dataType, "string", "Parameter type should be extracted");
Assert.assertEquals(actionDoc.parameters.get(0).description, "Resource path", "Description of the " + "parameter should be extracted");
Assert.assertEquals(actionDoc.returnParams.get(1).dataType, "int", "Return parameter type should be extracted");
Assert.assertEquals(actionDoc.returnParams.get(1).description, "Error occured during HTTP client invocation", "Description of the return parameter should be extracted");
}
Aggregations