use of nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion in project magik-tools by StevenLooman.
the class ImplementationProvider method implementationsForMethodInvocation.
private List<org.eclipse.lsp4j.Location> implementationsForMethodInvocation(final ITypeKeeper typeKeeper, final LocalTypeReasoner reasoner, final AstNode currentNode) {
final MethodInvocationNodeHelper helper = new MethodInvocationNodeHelper(currentNode);
final String methodName = helper.getMethodName();
final AstNode previousSiblingNode = currentNode.getPreviousSibling();
final ExpressionResult result = reasoner.getNodeType(previousSiblingNode);
final AbstractType unsetType = typeKeeper.getType(GlobalReference.of("sw:unset"));
AbstractType type = result.get(0, unsetType);
final List<org.eclipse.lsp4j.Location> locations = new ArrayList<>();
if (type == UndefinedType.INSTANCE) {
LOGGER.debug("Finding implementations for method: {}", methodName);
typeKeeper.getTypes().stream().flatMap(anyType -> anyType.getMethods().stream()).filter(m -> m.getName().equals(methodName)).map(Method::getLocation).map(Lsp4jConversion::locationToLsp4j).forEach(locations::add);
} else {
if (type == SelfType.INSTANCE) {
final AstNode methodDefNode = currentNode.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
final MethodDefinitionNodeHelper methodDefHelper = new MethodDefinitionNodeHelper(methodDefNode);
final GlobalReference globalRef = methodDefHelper.getTypeGlobalReference();
type = typeKeeper.getType(globalRef);
}
LOGGER.debug("Finding implementations for type:, {}, method: {}", type.getFullName(), methodName);
type.getMethods(methodName).stream().map(Method::getLocation).map(Lsp4jConversion::locationToLsp4j).forEach(locations::add);
}
return locations;
}
use of nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion in project magik-tools by StevenLooman.
the class RenameProvider method provideRename.
/**
* Provide rename.
* @param magikFile Magik file.
* @param position Position in magik source.
* @param newName New name.
* @return Edits to workspace.
*/
public WorkspaceEdit provideRename(final MagikTypedFile magikFile, final Position position, final String newName) {
// Parse magik.
final AstNode topNode = magikFile.getTopNode();
// Should always be on an identifier.
final AstNode node = AstQuery.nodeAt(topNode, Lsp4jConversion.positionFromLsp4j(position), MagikGrammar.IDENTIFIER);
if (node == null) {
return null;
}
// Set up scope.
final ScopeEntry scopeEntry = this.findScopeEntry(magikFile, node);
if (scopeEntry == null || scopeEntry.isType(ScopeEntry.Type.GLOBAL) || scopeEntry.isType(ScopeEntry.Type.DYNAMIC) || scopeEntry.isType(ScopeEntry.Type.IMPORT)) {
return null;
}
// Provide edits.
final String uri = magikFile.getUri().toString();
final AstNode definitionNode = scopeEntry.getNode();
final List<TextEdit> textEdits = Stream.concat(Stream.of(definitionNode), scopeEntry.getUsages().stream()).map(renameNode -> renameNode.isNot(MagikGrammar.IDENTIFIER) ? renameNode.getFirstChild(MagikGrammar.IDENTIFIER) : renameNode).map(Range::new).map(Lsp4jConversion::rangeToLsp4j).map(range -> new TextEdit(range, newName)).collect(Collectors.toList());
return new WorkspaceEdit(Map.of(uri, textEdits));
}
Aggregations