use of org.ballerinalang.langserver.CollectDiagnosticListener in project ballerina by ballerina-lang.
the class ParserUtils method compile.
/**
* Compile a Ballerina file.
*
* @param content file content
* @param path file path
* @param compilerPhase {CompilerPhase} set phase for the compiler.
* @return
*/
public static BallerinaFile compile(String content, Path path, CompilerPhase compilerPhase) {
if (documentManager.isFileOpen(path)) {
documentManager.updateFile(path, content);
} else {
documentManager.openFile(path, content);
}
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(path);
String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, path.toString());
LSDocument sourceDocument = new LSDocument();
sourceDocument.setUri(path.toUri().toString());
sourceDocument.setSourceRoot(sourceRoot);
PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, documentManager);
CompilerContext context = TextDocumentServiceUtil.prepareCompilerContext(packageRepository, sourceDocument, true, documentManager, CompilerPhase.DEFINE);
List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics = new ArrayList<>();
CollectDiagnosticListener diagnosticListener = new CollectDiagnosticListener(balDiagnostics);
context.put(DiagnosticListener.class, diagnosticListener);
BLangPackage bLangPackage = null;
try {
Compiler compiler = Compiler.getInstance(context);
if ("".equals(pkgName)) {
Path filePath = path.getFileName();
if (filePath != null) {
bLangPackage = compiler.compile(filePath.toString());
}
} else {
bLangPackage = compiler.compile(pkgName);
}
} catch (Exception e) {
// Ignore.
}
BallerinaFile bfile = new BallerinaFile();
bfile.setBLangPackage(bLangPackage);
bfile.setDiagnostics(balDiagnostics);
return bfile;
}
Aggregations