Search in sources :

Example 96 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CommandExecutor method executeAddDocumentation.

/**
 * Execute the add documentation command.
 * @param context   Workspace service context
 */
private static void executeAddDocumentation(WorkspaceServiceContext context) {
    String topLevelNodeType = "";
    String documentUri = "";
    int line = 0;
    VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
    for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
        if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
            documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
            textDocumentIdentifier.setUri(documentUri);
            context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
        } else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_NODE_TYPE)) {
            topLevelNodeType = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
        } else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_NODE_LINE)) {
            line = Integer.parseInt((String) ((LinkedTreeMap) arg).get(ARG_VALUE));
        }
    }
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
    CommandUtil.DocAttachmentInfo docAttachmentInfo = getDocumentEditForNodeByPosition(topLevelNodeType, bLangPackage, line);
    if (docAttachmentInfo != null) {
        String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
        String[] contentComponents = fileContent.split(System.lineSeparator());
        int replaceEndCol = contentComponents[line - 1].length();
        String replaceText = String.join(System.lineSeparator(), Arrays.asList(Arrays.copyOfRange(contentComponents, 0, line))) + System.lineSeparator() + docAttachmentInfo.getDocAttachment();
        Range range = new Range(new Position(0, 0), new Position(line - 1, replaceEndCol));
        applySingleTextEdit(replaceText, range, textDocumentIdentifier, context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
    }
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Position(org.eclipse.lsp4j.Position) Range(org.eclipse.lsp4j.Range) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Example 97 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CommandExecutor method executeImportPackage.

/**
 * Execute the command, import package.
 * @param context   Workspace service context
 */
private static void executeImportPackage(WorkspaceServiceContext context) {
    String documentUri = null;
    VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
    for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
        if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
            documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
            textDocumentIdentifier.setUri(documentUri);
            context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
        } else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_PKG_NAME)) {
            context.put(ExecuteCommandKeys.PKG_NAME_KEY, (String) ((LinkedTreeMap) arg).get(ARG_VALUE));
        }
    }
    if (documentUri != null && context.get(ExecuteCommandKeys.PKG_NAME_KEY) != null) {
        String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
        String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
        int totalLines = contentComponents.length;
        int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
        int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
        BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
        context.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
        String pkgName = context.get(ExecuteCommandKeys.PKG_NAME_KEY);
        DiagnosticPos pos;
        // Filter the imports except the runtime import
        List<BLangImportPackage> imports = bLangPackage.getImports().stream().filter(bLangImportPackage -> !bLangImportPackage.getAlias().toString().equals(RUNTIME_PKG_ALIAS)).collect(Collectors.toList());
        if (!imports.isEmpty()) {
            BLangImportPackage lastImport = bLangPackage.getImports().get(bLangPackage.getImports().size() - 1);
            pos = lastImport.getPosition();
        } else if (imports.isEmpty() && bLangPackage.getPackageDeclaration() != null) {
            pos = (DiagnosticPos) bLangPackage.getPackageDeclaration().getPosition();
        } else {
            pos = null;
        }
        int endCol = pos == null ? -1 : pos.getEndColumn() - 1;
        int endLine = pos == null ? 0 : pos.getEndLine() - 1;
        String remainingTextToReplace;
        if (endCol != -1) {
            int contentLengthToReplaceStart = fileContent.substring(0, fileContent.indexOf(contentComponents[endLine])).length() + endCol + 1;
            remainingTextToReplace = fileContent.substring(contentLengthToReplaceStart);
        } else {
            remainingTextToReplace = fileContent;
        }
        String editText = (pos != null ? "\r\n" : "") + "import " + pkgName + ";" + (remainingTextToReplace.startsWith("\n") || remainingTextToReplace.startsWith("\r") ? "" : "\r\n") + remainingTextToReplace;
        Range range = new Range(new Position(endLine, endCol + 1), new Position(totalLines + 1, lastCharCol));
        applySingleTextEdit(editText, range, textDocumentIdentifier, context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) Arrays(java.util.Arrays) ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) WorkspaceServiceContext(org.ballerinalang.langserver.WorkspaceServiceContext) CommandConstants(org.ballerinalang.langserver.common.constants.CommandConstants) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) TextEdit(org.eclipse.lsp4j.TextEdit) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Position(org.eclipse.lsp4j.Position) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) URI(java.net.URI) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Collectors(java.util.stream.Collectors) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) TextDocumentServiceUtil(org.ballerinalang.langserver.TextDocumentServiceUtil) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Paths(java.nio.file.Paths) Node(org.ballerinalang.model.tree.Node) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Collections(java.util.Collections) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Position(org.eclipse.lsp4j.Position) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Range(org.eclipse.lsp4j.Range) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Example 98 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CommandUtil method getTransformerDocumentationByPosition.

