Search in sources :

Example 81 with Parameter

use of org.wso2.siddhi.annotation.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;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) TokenStream(org.antlr.v4.runtime.TokenStream) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 82 with Parameter

use of org.wso2.siddhi.annotation.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();
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) LSNodeVisitor(org.ballerinalang.langserver.common.LSNodeVisitor) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) Map(java.util.Map) ServiceScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ServiceScopeResolver) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) Names(org.wso2.ballerinalang.compiler.util.Names) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ConnectorScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ConnectorScopeResolver) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) ScopeResolverConstants(org.ballerinalang.langserver.completions.util.ScopeResolverConstants) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ObjectTypeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ObjectTypeScopeResolver) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) PackageNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.PackageNodeScopeResolver) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) HashMap(java.util.HashMap) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode) MatchStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.MatchStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) TopLevelNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.TopLevelNodeScopeResolver) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) Node(org.ballerinalang.model.tree.Node) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 83 with Parameter

use of org.wso2.siddhi.annotation.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");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) Page(org.ballerinalang.docgen.model.Page) Test(org.testng.annotations.Test)

Example 84 with Parameter

use of org.wso2.siddhi.annotation.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");
}
Also used : EnumDoc(org.ballerinalang.docgen.model.EnumDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Test(org.testng.annotations.Test)

Example 85 with Parameter

use of org.wso2.siddhi.annotation.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");
}
Also used : ActionDoc(org.ballerinalang.docgen.model.ActionDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) ConnectorDoc(org.ballerinalang.docgen.model.ConnectorDoc) Test(org.testng.annotations.Test)

Aggregations

HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)19 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)11 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)11 CharonException (org.wso2.charon3.core.exceptions.CharonException)11 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)11 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)11 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)11 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)11 Test (org.testng.annotations.Test)8 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)8 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)8 List (java.util.List)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)7 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)7 Parameter (org.apache.axis2.description.Parameter)6 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)6