use of org.ballerinalang.repository.PackageRepository 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;
}
use of org.ballerinalang.repository.PackageRepository in project ballerina by ballerina-lang.
the class TextDocumentServiceUtil method getBLangPackage.
/**
* Get the BLangPackage for a given program.
*
* @param context Language Server Context
* @param docManager Document manager
* @param preserveWhitespace Enable preserve whitespace
* @param customErrorStrategy custom error strategy class
* @param compileFullProject compile full project from the source root
* @return {@link BLangPackage} BLang Package
*/
public static List<BLangPackage> getBLangPackage(LanguageServerContext context, WorkspaceDocumentManager docManager, boolean preserveWhitespace, Class customErrorStrategy, boolean compileFullProject) {
String uri = context.get(DocumentServiceKeys.FILE_URI_KEY);
LSDocument document = new LSDocument(uri);
Path filePath = CommonUtil.getPath(document);
Path fileNamePath = filePath.getFileName();
String fileName = "";
if (fileNamePath != null) {
fileName = fileNamePath.toString();
}
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(filePath);
String pkgName = TextDocumentServiceUtil.getPackageNameForGivenFile(sourceRoot, filePath.toString());
LSDocument sourceDocument = new LSDocument();
sourceDocument.setUri(uri);
sourceDocument.setSourceRoot(sourceRoot);
PackageRepository packageRepository = new WorkspacePackageRepository(sourceRoot, docManager);
List<BLangPackage> packages = new ArrayList<>();
if (compileFullProject) {
if (!sourceRoot.isEmpty()) {
File projectDir = new File(sourceRoot);
File[] files = projectDir.listFiles();
if (files != null) {
for (File file : files) {
if ((file.isDirectory() && !file.getName().startsWith(".")) || (!file.isDirectory() && file.getName().endsWith(".bal"))) {
Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
packages.add(compiler.compile(file.getName()));
}
}
}
}
} else {
Compiler compiler = getCompiler(context, fileName, packageRepository, sourceDocument, preserveWhitespace, customErrorStrategy, docManager);
if ("".equals(pkgName)) {
packages.add(compiler.compile(fileName));
} else {
packages.add(compiler.compile(pkgName));
}
}
return packages;
}
use of org.ballerinalang.repository.PackageRepository 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);
}
Aggregations