/**
 * Get the Documentation attachment for the transformer.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the transformer in the source
 * @return {@link String}   Documentation attachment for the transformer
 */
static DocAttachmentInfo getTransformerDocumentationByPosition(BLangPackage bLangPackage, int line) {
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangTransformer) {
            BLangTransformer transformerNode = (BLangTransformer) topLevelNode;
            DiagnosticPos transformerPos = CommonUtil.toZeroBasedPosition(transformerNode.getPosition());
            int transformerStart = transformerPos.getStartLine();
            if (transformerStart == line) {
                return getTransformerNodeDocumentation(transformerNode, line);
            }
        }
    }
    return null;
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 99 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CommandUtil method getResourceDocumentationByPosition.

/**
 * Get the Documentation attachment for the resource.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the resource in the source
 * @return {@link String}   Documentation attachment for the resource
 */
static DocAttachmentInfo getResourceDocumentationByPosition(BLangPackage bLangPackage, int line) {
    // TODO: Currently resource position is invalid and we use the annotation attachment positions.
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangService) {
            BLangService serviceNode = (BLangService) topLevelNode;
            for (BLangResource bLangResource : serviceNode.getResources()) {
                List<BLangAnnotationAttachment> annotationAttachments = bLangResource.getAnnotationAttachments();
                DiagnosticPos resourcePos = CommonUtil.toZeroBasedPosition(bLangResource.getPosition());
                if (!annotationAttachments.isEmpty()) {
                    DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
                    if (lastAttachmentPos.getEndLine() < line && line < resourcePos.getEndLine()) {
                        return getResourceNodeDocumentation(bLangResource, lastAttachmentPos.getEndLine() + 1);
                    }
                } else if (resourcePos.getStartLine() == line) {
                    return getResourceNodeDocumentation(bLangResource, line);
                }
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 100 with BLangPackage

use of org.wso2.ballerinalang.compiler.tree.BLangPackage in project ballerina by ballerina-lang.

the class CommandUtil method getFunctionDocumentationByPosition.

/**
 * Get the Documentation attachment for the function.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the function in the source
 * @return {@link String}   Documentation attachment for the function
 */
static DocAttachmentInfo getFunctionDocumentationByPosition(BLangPackage bLangPackage, int line) {
    List<FunctionNode> filteredFunctions = new ArrayList<>();
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangFunction) {
            filteredFunctions.add((BLangFunction) topLevelNode);
        } else if (topLevelNode instanceof BLangObject) {
            filteredFunctions.addAll(((BLangObject) topLevelNode).getFunctions());
        }
        for (FunctionNode filteredFunction : filteredFunctions) {
            DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) filteredFunction.getPosition());
            int functionStart = functionPos.getStartLine();
            if (functionStart == line) {
                return getFunctionNodeDocumentation(filteredFunction, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) FunctionNode(org.ballerinalang.model.tree.FunctionNode) ArrayList(java.util.ArrayList) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Aggregations

BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)78 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)19 Page (org.ballerinalang.docgen.model.Page)18 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)15 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)14 IOException (java.io.IOException)13 List (java.util.List)13 Path (java.nio.file.Path)12 Compiler (org.wso2.ballerinalang.compiler.Compiler)12 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)12 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)11 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)9 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9