Java tutorial
/* * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.ballerinalang.composer.service.workspace.suggetions; import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CommonTokenStream; import org.apache.commons.lang3.StringUtils; import org.ballerinalang.composer.service.workspace.langserver.dto.Position; import org.ballerinalang.composer.service.workspace.rest.datamodel.BFile; import org.ballerinalang.composer.service.workspace.rest.datamodel.BallerinaComposerModelBuilder; import org.ballerinalang.model.BLangPackage; import org.ballerinalang.model.BallerinaFile; import org.ballerinalang.model.GlobalScope; import org.ballerinalang.model.types.BTypes; import org.ballerinalang.util.parser.BallerinaLexer; import org.ballerinalang.util.parser.BallerinaParser; import org.ballerinalang.util.parser.antlr4.BLangAntlr4Listener; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * Completion Suggester Impl */ public class AutoCompleteSuggesterImpl implements AutoCompleteSuggester { @Override public BallerinaFile getBallerinaFile(BFile bFile, Position cursorPosition, CapturePossibleTokenStrategy capturePossibleTokenStrategy) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream( bFile.getContent().getBytes(StandardCharsets.UTF_8)); ANTLRInputStream antlrInputStream = new ANTLRInputStream(inputStream); BallerinaLexer ballerinaLexer = new BallerinaLexer(antlrInputStream); CommonTokenStream ballerinaToken = new CommonTokenStream(ballerinaLexer); BallerinaParser ballerinaParser = new BallerinaParser(ballerinaToken); ballerinaParser.setErrorHandler(capturePossibleTokenStrategy); GlobalScope globalScope = GlobalScope.getInstance(); BTypes.loadBuiltInTypes(globalScope); BLangPackage bLangPackage = new BLangPackage(globalScope); BLangPackage.PackageBuilder packageBuilder = new BLangPackage.PackageBuilder(bLangPackage); BallerinaComposerModelBuilder bLangModelBuilder = new BallerinaComposerModelBuilder(packageBuilder, StringUtils.EMPTY); BLangAntlr4Listener ballerinaBaseListener = new BLangAntlr4Listener(true, ballerinaToken, bLangModelBuilder, new File(bFile.getFileName()).toPath()); ballerinaParser.addParseListener(ballerinaBaseListener); ballerinaParser.compilationUnit(); BallerinaFile ballerinaFile = bLangModelBuilder.build(); return ballerinaFile; } }