use of org.ballerinalang.langserver.common.LSDocument in project ballerina by ballerina-lang.
the class CommandUtil method getCommandsByDiagnostic.
/**
* Get the command instances for a given diagnostic.
* @param diagnostic Diagnostic to get the command against
* @param params Code Action parameters
* @param lsPackageCache Lang Server Package cache
* @return {@link List} List of commands related to the given diagnostic
*/
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
String diagnosticMessage = diagnostic.getMessage();
List<Command> commands = new ArrayList<>();
if (isUndefinedPackage(diagnosticMessage)) {
String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
Path openedPath = CommonUtil.getPath(sourceDocument);
String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
sourceDocument.setSourceRoot(sourceRoot);
lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
}).forEach(pkgEntry -> {
PackageID packageID = pkgEntry.getValue().packageID;
String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
});
}
return commands;
}
use of org.ballerinalang.langserver.common.LSDocument 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.langserver.common.LSDocument in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method formatting.
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
return CompletableFuture.supplyAsync(() -> {
String textEditContent = null;
TextDocumentServiceContext formatContext = new TextDocumentServiceContext();
formatContext.put(DocumentServiceKeys.FILE_URI_KEY, params.getTextDocument().getUri());
LSDocument document = new LSDocument(params.getTextDocument().getUri());
String fileContent = documentManager.getFileContent(CommonUtil.getPath(document));
String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
int totalLines = contentComponents.length;
Range range = new Range(new Position(0, 0), new Position(totalLines, lastCharCol));
try {
// Source generation for given ast.
JsonObject ast = TextDocumentFormatUtil.getAST(params, documentManager, formatContext);
SourceGen sourceGen = new SourceGen(0);
sourceGen.build(ast.getAsJsonObject("model"), null, "CompilationUnit");
textEditContent = sourceGen.getSourceOf(ast.getAsJsonObject("model"), true, false);
} catch (Exception e) {
// Ignore
}
TextEdit textEdit = textEditContent != null ? new TextEdit(range, textEditContent) : null;
return Collections.singletonList(textEdit);
});
}
use of org.ballerinalang.langserver.common.LSDocument in project ballerina by ballerina-lang.
the class CommonUtil method topLevelNodeTypeInLine.
/**
* Get the top level node type in the line.
*
* @param identifier Document Identifier
* @param startPosition Start position
* @param docManager Workspace document manager
* @return {@link String} Top level node type
*/
public static String topLevelNodeTypeInLine(TextDocumentIdentifier identifier, Position startPosition, WorkspaceDocumentManager docManager) {
// TODO: Need to support service and resources as well.
List<String> topLevelKeywords = Arrays.asList("function", "service", "resource", "struct", "enum", "transformer", "object");
LSDocument document = new LSDocument(identifier.getUri());
String fileContent = docManager.getFileContent(getPath(document));
String[] splitedFileContent = fileContent.split("\\n|\\r\\n|\\r");
if ((splitedFileContent.length - 1) >= startPosition.getLine()) {
String lineContent = splitedFileContent[startPosition.getLine()];
List<String> alphaNumericTokens = new ArrayList<>(Arrays.asList(lineContent.split("[^\\w']+")));
for (String topLevelKeyword : topLevelKeywords) {
if (alphaNumericTokens.contains(topLevelKeyword)) {
return topLevelKeyword;
}
}
}
return null;
}
use of org.ballerinalang.langserver.common.LSDocument in project ballerina by ballerina-lang.
the class RenameUtil method getReplaceSymbolName.
/**
* Get the symbol name to replace.
*
* @param params {@link RenameParams} for operation
* @param documentManager {@link WorkspaceDocumentManager} instance
* @return {@link String} Symbol name to replace
*/
public static String getReplaceSymbolName(RenameParams params, WorkspaceDocumentManager documentManager) {
Position position = params.getPosition();
int line = position.getLine();
int column = position.getCharacter();
String fileContent = documentManager.getFileContent(CommonUtil.getPath(new LSDocument(params.getTextDocument().getUri())));
String lineContent = fileContent.split("\\n|\\r\\n|\\r")[line];
return getIdentifierLiterals(lineContent, column - 1, -1).reverse().toString() + getIdentifierLiterals(lineContent, column, 1).toString();
}
Aggregations