Search in sources :

Example 1 with MethodDefinitionNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper in project magik-tools by StevenLooman.

the class BreakpointManager method addBreakpoint.

/**
 * Add a new breakpoint to self and debugger.
 *
 * @param source                     Source.
 * @param sourceBreakpoint Source breakpoint.
 * @return New magik breakpoint.
 * @throws IOException -
 * @throws ExecutionException -
 * @throws InterruptedException -
 */
MagikBreakpoint addBreakpoint(final Source source, final SourceBreakpoint sourceBreakpoint) throws IOException, InterruptedException, ExecutionException {
    int line = sourceBreakpoint.getLine();
    final AstNode methodNode = this.getNode(source, line);
    final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodNode);
    String method = null;
    int methodLine = 0;
    if (methodNode == null) {
        method = "<not_in_method>";
    } else {
        method = helper.getPakkageExemplarMethodName();
        methodLine = methodNode.getTokenLine();
        if (methodLine == line) {
            line = 0;
        }
    }
    String condition = sourceBreakpoint.getCondition();
    return this.createBreakpoint(source, method, line, condition);
}
Also used : MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) FunctionBreakpoint(org.eclipse.lsp4j.debug.FunctionBreakpoint) SourceBreakpoint(org.eclipse.lsp4j.debug.SourceBreakpoint) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with MethodDefinitionNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper in project magik-tools by StevenLooman.

the class HoverProvider method buildMethodDoc.

/**
 * Build hover text for method doc.
 * @param typeKeeper TypeKeeper.
 * @param reasoner LocalTypeReasoner.
 * @param node AstNode, METHOD_INVOCATION.
 * @param methodName Name of invoked method.
 * @param builder {{StringBuilder}} to fill.
 */
private void buildMethodDoc(final ITypeKeeper typeKeeper, final LocalTypeReasoner reasoner, final AstNode node, final String methodName, final StringBuilder builder) {
    // Get type from reasoner.
    final ExpressionResult result = reasoner.getNodeType(node);
    // We know what self is.
    AbstractType type = result.get(0, UndefinedType.INSTANCE);
    if (type == SelfType.INSTANCE) {
        final AstNode methodDefNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
        if (methodDefNode == null) {
            // This can happen in case of a procedure definition calling a method on _self.
            type = UndefinedType.INSTANCE;
        } else {
            final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodDefNode);
            final GlobalReference globalRef = helper.getTypeGlobalReference();
            type = typeKeeper.getType(globalRef);
        }
    }
    // Get method info.
    final Collection<Method> methods = type.getMethods(methodName);
    if (methods.isEmpty()) {
        this.buildMethodUnknownDoc(type, methodName, builder);
        return;
    }
    methods.forEach(method -> this.buildMethodSignatureDoc(method, builder));
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) AstNode(com.sonar.sslr.api.AstNode)

Example 3 with MethodDefinitionNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper in project magik-tools by StevenLooman.

the class HoverProvider method provideHoverMethodDefinition.

/**
 * Provide hover for a method definition. Either the exemplar or the method name.
 * @param magikFile Magik file.
 * @param hoveredNode Hovered node.
 * @param builder Builder to add text to.
 */
private void provideHoverMethodDefinition(final MagikTypedFile magikFile, final AstNode hoveredNode, final StringBuilder builder) {
    final ITypeKeeper typeKeeper = magikFile.getTypeKeeper();
    final AstNode methodDefNode = hoveredNode.getParent();
    final MethodDefinitionNodeHelper methodDefHelper = new MethodDefinitionNodeHelper(methodDefNode);
    final List<AstNode> identifierNodes = methodDefNode.getChildren(MagikGrammar.IDENTIFIER);
    if (hoveredNode == identifierNodes.get(0)) {
        // Hovered over exemplar.
        final GlobalReference globalRef = methodDefHelper.getTypeGlobalReference();
        LOGGER.debug("Providing hover for type: {}", globalRef);
        this.buildTypeDoc(typeKeeper, globalRef, builder);
    } else if (hoveredNode == identifierNodes.get(1)) {
        // Hovered over method name.
        final GlobalReference globalRef = methodDefHelper.getTypeGlobalReference();
        final String methodName = methodDefHelper.getMethodName();
        LOGGER.debug("Providing hover for type: {}, method: {}", globalRef, methodName);
        this.buildMethodDoc(typeKeeper, globalRef, methodName, builder);
    }
}
Also used : GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) AstNode(com.sonar.sslr.api.AstNode)

Example 4 with MethodDefinitionNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper in project magik-tools by StevenLooman.

the class HoverProvider method buildTypeDoc.

/**
 * Build hover text for type doc.
 * @param typeKeeper TypeKeeper.
 * @param reasoner {{LocalTypeReasoner}} which has reasoned over the AST.
 * @param node {{AstNode}} to get info from.
 * @param builder {{StringBuilder}} to fill.
 */
private void buildTypeDoc(final ITypeKeeper typeKeeper, final LocalTypeReasoner reasoner, final AstNode node, final StringBuilder builder) {
    // Get type from reasoner.
    final ExpressionResult result = reasoner.getNodeType(node);
    // We know what self is.
    AbstractType type = result.get(0, UndefinedType.INSTANCE);
    if (type == SelfType.INSTANCE) {
        final AstNode methodDefNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
        if (methodDefNode == null) {
            // This can happen in case of a procedure definition calling a method on _self.
            type = UndefinedType.INSTANCE;
        } else {
            final MethodDefinitionNodeHelper methodDefHelper = new MethodDefinitionNodeHelper(methodDefNode);
            final GlobalReference globalRef = methodDefHelper.getTypeGlobalReference();
            type = typeKeeper.getType(globalRef);
        }
    }
    this.buildTypeSignatureDoc(type, builder);
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Example 5 with MethodDefinitionNodeHelper

use of nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper in project magik-tools by StevenLooman.

the class LocalTypeReasoner method getMethodOwnerType.

// endregion
private AbstractType getMethodOwnerType(final AstNode node) {
    final AstNode methodDefNode = node.getFirstAncestor(MagikGrammar.METHOD_DEFINITION);
    if (methodDefNode == null) {
        // This can happen in case of a procedure definition calling a method on _self.
        return UndefinedType.INSTANCE;
    }
    final MethodDefinitionNodeHelper helper = new MethodDefinitionNodeHelper(methodDefNode);
    final GlobalReference globalRef = helper.getTypeGlobalReference();
    return this.typeKeeper.getType(globalRef);
}
Also used : GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) MethodDefinitionNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)14 AstNode (com.sonar.sslr.api.AstNode)12 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)10 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)7 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)7 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)7 List (java.util.List)4 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)4 AstQuery (nl.ramsolutions.sw.magik.analysis.AstQuery)4 LocalTypeReasoner (nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner)4 SelfType (nl.ramsolutions.sw.magik.analysis.typing.types.SelfType)4 UndefinedType (nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType)4 MagikGrammar (nl.ramsolutions.sw.magik.api.MagikGrammar)4 Lsp4jConversion (nl.ramsolutions.sw.magik.languageserver.Lsp4jConversion)4 Position (org.eclipse.lsp4j.Position)4 ServerCapabilities (org.eclipse.lsp4j.ServerCapabilities)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3