Search in sources :

Example 1 with AstNode

use of com.sonar.sslr.api.AstNode 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 AstNode

use of com.sonar.sslr.api.AstNode 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 AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class MagikIndexer method handleDefinition.

private void handleDefinition(final MagikFile magikFile, final IndexedExemplarDefinition definition) {
    final AstNode node = definition.getNode();
    final GlobalReference globalRef = definition.getGlobalReference();
    final MagikType type = this.typeKeeper.getType(globalRef) != UndefinedType.INSTANCE ? (MagikType) this.typeKeeper.getType(globalRef) : new IndexedType(globalRef);
    this.typeKeeper.addType(type);
    final Map<String, String> slots = Collections.emptyMap();
    final List<String> parents = definition.getParents();
    final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:indexed_format_mixin"));
    this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
    final Path path = Paths.get(magikFile.getUri());
    this.indexedTypes.get(path).add(type);
    LOGGER.debug("Indexed type: {}", type);
}
Also used : Path(java.nio.file.Path) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)

Example 4 with AstNode

use of com.sonar.sslr.api.AstNode in project magik-tools by StevenLooman.

the class MagikIndexer method handleDefinition.

private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDefinition definition) {
    final AstNode node = definition.getNode();
    final GlobalReference globalRef = definition.getGlobalReference();
    final MagikType type = this.typeKeeper.getType(globalRef) instanceof SlottedType ? (MagikType) this.typeKeeper.getType(globalRef) : new SlottedType(globalRef);
    this.typeKeeper.addType(type);
    final NewDocParser docParser = new NewDocParser(node);
    final Map<String, String> slotTypes = docParser.getSlotTypes();
    // This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
    final Map<String, String> slots = definition.getSlots().stream().map(SlottedExemplarDefinition.Slot::getName).collect(Collectors.toMap(slotName -> slotName, slotName -> slotTypes.getOrDefault(slotName, "")));
    final List<String> parents = definition.getParents();
    final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:slotted_format_mixin"));
    this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
    final Path path = Paths.get(magikFile.getUri());
    this.indexedTypes.get(path).add(type);
    LOGGER.debug("Indexed type: {}", type);
}
Also used : NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) Method(nl.ramsolutions.sw.magik.analysis.typing.types.Method) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition) IndexedType(nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType) LoggerFactory(org.slf4j.LoggerFactory) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) Package(nl.ramsolutions.sw.magik.analysis.typing.types.Package) AliasType(nl.ramsolutions.sw.magik.analysis.typing.types.AliasType) IndexedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.IndexedExemplarDefinition) UndefinedType(nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType) EnumerationDefinition(nl.ramsolutions.sw.magik.analysis.definitions.EnumerationDefinition) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) MagikGrammar(nl.ramsolutions.sw.magik.api.MagikGrammar) EnumSet(java.util.EnumSet) MethodInvocationNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper) MethodDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MethodDefinition) TypeAnnotationHandler(nl.ramsolutions.sw.magik.analysis.typing.TypeAnnotationHandler) PackageNodeHelper(nl.ramsolutions.sw.magik.analysis.helpers.PackageNodeHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ParameterDefinition(nl.ramsolutions.sw.magik.analysis.definitions.ParameterDefinition) GlobalDefinition(nl.ramsolutions.sw.magik.analysis.definitions.GlobalDefinition) HashMap(java.util.HashMap) SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) ITypeKeeper(nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper) HashSet(java.util.HashSet) TypeParser(nl.ramsolutions.sw.magik.analysis.typing.TypeParser) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) Location(nl.ramsolutions.sw.magik.analysis.Location) Nullable(javax.annotation.Nullable) GlobalScope(nl.ramsolutions.sw.magik.analysis.scope.GlobalScope) MagikCommentExtractor(nl.ramsolutions.sw.magik.parser.MagikCommentExtractor) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) Token(com.sonar.sslr.api.Token) Slot(nl.ramsolutions.sw.magik.analysis.typing.types.Slot) Logger(org.slf4j.Logger) MagikFile(nl.ramsolutions.sw.magik.MagikFile) IOException(java.io.IOException) Parameter(nl.ramsolutions.sw.magik.analysis.typing.types.Parameter) NewDocParser(nl.ramsolutions.sw.magik.parser.NewDocParser) BinaryOperatorDefinition(nl.ramsolutions.sw.magik.analysis.definitions.BinaryOperatorDefinition) MixinDefinition(nl.ramsolutions.sw.magik.analysis.definitions.MixinDefinition) IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) PackageDefinition(nl.ramsolutions.sw.magik.analysis.definitions.PackageDefinition) Paths(java.nio.file.Paths) Definition(nl.ramsolutions.sw.magik.analysis.definitions.Definition) ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) Scope(nl.ramsolutions.sw.magik.analysis.scope.Scope) Collections(java.util.Collections) BinaryOperator(nl.ramsolutions.sw.magik.analysis.typing.BinaryOperator) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) Path(java.nio.file.Path) SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) GlobalReference(nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) SlottedExemplarDefinition(nl.ramsolutions.sw.magik.analysis.definitions.SlottedExemplarDefinition)

Example 5 with AstNode

use of com.sonar.sslr.api.AstNode 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)

Aggregations

AstNode (com.sonar.sslr.api.AstNode)412 Test (org.junit.jupiter.api.Test)91 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)71 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)49 Token (org.sonar.plugins.python.api.tree.Token)46 Test (org.junit.Test)45 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)41 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)40 Token (com.sonar.sslr.api.Token)39 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)39 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)39 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)38 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)38 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)38 Expression (org.sonar.plugins.python.api.tree.Expression)38 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)38 List (java.util.List)35 RuleTest (org.sonar.python.parser.RuleTest)34 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)33 ArrayList (java.util.ArrayList)30