use of org.ballerinalang.langserver.LSPackageCache 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.LSPackageCache in project ballerina by ballerina-lang.
the class PackageNameContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
LSPackageCache lsPackageCache = completionContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY);
lsPackageCache.getPackageMap().entrySet().forEach(pkgEntry -> {
PackageID packageID = pkgEntry.getValue().packageID;
String label = packageID.getOrgName().getValue() + "/" + packageID.getName().getValue();
String insertText = label + ";";
fillImportCompletion(label, insertText, completionItems);
});
return completionItems;
}
use of org.ballerinalang.langserver.LSPackageCache in project ballerina by ballerina-lang.
the class DefinitionUtil method getDefinitionPosition.
/**
* Get definition position for the given definition context.
*
* @param definitionContext context of the definition.
* @param lSPackageCache package context for language server.
* @return position
*/
public static List<Location> getDefinitionPosition(TextDocumentServiceContext definitionContext, LSPackageCache lSPackageCache) {
List<Location> contents = new ArrayList<>();
if (definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY) == null) {
return contents;
}
String nodeKind = definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY);
BLangPackage bLangPackage = getPackageOfTheOwner(definitionContext.get(NodeContextKeys.NODE_OWNER_PACKAGE_KEY), definitionContext, lSPackageCache);
BLangNode bLangNode = null;
switch(nodeKind) {
case ContextConstants.FUNCTION:
bLangNode = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.STRUCT:
bLangNode = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.OBJECT:
bLangNode = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ENUM:
bLangNode = bLangPackage.enums.stream().filter(enm -> enm.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
// Fixing the position issue with enum node.
bLangNode.getPosition().eLine = bLangNode.getPosition().sLine;
bLangNode.getPosition().eCol = bLangNode.getPosition().sCol;
break;
case ContextConstants.CONNECTOR:
bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ACTION:
bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) definitionContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.TRANSFORMER:
bLangNode = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ENDPOINT:
bLangNode = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangNode == null) {
DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
}
}
break;
case ContextConstants.VARIABLE:
bLangNode = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(definitionContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
// BLangNode is null only when node at the cursor position is a local variable.
if (bLangNode == null) {
DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
break;
}
}
break;
default:
break;
}
if (bLangNode == null) {
return contents;
}
Location l = new Location();
TextDocumentPositionParams position = definitionContext.get(DocumentServiceKeys.POSITION_KEY);
Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
if (parentPath != null) {
String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
Path filePath = Paths.get(CommonUtil.getPackageURI(definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue(), parentPath.toString(), definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue()), fileName);
l.setUri(filePath.toUri().toString());
Range r = new Range();
// Subtract 1 to convert the token lines and char positions to zero based indexing
r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
l.setRange(r);
contents.add(l);
}
return contents;
}
Aggregations