/** * Add function node location to locations list. * * @param node function node to calculate the location of * @param ownerPkg package of the owner * @param currentPkg package of the current node as a list of package paths */ private void addLocation(BLangFunction node, String ownerPkg, String currentPkg) { DiagnosticPos position = node.getPosition(); Set<Whitespace> wsSet = node.getWS(); if (wsSet != null && wsSet.size() > 4 && !node.annAttachments.isEmpty()) { Whitespace[] wsArray = new Whitespace[wsSet.size()]; wsSet.toArray(wsArray); Arrays.sort(wsArray); int lastAnnotationEndline = CommonUtil.getLastItem(node.annAttachments).pos.eLine; position.sLine = lastAnnotationEndline + wsArray[0].getWs().split(CommonUtil.LINE_SEPARATOR_SPLIT).length - 1; } this.locations.add(getLocation(node, ownerPkg, currentPkg)); }
/** * Get the Documentation attachment for the function. * * @param bLangPackage BLangPackage built * @param line Start line of the function in the source * @return {@link DocAttachmentInfo} Documentation attachment for the function */ private static DocAttachmentInfo getFunctionDocumentationByPosition(BLangPackage bLangPackage, int line) { List<BLangFunction> filteredFunctions = new ArrayList<>(); for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) { if (topLevelNode instanceof BLangFunction) { filteredFunctions.add((BLangFunction) topLevelNode); } else if (topLevelNode instanceof BLangTypeDefinition && ((BLangTypeDefinition) topLevelNode).typeNode instanceof BLangObjectTypeNode) { filteredFunctions .addAll(((BLangObjectTypeNode) (((BLangTypeDefinition) topLevelNode).typeNode)).getFunctions()); } } for (BLangFunction filteredFunction : filteredFunctions) { DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition(filteredFunction.getPosition()); int functionStart = functionPos.getStartLine(); if (functionStart == line) { return getFunctionNodeDocumentation(filteredFunction); } } return null; }
private static DocAttachmentInfo getFunctionNodeDocumentation(BLangFunction bLangFunction) { List<String> attributes = new ArrayList<>(); DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition(bLangFunction.getPosition()); List<BLangAnnotationAttachment> annotations = bLangFunction.getAnnotationAttachments(); Position docStart = getDocumentationStartPosition(bLangFunction.getPosition(), annotations); int offset = functionPos.getStartColumn(); List<BLangVariable> params = new ArrayList<>(bLangFunction.getParameters()); if (bLangFunction.getRestParameters() != null) { params.add((BLangVariable) bLangFunction.getRestParameters()); } params.addAll(bLangFunction.getDefaultableParameters() .stream() .map(bLangVariableDef -> bLangVariableDef.var) .collect(Collectors.toList())); params.sort(new FunctionArgsComparator()); params.forEach(param -> attributes.add(getDocAttributeFromBLangVariable((BLangSimpleVariable) param, offset))); if (bLangFunction.symbol.retType.getKind() != TypeKind.NIL) { attributes.add(getReturnFieldDescription(offset)); } return new DocAttachmentInfo(getDocumentationAttachment(attributes, functionPos.getStartColumn()), docStart); }
List<BLangAnnotationAttachment> annotations = bLangFunction.annAttachments; int firstAnnotationStart = -1; DiagnosticPos resourcePosition = CommonUtil.toZeroBasedPosition(bLangFunction.getPosition()); if (!annotations.isEmpty()) { firstAnnotationStart = CommonUtil
DiagnosticPos funcPosition = bLangFunction.getPosition(); DiagnosticPos position = new DiagnosticPos(funcPosition.src, funcPosition.sLine, funcPosition.eLine, funcPosition.sCol,
private boolean isLambdaFunction(BLangFunction fn) { List<BLangAnnotationAttachment> annotations = fn.annAttachments; boolean hasLambdaAnnon = false; for (AnnotationAttachmentNode attachmentNode : annotations) { hasLambdaAnnon = this.hasLambaAnnotation(attachmentNode); if (hasLambdaAnnon) { break; } } if (hasLambdaAnnon) { BLangFunction bfn = (BLangFunction) fn; if (!this.validateLambdaFunction(bfn)) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, fn.getPosition(), "Invalid function signature for an AWS lambda function: " + bfn + ", it should be 'public function (awslambda:Context, json) returns json|error'"); return false; } else { return true; } } else { return false; } }
msg = String.format(msg, resource.getName().getValue(), serviceName); if (parameters.size() != 1) { dlog.logDiagnostic(ERROR, resource.getPosition(), msg); return; if (!"file".equals(event.tsymbol.pkgID.name.value) || !FILE_SYSTEM_EVENT .equals(event.tsymbol.name.value)) { dlog.logDiagnostic(ERROR, resource.getPosition(), msg); return; dlog.logDiagnostic(ERROR, resource.getPosition(), msg); dlog.logDiagnostic(ERROR, resource.getPosition(), "Invalid resource name " + resource.getName().getValue() + " in service " + serviceName);
@Override public void visit(BLangFunction funcNode) { String functionName = funcNode.getName().getValue(); SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, this.symbolEnv); CursorPositionResolver cpr = CursorPositionResolvers.getResolverByClass(this.cursorPositionResolver); funcNode.annAttachments.forEach(annotationAttachment -> this.acceptNode(annotationAttachment, funcEnv)); boolean withinParamContext = CompletionVisitorUtil .isWithinParameterContext(functionName, UtilSymbolKeys.FUNCTION_KEYWORD_KEY, funcEnv, lsContext, this); boolean cursorBeforeNode = cpr.isCursorBeforeNode(funcNode.getPosition(), this, this.lsContext, funcNode, funcNode.symbol); if (terminateVisitor || cursorBeforeNode || withinParamContext) { return; } if (funcNode.getBody() != null) { this.blockOwnerStack.push(funcNode); this.cursorPositionResolver = BlockStatementScopeResolver.class; this.acceptNode(funcNode.body, funcEnv); this.blockOwnerStack.pop(); } }