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);
}
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);
}
});
}
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);
}
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;
}
Aggregations