Search in sources :

Example 6 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class ParserRuleMatchStatementContextResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    int currentTokenIndex = completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
    List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    while (true) {
        if (currentTokenIndex < 0) {
            // Ideally should not come to this point
            return completionItems;
        }
        Token token = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
        if (token.getText().equals(UtilSymbolKeys.MATCH_KEYWORD_KEY)) {
            currentTokenIndex = token.getTokenIndex();
            break;
        } else {
            currentTokenIndex = token.getTokenIndex();
        }
    }
    String identifierMatched = CommonUtil.getNextDefaultToken(tokenStream, currentTokenIndex).getText();
    SymbolInfo identifierSymbol = visibleSymbols.stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.getName().getValue().equals(identifierMatched)).findFirst().orElseGet(null);
    if (identifierSymbol == null) {
        return completionItems;
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BUnionType) {
        Set<BType> memberTypes = ((BUnionType) identifierSymbol.getScopeEntry().symbol.type).getMemberTypes();
        memberTypes.forEach(bType -> {
            completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
        });
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BJSONType) {
        ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
        List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(filteredBasicTypes, completionItems);
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BStructType) {
        List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(structSymbols, completionItems);
    }
    return completionItems;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Set(java.util.Set) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletionItem(org.eclipse.lsp4j.CompletionItem) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) List(java.util.List) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) AbstractItemResolver(org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) TokenStream(org.antlr.v4.runtime.TokenStream) Set(java.util.Set) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class GlobalScopeResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    ParserRuleContext parserRuleContext = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY);
    if (parserRuleContext == null) {
        // If the parser rule context is null we don't have any errors. In this case we add the types
        List<SymbolInfo> bTypeSymbolInfo = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY).stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.type != null).collect(Collectors.toList());
        this.populateCompletionItemList(bTypeSymbolInfo, completionItems);
    } else {
        return CompletionItemResolver.getResolverByClass(parserRuleContext.getClass()).resolveItems(completionContext);
    }
    return completionItems;
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Collectors(java.util.stream.Collectors) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) CompletionItemResolver(org.ballerinalang.langserver.completions.util.CompletionItemResolver) ArrayList(java.util.ArrayList) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 8 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class SignatureHelpUtilTest method getCallableItemNameTest.

@Test(description = "Test get callable unit name", dataProvider = "positionData")
public void getCallableItemNameTest(Position position, String funcName) throws URISyntaxException, IOException {
    URI fileLocation = CLASS_LOADER.getResource("signature" + File.separator + "util" + File.separator + "testUtil.bal").toURI();
    String stringContent = new String(Files.readAllBytes(Paths.get(fileLocation)));
    TextDocumentServiceContext serviceContext = new TextDocumentServiceContext();
    SignatureHelpUtil.captureCallableItemInfo(position, stringContent, serviceContext);
    String capturedFunctionName = serviceContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
    Assert.assertEquals(funcName, capturedFunctionName);
}
Also used : TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 9 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class CompletionTestUtil method getCompletions.

/**
 * Get the completions list.
 *
 * @param documentManager Document manager instance
 * @param pos             {@link TextDocumentPositionParams} position params
 */
public static List<CompletionItem> getCompletions(WorkspaceDocumentManagerImpl documentManager, TextDocumentPositionParams pos) {
    List<CompletionItem> completions;
    TextDocumentServiceContext completionContext = new TextDocumentServiceContext();
    completionContext.put(DocumentServiceKeys.POSITION_KEY, pos);
    completionContext.put(DocumentServiceKeys.FILE_URI_KEY, pos.getTextDocument().getUri());
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(completionContext, documentManager, false, CompletionCustomErrorStrategy.class, false).get(0);
    completionContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
    // Visit the package to resolve the symbols
    TreeVisitor treeVisitor = new TreeVisitor(completionContext);
    bLangPackage.accept(treeVisitor);
    BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
    if (symbolEnvNode == null) {
        completions = CompletionItemResolver.getResolverByClass(TopLevelResolver.class).resolveItems(completionContext);
    } else {
        completions = CompletionItemResolver.getResolverByClass(symbolEnvNode.getClass()).resolveItems(completionContext);
    }
    return completions;
}
Also used : TreeVisitor(org.ballerinalang.langserver.completions.TreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionCustomErrorStrategy(org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Aggregations

TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)7 Collectors (java.util.stream.Collectors)5 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)5 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 UtilSymbolKeys (org.ballerinalang.langserver.common.UtilSymbolKeys)3 CommonUtil (org.ballerinalang.langserver.common.utils.CommonUtil)3 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)3 Position (org.eclipse.lsp4j.Position)3 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)3 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)3 Token (org.antlr.v4.runtime.Token)2 TokenStream (org.antlr.v4.runtime.TokenStream)2