Search in sources :

Example 1 with Lsp4jConversion

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;
}
Also used : Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) AstNode(com.sonar.sslr.api.AstNode) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Logger(org.slf4j.Logger) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) LoggerFactory(org.slf4j.LoggerFactory) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) ArrayList(java.util.ArrayList) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) LocalTypeReasoner(nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner) List(java.util.List) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Lsp4jConversion(nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion) Position(org.eclipse.lsp4j.Position) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) ArrayList(java.util.ArrayList) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with Lsp4jConversion

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));
}
Also used : GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) AstNode(com.sonar.sslr.api.AstNode) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) PrepareRenameResult(org.eclipse.lsp4j.PrepareRenameResult) Collectors(java.util.stream.Collectors) ServerCapabilities(org.eclipse.lsp4j.ServerCapabilities) List(java.util.List) Stream(java.util.stream.Stream) TextEdit(org.eclipse.lsp4j.TextEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameOptions(org.eclipse.lsp4j.RenameOptions) Lsp4jConversion(nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Position(org.eclipse.lsp4j.Position) Range(nl.ramsolutions.sw.magik.analysis.Range) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) CheckForNull(javax.annotation.CheckForNull) AstQuery(nl.ramsolutions.sw.magik.analysis.AstQuery) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) TextEdit(org.eclipse.lsp4j.TextEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AstNode(com.sonar.sslr.api.AstNode) Lsp4jConversion(nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)2 List (java.util.List)2 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)2 AstQuery (nl.ramsolutions.sw.magik.analysis.AstQuery)2 MagikGrammar (nl.ramsolutions.sw.magik.api.MagikGrammar)2 Lsp4jConversion (nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion)2 Position (org.eclipse.lsp4j.Position)2 ServerCapabilities (org.eclipse.lsp4j.ServerCapabilities)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CheckForNull (javax.annotation.CheckForNull)1 Range (nl.ramsolutions.sw.magik.analysis.Range)1 MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)1 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)1 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)1 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)1 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)1 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)1