Search in sources :

Example 11 with LSDocument

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

the class BallerinaTextDocumentService method didOpen.

@Override
public void didOpen(DidOpenTextDocumentParams params) {
    LSDocument document = new LSDocument(params.getTextDocument().getUri());
    Path openedPath = CommonUtil.getPath(document);
    if (openedPath == null) {
        return;
    }
    String content = params.getTextDocument().getText();
    this.documentManager.openFile(openedPath, content);
    compileAndSendDiagnostics(content, document, openedPath);
}
Also used : Path(java.nio.file.Path) LSDocument(org.ballerinalang.langserver.common.LSDocument) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 12 with LSDocument

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

the class BallerinaTextDocumentService method didChange.

@Override
public void didChange(DidChangeTextDocumentParams params) {
    LSDocument document = new LSDocument(params.getTextDocument().getUri());
    Path changedPath = CommonUtil.getPath(document);
    if (changedPath == null) {
        return;
    }
    String content = params.getContentChanges().get(0).getText();
    this.documentManager.updateFile(changedPath, content);
    this.diagPushDebouncer.call(new Runnable() {

        @Override
        public void run() {
            compileAndSendDiagnostics(content, document, changedPath);
        }
    });
}
Also used : Path(java.nio.file.Path) LSDocument(org.ballerinalang.langserver.common.LSDocument) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 13 with LSDocument

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

the class BallerinaTextDocumentService method compileAndSendDiagnostics.

private void compileAndSendDiagnostics(String content, LSDocument document, Path path) {
    String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
    String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
    LSDocument sourceDocument = new LSDocument();
    sourceDocument.setUri(document.getURIString());
    sourceDocument.setSourceRoot(sourceRoot);
    PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
    CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, false, documentManager);
    List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
    CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
    context.put(DiagnosticListener.class, diagnosticListener);
    Compiler compiler = Compiler.getInstance(context);
    if ("".equals(pkgName)) {
        Path filePath = path.getFileName();
        if (filePath != null) {
            compiler.compile(filePath.toString());
        }
    } else {
        compiler.compile(pkgName);
    }
    publishDiagnostics(balDiagnostics, path);
}
Also used : Path(java.nio.file.Path) Compiler(org.wso2.ballerinalang.compiler.Compiler) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) PackageRepository(org.ballerinalang.repository.PackageRepository) MarkedString(org.eclipse.lsp4j.MarkedString) WorkspacePackageRepository(org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository) LSDocument(org.ballerinalang.langserver.common.LSDocument)

Example 14 with LSDocument

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

the class CommonUtil method getCurrentPackageByFileName.

/**
 * Get current package by given file name.
 *
 * @param packages list of packages to be searched
 * @param fileUri  string file URI
 * @return {@link BLangPackage} current package
 */
public static BLangPackage getCurrentPackageByFileName(List<BLangPackage> packages, String fileUri) {
    LSDocument document = new LSDocument(fileUri);
    Path filePath = getPath(document);
    Path fileNamePath = filePath.getFileName();
    BLangPackage currentPackage = null;
    try {
        found: for (BLangPackage bLangPackage : packages) {
            for (BLangCompilationUnit compilationUnit : bLangPackage.getCompilationUnits()) {
                if (compilationUnit.name.equals(fileNamePath.getFileName().toString())) {
                    currentPackage = bLangPackage;
                    break found;
                }
            }
        }
    } catch (NullPointerException e) {
        currentPackage = packages.get(0);
    }
    return currentPackage;
}
Also used : Path(java.nio.file.Path) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) LSDocument(org.ballerinalang.langserver.common.LSDocument) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)

Aggregations

LSDocument (org.ballerinalang.langserver.common.LSDocument)14 Path (java.nio.file.Path)10 ArrayList (java.util.ArrayList)7 Position (org.eclipse.lsp4j.Position)5 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)5 MarkedString (org.eclipse.lsp4j.MarkedString)4 Range (org.eclipse.lsp4j.Range)4 List (java.util.List)3 CommonUtil (org.ballerinalang.langserver.common.utils.CommonUtil)3 WorkspacePackageRepository (org.ballerinalang.langserver.workspace.repository.WorkspacePackageRepository)3 PackageRepository (org.ballerinalang.repository.PackageRepository)3 Location (org.eclipse.lsp4j.Location)3 Compiler (org.wso2.ballerinalang.compiler.Compiler)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 LSPackageCache (org.ballerinalang.langserver.LSPackageCache)2 PackageID (org.ballerinalang.model.elements.PackageID)2 Diagnostic (org.eclipse.lsp4j.Diagnostic)2 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)2 TextEdit (org.eclipse.lsp4j.TextEdit)2