Search in sources :

Example 1 with Range

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

the class HoverProvider method provideHover.

/**
 * Provide a hover at the given position.
 * @param magikFile Magik file.
 * @param position Position in file.
 * @return Hover at position.
 */
public Hover provideHover(final MagikTypedFile magikFile, final Position position) {
    // Parse and reason magik.
    final AstNode node = magikFile.getTopNode();
    final AstNode hoveredTokenNode = AstQuery.nodeAt(node, Lsp4jConversion.positionFromLsp4j(position));
    if (hoveredTokenNode == null) {
        return null;
    }
    final AstNode hoveredNode = hoveredTokenNode.getParent();
    LOGGER.debug("Hovering on: {}", hoveredNode.getTokenValue());
    // See what we should provide a hover for.
    final StringBuilder builder = new StringBuilder();
    if (hoveredNode.is(MagikGrammar.IDENTIFIER) && hoveredNode.getParent().is(MagikGrammar.METHOD_DEFINITION)) {
        // Method definition (exemplar or method name).
        this.provideHoverMethodDefinition(magikFile, hoveredNode, builder);
    } else if (hoveredNode.is(MagikGrammar.IDENTIFIER) && hoveredNode.getParent().is(MagikGrammar.METHOD_INVOCATION)) {
        this.provideHoverMethodInvocation(magikFile, hoveredNode, builder);
    } else if (hoveredNode.getParent().is(MagikGrammar.ATOM) || hoveredNode.getParent().is(MagikGrammar.SLOT)) {
        final AstNode atomNode = hoveredNode.getFirstAncestor(MagikGrammar.ATOM);
        this.provideHoverAtom(magikFile, atomNode, builder);
    } else if (hoveredNode.getParent().is(MagikGrammar.PARAMETER)) {
        final AstNode parameterNode = hoveredNode.getParent();
        this.provideHoverAtom(magikFile, parameterNode, builder);
    }
    final MarkupContent contents = new MarkupContent(MarkupKind.MARKDOWN, builder.toString());
    final Range range = new Range(hoveredTokenNode);
    final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
    return new Hover(contents, rangeLsp4j);
}
Also used : Hover(org.eclipse.lsp4j.Hover) Range(nl.ramsolutions.sw.magik.analysis.Range) MarkupContent(org.eclipse.lsp4j.MarkupContent) AstNode(com.sonar.sslr.api.AstNode)

Example 2 with Range

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

the class MagikCheck method addIssue.

/**
 * Add a new issue.
 * @param startLine Start line of issue.
 * @param startColumn Start column of issue.
 * @param endLine End line of issue.
 * @param endColumn End column of issue.
 * @param message Message of issue.
 */
public void addIssue(final int startLine, final int startColumn, final int endLine, final int endColumn, final String message) {
    final URI uri = this.getMagikFile().getUri();
    final Position startPosition = new Position(startLine, startColumn);
    final Position endPosition = new Position(endLine, endColumn);
    final Range range = new Range(startPosition, endPosition);
    final Location location = new Location(uri, range);
    this.addIssue(location, message);
}
Also used : Position(nl.ramsolutions.sw.magik.analysis.Position) Range(nl.ramsolutions.sw.magik.analysis.Range) URI(java.net.URI) Location(nl.ramsolutions.sw.magik.analysis.Location)

Example 3 with Range

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

the class LineLengthCheck method walkPreMagik.

@Override
protected void walkPreMagik(final AstNode node) {
    final MagikFile magikFile = this.getMagikFile();
    String[] lines = magikFile.getSourceLines();
    if (lines == null) {
        lines = new String[] {};
    }
    int lineNo = 1;
    for (final String line : lines) {
        final AtomicInteger columnNo = new AtomicInteger(0);
        final AtomicInteger issueColumnNo = new AtomicInteger(0);
        line.chars().forEachOrdered(chr -> {
            final int cur;
            if (chr == '\t') {
                cur = columnNo.addAndGet(TAB_WIDTH);
            } else {
                cur = columnNo.incrementAndGet();
            }
            if (cur <= this.maxLineLength + 1) {
                issueColumnNo.incrementAndGet();
            }
        });
        if (columnNo.get() > this.maxLineLength) {
            final URI uri = this.getMagikFile().getUri();
            final Position startPosition = new Position(lineNo, issueColumnNo.get() - 1);
            final Position endPosition = new Position(lineNo, line.length());
            final Range range = new Range(startPosition, endPosition);
            final Location location = new Location(uri, range);
            final String message = String.format(MESSAGE, line.length(), this.maxLineLength);
            this.addIssue(location, message);
        }
        ++lineNo;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Position(nl.ramsolutions.sw.magik.analysis.Position) MagikFile(nl.ramsolutions.sw.magik.MagikFile) Range(nl.ramsolutions.sw.magik.analysis.Range) URI(java.net.URI) Location(nl.ramsolutions.sw.magik.analysis.Location)

Example 4 with Range

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

the class RenameProvider method providePrepareRename.

/**
 * Provide prepare rename.
 * @param magikFile Magik file.
 * @param position Position in magik source.
 * @return Prepare rename or null if no rename possible.
 */
public Either<org.eclipse.lsp4j.Range, PrepareRenameResult> providePrepareRename(final MagikTypedFile magikFile, final Position position) {
    // 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;
    }
    final Range range = new Range(node);
    final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
    final String identifier = node.getTokenOriginalValue();
    final PrepareRenameResult result = new PrepareRenameResult(rangeLsp4j, identifier);
    return Either.forRight(result);
}
Also used : ScopeEntry(nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry) PrepareRenameResult(org.eclipse.lsp4j.PrepareRenameResult) Range(nl.ramsolutions.sw.magik.analysis.Range) AstNode(com.sonar.sslr.api.AstNode)

Example 5 with Range

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

the class Lsp4jConversion method locationToLsp4j.

/**
 * Convert a Location to LSP4J.
 * @param location Location to convert.
 * @return Location in LSP4J.
 */
public static org.eclipse.lsp4j.Location locationToLsp4j(final Location location) {
    final String uri = location.getUri().toString();
    final Range range = location.getRange();
    final org.eclipse.lsp4j.Range rangeLsp4j = Lsp4jConversion.rangeToLsp4j(range);
    return new org.eclipse.lsp4j.Location(uri, rangeLsp4j);
}
Also used : Range(nl.ramsolutions.sw.magik.analysis.Range) Location(nl.ramsolutions.sw.magik.analysis.Location)

Aggregations

Range (nl.ramsolutions.sw.magik.analysis.Range)8 AstNode (com.sonar.sslr.api.AstNode)4 Location (nl.ramsolutions.sw.magik.analysis.Location)3 URI (java.net.URI)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 CheckForNull (javax.annotation.CheckForNull)2 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)2 AstQuery (nl.ramsolutions.sw.magik.analysis.AstQuery)2 Position (nl.ramsolutions.sw.magik.analysis.Position)2 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)2 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)2 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)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